简体   繁体   English

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

[英]Socket.io as server, 'standard' javascript as client?

So i've built a simple websocket client implementation using Haxe NME (HTML5 target ofc). 所以我使用Haxe NME(HTML5 target ofc)构建了一个简单的websocket客户端实现。
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.) (我正在使用xirsys_stdjs haxelib来使用HTML5 websocket的东西。)

I want to have a local (on my own machine) running websocket server . 我希望有一个本地(在我自己的机器上)运行websocket 服务器 I'm using Socket.io at the moment, because i cannot find an easier / simpler solution to go with. 我现在正在使用Socket.io,因为我找不到更简单/更简单的解决方案。

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 . 我目前正在尝试使用socket.io作为套接字服务器,但是使用“标准”javascript套接字实现作为客户端 (Haxe HTML5), 而不使用socket.io库clientside

Does anyone know if this should be possible? 有谁知道这是否应该可行? because i cannot get it working. 因为我无法让它发挥作用。 Here's my socket.io code: 这是我的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.");
   });

});

And here's my Haxe (client) code: 这是我的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.");
    }
}

In case the Haxe code is unclear: it's using 2 extern classes for the webSocket implementation: MozWebSocket and WebSocket. 如果Haxe代码不清楚:它使用2个extern类用于webSocket实现:MozWebSocket和WebSocket。 These are just typed 'interfaces' for the corresponding JavaScript classes. 这些只是相应JavaScript类的类型“接口”。

websocket.io! 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!) 示例显示了您要问的完全相同的事情......以及我花了20多个小时搜索的内容(最终找到了!)

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

Update: Jan 2014 更新:2014年1月

The websocket.io repository has not seen any activity for about 2 years. websocket.io存储库大约2年没有看到任何活动。 It could be because it is stable, or it could be because it is abandoned. 这可能是因为它是稳定的,或者可能是因为它被抛弃了。

The same people have another repository called engine.io. 同一个人有另一个名为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. 在自述文件中,他们说这与websocket.io是同构的...似乎engine.io是现在所有行动的地方。

https://github.com/LearnBoost/engine.io 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. http://socket.io/#how-to-use在上述链接中,向下到页面底部,socket.io文档演示了它的最后一个示例,如何将其模块用作普通的旧xbrowser webSocket服务器。

SERVER 服务器

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

io.sockets.on('connection', function (socket)
 {
  socket.on('message', function () { });
  socket.on('disconnect', function () { });
 });

BROWSER 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 --Doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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