Haxe: Neko Server-client communication example (chat)

Recently I've been searching for examples of client-server communication in Haxe, however could not find anything specific. After some search and asking around, I was pointed to sys.net.Socket class, but the actual means of usage remained unclear. It was also confirmed to mirror POSIX socket functionality. Indeed it does that, though, given that Haxe implementation uses exceptions rather than return values, usage remained uneasy.

After some experimenting, I've figured a semi-simple way of using "blocking" sockets.

Since the socket is blocking the thread activity (could've guessed by name) while it's reading/writing, a logical thing is to make a new (separate) thread for it to run in. While it may not be an excellent idea with native Windows/*nix threads (which have a fixed-size stack memory area attached to them), Neko threads remain nice and small, only increasing memory usage by few kilobytes each (depends on thread contents). Thus there are two threads to be put in client application.
Server, on other hand, is similar, but slightly different. Unlike client, it has to accept and manipulate multiple sockets (clients). As well it needs a separate thread to listen for new connections.
While I went for a custom implementation of these things, it turned out that there's this nice neko.net.ThreadServer class (and even a sort of tutorial about it), which serves similar purposes (doesn't save one from writing client applications though).

This example features a simple chat, including basic actions such as joining, leaving, and sending messages, as well as some examples of command implementations, including /list (displays online users), /name (changes name), and /msg (sends private message).
In the good traditions of server applications, server displays full activity log.
Also there's a considerably fancy "RawEdit" class, which I wrote to allow entering text on stdin+stdout (that is, seeing what you're typing) while other data is being sent to the same stdout handle.

Overall, this should be useful.


Related posts:

4 thoughts on “Haxe: Neko Server-client communication example (chat)

  1. RawEdit class will block my socket send/get?

    //here is my code
    function threadListen() {
    		var buffer = Bytes.alloc(1024);
            var socks = [socket];
    		trace("waiting for message!");
            var timer = new haxe.Timer(100);
    		trace("time=" + Math.random());
            timer.run = function() {
                try {
                    var r:Array;
                    do {
                        r = sys.net.Socket.select(socks, null, null, 0.001).read;
                        for (s in r) {
                            var size = s.input.readBytes(buffer, 0, buffer.length);
                            message.addBytes(buffer.sub(0, size));
                    } while (r.length > 0);
                } catch (e:haxe.io.Eof) {
                } catch (e:Dynamic) {
                    #if haxe3
    		wait = false;
    		trace("not excute?");
    • RawEdit code is ran on the main thread while socket code should run on a different thread. Otherwise it’ll have to wait for RawEdit.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.