Permalink
Browse files

Adding samples and readme example

  • Loading branch information...
MattTuttle committed Apr 3, 2015
1 parent 708ecdc commit b9bc3348220b72e3b7570555c887fae9189f5b56
Showing with 116 additions and 21 deletions.
  1. +53 −0 README.md
  2. +0 −3 build.hxml
  3. +17 −0 samples/build.hxml
  4. +24 −0 samples/telnet/Client.hx
  5. +17 −0 samples/telnet/Echo.hx
  6. +5 −18 samples/websocket/{Main.hx → Echo.hx}
View
@@ -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
View

This file was deleted.

Oops, something went wrong.
View
@@ -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
View
@@ -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();
}
}
}
View
@@ -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();
}
}
@@ -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.