socket-bar

Websockets for multiple Haxe platforms
https://github.com/aBARICHELLO/socket-bar

To install, run:

haxelib install socket-bar 1.4.0 

See using Haxelib in Haxelib documentation for more information.

README.md

socket-bar

Fork of https://github.com/soywiz/haxe-ws

Haxe Websockets supporting the following targets: - C++ - HashLink - JavaScript

Incomplete support for Java and C#

Examples

Client

import haxe.io.Bytes;
import hx.ws.Log;
import hx.ws.WebSocket;

class Main {
    static function main() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var ws = new WebSocket("ws://localhost:5000");

        ws.onopen = function() {
            ws.send("alice string");
            ws.send(Bytes.ofString("alice bytes"));
        }

        #if sys
        Sys.getChar(true);
        #end
    }
}

Server

Main.hx

import hx.ws.Log;
import hx.ws.WebSocketServer;

class Main {
    static function main() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var server = new WebSocketServer<MyHandler>("localhost", 5000, 10);
        server.start();
    }
}

MyHandler.hx

import hx.ws.SocketImpl;
import hx.ws.WebSocketHandler;
import hx.ws.Types;

class MyHandler extends WebSocketHandler {
    public function new(s: SocketImpl) {
        super(s);
        onopen = function() {
            trace(id + ". OPEN");
        }
        onclose = function() {
            trace(id + ". CLOSE");
        }
        onmessage = function(message: MessageType) {
            switch (message) {
                case BytesMessage(content):
                    trace(content.readAllAvailableBytes());
                case StrMessage(content):
                    var str = "echo: " + content;
                    trace(str);
                    send(str);
            }
        }
        onerror = function(error) {
            trace(id + ". ERROR: " + error);
        }
    }
}
Contributors
ianharrigan
barichello
Version
1.4.0
Published
6 years ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub