如何将两个node.js服务器与websockets连接? [英] How to connect two node.js servers with websockets?
问题描述
这是我的问题:
我有服务器A,运行node.js并使用socket.io与客户端(Web浏览器)进行通信.这一切都很好,花花公子.
I have server A, running node.js and using socket.io for communicating with clients (web browsers). This all is running fine and dandy.
但是,现在我有了服务器B,也需要通过websocket连接到服务器A,所以我遇到了麻烦.我发现,没有一个node.js websocket客户端不能与服务器A上的socket.io一起使用.
However, now that I have server B, which also needs to connect to server A through websockets, I have hit a wall. None of the node.js websocket clients I've found won't work with the socket.io on the server A.
因此,我正在努力做到这一点:
So, this is the case I'm striving for:
.--------. .----------. .----------.
| CLIENT | <--> | SERVER A | <--> | SERVER B |
'--------' '----------' '----------'
客户端-服务器连接是通过socket.io完成的
Client-server A connection is done through socket.io
现在,服务器B(正在运行node.js)应该通过websocket连接到服务器A(以便通过端口80).但是...
Now, Server B (running node.js) should connect to server A via websocket (in order to go through port 80). But...
即使socket.io-client模块中的示例代码也不起作用...:/
Even the example code in socket.io-client module doesn't work... :/
// Connect to server
var socket = new io.Socket('localhost', {port: 8080});
socket.connect();
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected.');
});
代码通过时没有任何错误,执行在几秒钟后结束.
The code just passes without any errors and execution ends after few seconds.
更新:代码示例
服务器(可以正常工作)如下所示:
Server (which works just fine) looks like this:
// Load requirements
var http = require('http'),
io = require('socket.io');
// Create server & socket
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Aw, snap! 404</h1>');
});
server.listen(8080);
io = io.listen(server);
// Add a connect listener
io.sockets.on('connection', function(socket) {
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
客户长这样
console.log('1');
// Connect to server
var io = require('socket.io-client')
var socket = new io.Socket('localhost', {port: 8080});
socket.connect();
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
1、2和3可以很好地打印出来,没有错误,几秒钟后该过程就退出了
1, 2 and 3 prints out just fine, no errors, and few seconds later the process just exits
此外,即使我在"everything"上设置了socket.io日志记录,服务器A也不向日志输出任何内容.
Also, server A doesn't output anything to the log, even though I have the socket.io logging set on "everything".
推荐答案
由于某些原因,即使我对它们进行了三遍检查,我仍在使用旧示例.嗯,好吧.
Turns out I was using old examples, for some reason, even though I triple checked them. Well, doh.
此外,事实证明,socket.io-client在最新的Node(6.x.x)上已损坏.设法从github找到了一个更新,替换了文件,是的,一切正常!
Also, it turned out that the socket.io-client is broken on latest Node (6.x.x). Managed to find an update from github for it, replaced the files and yay, everything's working!
编辑:不幸的是,我没有保存任何到工作示例的链接,但是在快速浏览了一下代码之后,似乎唯一的更改就是客户端代码,现在看起来像这样:>
Edit: Unfortunately I didn't save any links to working examples but after quickly skimming through the code it seems that the only changes were to the client code, which now looks like this:
console.log('1');
// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
这篇关于如何将两个node.js服务器与websockets连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!