NodeJS 的 Web Sockets 服务器端实现 [英] Web Sockets server side implementation for NodeJS

查看:22
本文介绍了NodeJS 的 Web Sockets 服务器端实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的部分原因可能是客户端的讨论太多,让我见木不见林.

Part of this problem might be that there's too much discussion on the client side for me to see wood for the trees.

无论如何,这就是我想要做的.我需要一个独立于平台的 WebSockets 服务器端实现.我希望它在 NodeJS 中运行.

Anyway, here's what I want to do. I need a platform independent server-side implementation of WebSockets. I'd like it to run in NodeJS.

现在,我在这个主题上找到的 99% 的内容都与 socket.io 相关.但据我所知,这不是 WebSockets,它是一个特殊的额外"工具.协议本身.我需要一些符合(尚未)标准"的东西.这样做是有充分理由的,而且不容商量,请相信我.

Now, 99% of what I've found on this topic talks about socket.io. But so far as I can tell, that is not WebSockets, it's a special "extra" protocol in its own right. I need something that works "by the (not-yet) standard". There's a good reason for that, and it's non-negotiable, trust me on that.

所以,我尝试了 WebSocket,但这需要(或似乎需要 Python 和更糟的是 Visual Studio)在 Windows 上运行.我需要一些独立于平台的东西,不需要像这样的特殊东西.

So, I tried WebSocket, but that requires (or appears to require both python and, worse, Visual Studio) to run on Windows. I need something that is platform independent and doesn't need special things like this.

我也尝试过 node-websocket-server,但我根本无法让它工作.主页上的示例对我来说失败了.它似乎接受了一个连接,但客户端没有看到它,双方都不能发送任何东西,客户端立即看到连接已关闭.事实上,我所得到的只是一个连接".回调,然后就好像不行了.在调试模式下运行并没有告诉我任何有用的信息,除了一些关于某个对象或其他没有 flush() 方法的内部错误.我有点怀疑这是一个不复存在的项目?

I also tried node-websocket-server, but I can't get that to work at all. The example on the main page fails for me. It seems to accept a connection, but the client doesn't see it, neither side gets to send anything, and the client immediately sees the connection as closed. Indeed, all I ever get is a "connection" callback, and then it seems to die. Running in debug mode didn't tell me anything useful, except for some internal error about some object or other not having a flush() method. I half-suspect this is a defunct project?

所以,我没有想法了.是否有可能说服 socket.io 完全按照 WebSockets 的(非)规范工作?有没有办法让 node-websocket-server 表现出我找不到的行为.有没有办法绕过 websocket 中的 Visual Studio 依赖项,或者是否有其他一些基于 NodeJS 的工具可以满足我的所有要求?

So, I'm out of ideas. Is it possible to persuade socket.io to work purely by the (non)spec for WebSockets? Is there a way to get node-websocket-server to behave that I've failed to find. Is there a way round the Visual Studio dependency in websocket, or is there some other NodeJS based tool that meets all my requirements?

哦,还有一件事,我希望该工具与连接"和平共处.因为我将它用于我的常规文档服务.

Oh, one other thing, I'd like the tool to coexist peacefully with "connect" as I'm using that for my regular document serving.

推荐答案

当我尝试在不同平台上使用 Socket.IO 时,我遇到了与您完全相同的问题没有客户端的直接端口(也没有自己移植的动机).

I had the exact same problem that you're facing at the moment when I tried to use Socket.IO on a different platform without a direct port of the client (and without the motivation to port it myself).

我最终将我的代码移动到使用 ws,这是一个基于标准的 websocket 节点实现,没有从 socket.io 添加糖.

I ended up moving my code to use ws which is a standards based websocket implementation for node without the added sugar from socket.io.

在我的情况下,它在几个不同的平台上运行得非常好,但您需要重新编写大部分连接/重新连接代码等.

It works extremely well in my case over several different platforms but you would need to rework most of the connection/reconnection code etc.

网站:链接

GitHub:链接

NPM : npm install ws

这篇关于NodeJS 的 Web Sockets 服务器端实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆