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.

Download

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>