Socket.io as a server, "standard" javascript as a client?

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.

+6
source share
3 answers

websocket.io! from the same guys. the sample shows the same thing you are asking for ... and what I spent 20 hours looking for (and finally found!)

https://github.com/LearnBoost/websocket.io

Update: January 2014

There has been no activity in websocket.io for about 2 years. This may be because it is stable, or it may be because it is abandoned.

The same people have another repository called engine.io. In the readme, they say this is isomorphic to websocket.io ... Engine.io seems to be where all the action these days is.

https://github.com/LearnBoost/engine.io

+4
source

While searching for the same thing, I found https://github.com/einaros/ws/ , and her server example worked for me with my previously existing simple javascript client.

0
source

http://socket.io/#how-to-use Following the link down to the bottom of the page, the socket.io documentation demonstrates how to last. For example, how to use your module as a simple old xbrowser webSocket server.

SERVER

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on('message', function () { }); socket.on('disconnect', function () { }); }); 

BROWSER

 <script> var socket= io.connect('http://localhost/'); socket.on('connect', function () { socket.send('hi'); socket.on('message', function (msg) { // my msg }); }); </script> 

Hope you are looking

- Doc

-2
source

Source: https://habr.com/ru/post/915664/


All Articles