Skip to content

Commit

Permalink
Adding samples and readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
MattTuttle committed Apr 3, 2015
1 parent 708ecdc commit b9bc334
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 21 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,59 @@ On top of the transport layers are the Client and Server classes. These handle s

When creating a Client or Server object you need to pass a Protocol to it. A protocol defines the specific interactions between a client and server. For example, you can create a custom RPC based protocol that easily calls function on the server from a client (or vice versa). Another example of a protocol would be HTTP which could generate a reply from the server to a browser.

## Example Echo Server

Let's say you want to create a basic telnet echo server. We can do so by extending the Telnet protocol and overriding the `lineReceived` function.

```haxe
class Echo extends hxnet.protocols.Telnet
{
override private function lineReceived(line:String)
{
writeLine(line);
}
static public function main()
{
var server = new hxnet.tcp.Server(new hxnet.base.Factory(Echo), 4000);
server.start();
}
}
```

The main function creates an instance of a TCP server (port 4000) and uses the base Factory class to create a new instance of Echo for every client connection. Then it calls `start` which is a shortcut for listening on a port and updating infinitely.

## Example Client

You may want to connect to a server to retrieve data. By default hxnet blocks on all connections which can be a problem with gui applications because it will lock up the rendering. We can fix that by setting `blocking` to false.

```haxe
class Client extends hxnet.protocols.Telnet
{
override private function lineReceived(line:String)
{
trace(line);
}
static public function main()
{
var client = new hxnet.tcp.Client();
client.protocol = new Client(); // set the protocol we want to use
client.connect("localhost", 4000);
client.blocking = false; // important for gui clients
while (true)
{
client.update();
// add application logic here
}
}
}
```

## License

Expand Down
3 changes: 0 additions & 3 deletions build.hxml

This file was deleted.

17 changes: 17 additions & 0 deletions samples/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-cp samples
-cp ..

--each

-neko websocket.n
-main websocket.Echo

--next

-neko telnet.n
-main telnet.Echo

--next

-neko client.n
-main telnet.Client
24 changes: 24 additions & 0 deletions samples/telnet/Client.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package telnet;

class Client extends hxnet.protocols.Telnet
{

override private function lineReceived(line:String)
{
trace(line);
}

static public function main()
{
var client = new hxnet.tcp.Client();
client.protocol = new Client();
client.connect("localhost", 4000);

while (true)
{
// application logic
client.update();
}
}

}
17 changes: 17 additions & 0 deletions samples/telnet/Echo.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package telnet;

class Echo extends hxnet.protocols.Telnet
{

override private function lineReceived(line:String)
{
writeLine(line);
}

static public function main()
{
var server = new hxnet.tcp.Server(new hxnet.base.Factory(Echo), 4000, "localhost");
server.start();
}

}
23 changes: 5 additions & 18 deletions samples/websocket/Main.hx → samples/websocket/Echo.hx
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import hxnet.tcp.Server;
package websocket;

class Echo extends hxnet.protocols.WebSocket
{

override private function recvText(text:String) {
sendText(text);
}

override private function recvBinary(data:haxe.io.Bytes) {
sendBinary(data);
}
}

class Main
{

public function new()
{
server = new Server(new hxnet.base.Factory(Echo), 4000, "localhost");
server.listen();
while (true)
{
server.update();
}
}

static public function main()
{
new Main();
var server = new hxnet.tcp.Server(new hxnet.base.Factory(Echo), 4000, "localhost");
server.start();
}

private var server:Server;
}
}

0 comments on commit b9bc334

Please sign in to comment.