Socket.io as server, 'standard' javascript as client?
Asked Answered
I

3

7

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.

Innkeeper answered 14/5, 2012 at 12:1 Comment(0)
G
4

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

Gabrielagabriele answered 12/12, 2012 at 5:21 Comment(3)
well, damn, that would've been handy for me a year ago :) Though, nice of you to post it! May it help people looking for the same! Also, nice that they implemented support for a wide array of websocket specs. i found that different spec implementations can be a real PITA.Innkeeper
@span no updates could mean abandoned, or it could mean completed :)Gabrielagabriele
January 2014: last commit was 2 years ago. looks abandoned to me.Vibrio
M
0

While searching for the same thing I just found https://github.com/einaros/ws/ and its server example worked for me with my pre-existing plain javascript client.

Marney answered 2/5, 2014 at 15:56 Comment(0)
A
-2

http://socket.io/#how-to-use At the mentioned link, down towards the bottom of the page, the socket.io documentation demonstrates as it's last example, how to use their module as a plain 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 that's what your looking for

--Doc

Abdomen answered 14/5, 2012 at 16:33 Comment(2)
Hey doc, thanks, but that wasn't what i was looking for. i'm looking for a default javascript (without using 'io.connect' which comes from the clientside socket.io javascript file) implementantion. this is because i'm using socket.io as replacement for my arduino websocket Server implementation. therefore, i cannot rely on socket.io clientside javascript. does this clear up my question? Thanks!Innkeeper
I see what you're talking about hopefully someone answers soon. -- Basically, instead of thinking in terms of your haxe code, you can speak more of talking between a socket.io server and native html5 javascript WebSocket (var socket = new WebSocket('ws://localhost');)Algetic

© 2022 - 2024 — McMap. All rights reserved.