Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io as server, 'standard' javascript as client?

So i've built a simple websocket client implementation using Haxe NME (HTML5 target ofc).
It connects to

ws://echo.websocket.org (sorry no link, SO sees this as an invalid domain)

which works perfectly! (i'm using xirsys_stdjs haxelib to use the HTML5 websocket stuff.)

I want to have a local (on my own machine) running websocket server. I'm using Socket.io at the moment, because i cannot find an easier / simpler solution to go with.

I'm currently trying to use socket.io as socket server, but a 'standard' javascript socket implementation as client (Haxe HTML5), without using the socket.io library clientside.

Does anyone know if this should be possible? because i cannot get it working. Here's 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's my Haxe (client) code:

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.");
    }
}

In case the Haxe code is unclear: it's using 2 extern classes for the webSocket implementation: MozWebSocket and WebSocket. These are just typed 'interfaces' for the corresponding JavaScript classes.

like image 870
Michael Trouw Avatar asked May 14 '12 12:05

Michael Trouw


People also ask

Can I use socket IO client to connect to a standard WebSocket?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

websocket.io! from the same guys. sample shows exact same thing that you are asking about... and something that I spent past 20 hours searching for (and finally found!)

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

Update: Jan 2014

The websocket.io repository has not seen any activity for about 2 years. It could be because it is stable, or it could be because it is abandoned.

The same people have another repository called engine.io. In the readme they say that this is isomorphic with websocket.io... It seems that engine.io is where all the action is these days.

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

like image 53
Kinjal Dixit Avatar answered Oct 10 '22 02:10

Kinjal Dixit