javascript 是否可以在 node.js 服务器上设置运行(服务器端)的 socket.io 客户端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9687561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set up a socket.io client running (server-side) on a node.js server?
提问by Myk
I'd like to enable socket-based p2p communications between two or more different node.js application servers. I'm using socket.io to handle all such communication between a given server and the web application it serves - but what I'm looking for is a way to communicate server-to-server.
我想在两个或多个不同的 node.js 应用程序服务器之间启用基于套接字的 p2p 通信。我正在使用 socket.io 来处理给定服务器与其所服务的 Web 应用程序之间的所有此类通信 - 但我正在寻找的是一种服务器到服务器通信的方式。
I had initially assumed it would be as easy as something like this:
我最初认为它会像这样简单:
var io = require("socket.io");
var socket = io.connect("my remote endpoint");
However, as it turns out the server-side socket.io implementation doesn't offer a "connect" method, only a listen method.
然而,事实证明服务器端 socket.io 实现不提供“连接”方法,只有一个监听方法。
Why is this? Why can't I treat a node application server as a client to a socket.io server running elsewhere? Is there any way that I can achieve this functionality?
为什么是这样?为什么我不能将节点应用程序服务器视为在其他地方运行的 socket.io 服务器的客户端?有什么办法可以实现这个功能吗?
回答by Myk
OK, so thanks to @pimvdb in the comments above I've got a workable solution.
好的,感谢上面评论中的@pimvdb,我有了一个可行的解决方案。
Basically, the socket.io library that npm installs has a dependency on another module, called socket.io-client. In a standard socket.io installation this will be installed in node_modules/socket.io/node_modules/socket.io-client
基本上,npm 安装的 socket.io 库依赖于另一个模块,称为 socket.io-client。在标准的 socket.io 安装中,这将安装在 node_modules/socket.io/node_modules/socket.io-client
However, it's also possible to say "npm install socket.io-client" and install it as its own first-class citizen library.
但是,也可以说“npm install socket.io-client”并将其安装为自己的一流公民库。
Then your usage looks like this:
然后你的用法看起来像这样:
var client = require("socket.io-client");
var socket = client.connect("http://myendpoint.com:3000/whatever");
socket.emit("test", "foo");
And everything works.
一切正常。
So, thanks man!
所以,谢谢大佬!
回答by Luca Rainone
Just for clarification, this is an example with listeners and possibility to emit events (and without install again a module already installed)
只是为了澄清,这是一个带有侦听器和发出事件可能性的示例(无需再次安装已安装的模块)
var io = require('socket.io/node_modules/socket.io-client');
client = io.connect('http://'+CONFIG.host+':'+CONFIG.port);
client.on('connect',function() {
client.emit("test","foo");
});
回答by Mark
Before you go full speed on socket.io for server-to-server communications..... socket.io is engineered as a browser to server comm infrastructure. I'm far from certain it is the best solution for P2P server stuff. Plus, if you do server-to-server - why not just do Websockets? There are various websocket modules for node - e.g. https://github.com/einaros/ws
在您全速使用 socket.io 进行服务器到服务器通信之前...... socket.io 被设计为服务器通信基础设施的浏览器。我不确定它是 P2P 服务器的最佳解决方案。另外,如果你做服务器到服务器 - 为什么不只做 Websockets?节点有各种 websocket 模块 - 例如https://github.com/einaros/ws