Socket.io作为服务器,'标准'javascript作为客户端?

Mic*_*ouw 6 client-server haxe websocket socket.io

所以我使用Haxe NME(HTML5 target ofc)构建了一个简单的websocket客户端实现.
它连接到

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

哪作得很好!(我正在使用xirsys_stdjs haxelib来使用HTML5 websocket的东西.)

我希望有一个本地(在我自己的机器上)运行websocket 服务器.我现在正在使用Socket.io,因为我无法找到更简单/更简单的解决方案.

我目前正在尝试使用socket.io作为套接字服务器,但是使用"标准"javascript套接字实现作为客户端(Haxe HTML5),而不使用socket.io库clientside.

有谁知道这是否应该可行?因为我无法让它发挥作用.这是我的socket.io代码:

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

});
Run Code Online (Sandbox Code Playgroud)

这是我的Haxe(客户端)代码:

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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果Haxe代码不清楚:它使用2个extern类用于webSocket实现:MozWebSocket和WebSocket.这些只是相应JavaScript类的类型"接口".

小智 -2

http://socket.io/#how-to-use 在提到的链接中,在页面底部,socket.io 文档演示了如何将其模块用作普通的旧 xbrowser webSocket 服务器。

服务器

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket)
 {
  socket.on('message', function () { });
  socket.on('disconnect', function () { });
 });
Run Code Online (Sandbox Code Playgroud)

浏览器

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

希望这就是您所寻找的

——博士