So, I created a simple websocket client implementation using Haxe NME (HTML5 ofc target).
It connects to
ws://echo.websocket.org (sorry no link, SO sees this as an invalid domain)
which works great! (I use xirsys_stdjs haxelib to use the HTML5 website material.)
I want to have a local (on my own machine) launch of the websocket server . I am using Socket.io at the moment because I cannot find a lighter / simpler solution that I can work with.
I'm currently trying to use socket.io as a socket server, but the "standard" javascript socket implementation as a client (Haxe HTML5) without using the socket.io clientside library.
Does anyone know if this is possible? because I can't get it to work. Here is my socket.io code:
var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(1337); function handler (req, res) { fs.readFile(__dirname + '/client.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } // WEBSOCKET IMPLEMENTATION io.sockets.on('connection', function (socket) { console.log("webSocket connected..."); socket.on('message', function () { console.log("server recieved something"); // TODO: find out how to access data recieved. // probably 'msg' parameter, omitted in example? }); socket.on('disconnect', function () { console.log("webSocket disconnected."); }); });
And here is my Haxe code (client):
static var webSocketEndPoint:String = "ws://echo.websocket.org"; //static var webSocketEndPoint:String = "ws://localhost:1337"; ... private function initializeWebSocket ():Void { if (untyped __js__('"MozWebSocket" in window') ) { websocket = new MozWebSocket(webSocketEndPoint); trace("websocket endpoint: " + webSocketEndPoint); } else { websocket = new WebSocket(webSocketEndPoint); } // add websocket JS events websocket.onopen = function (event:Dynamic):Void { jeash.Lib.trace("websocket opened..."); websocket.send("hello HaXe WebSocket!"); } websocket.onerror = function (event:Dynamic):Void { jeash.Lib.trace("websocket erred... " + event.data); } websocket.onmessage = function (event:Dynamic):Void { jeash.Lib.trace("recieved message: " + event.data); switchDataRecieved(event.data); } websocket.onclose = function (event:Dynamic):Void { jeash.Lib.trace("websocket closed."); } }
If the Haxe code is unclear: it uses 2 external classes to implement webSocket: MozWebSocket and WebSocket. These are just βinterfacesβ for the corresponding JavaScript classes.
source share