简体   繁体   中英

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.

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

在搜索同样的东西时,我刚刚找到了https://github.com/einaros/ws/ ,它的服务器示例为我使用我已经存在的普通javascript客户端。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM