如何使用 websockets 连接两个 node.js 服务器? [英] How to connect two node.js servers with websockets?

查看:25
本文介绍了如何使用 websockets 连接两个 node.js 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

我有服务器 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,它也需要通过 websockets 连接到服务器 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 |
'--------'      '----------'      '----------'

Client-server 一个连接是通过 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

此外,即使我将 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');

这篇关于如何使用 websockets 连接两个 node.js 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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