gpt4 book ai didi

node.js - NodeJS 服务器之间的消息

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:15 25 4
gpt4 key购买 nike

想象一下,我有 5 个 NodeJS 服务器运行一些随机操作(例如接收数据 - 该操作意味着客户端与服务器连接)。现在我想创建第 6 个 NodeJS 服务器。我希望 5 台服务器与这台新服务器连接,当新客户端与 5 台服务器之一连接时,它们将发送一条消息来讲述这个故事(到第 6 台服务器)。如何在 NodeJS 服务器之间共享消息?

我的尝试:第 6 个 NodeJS 服务器:

binaryServer = BinaryServer({ port: 9003}); //Stream de Entrada

binaryServer.on('connection', function (client) {

console.log(client.id);


client.on('stream', function (stream, meta) {

console.log(client.id +"||" +stream.id);

console.log(meta);

console.log(meta);

stream.on('data', function(data){

console.log(data);
stream.write("Recebi-----> "+data);

})



stream.on('end', function () {

console.log("end");
});

stream.on('error', function (error) {
console.log(error);
});

stream.on('pause', function () {
console.log("pause");

});

stream.on('resume', function () {
console.log("resume");
});

});

client.on('close', function(){

console.log("close: "+client.id);

});

});

5 个服务器之一:

binaryclient = BinaryClient('ws://localhost:9003');

//(.....)

binaryclient.on('open', function() {
console.log("opened connection");

StreamC = binaryclient.createStream("dasda");

StreamC.write("Somethin like" + total_clients);

StreamC.on('data', function(data){

console.log(data);

});

StreamC.on('error', function (error) {
console.log(error);
});

});

使用 Binarjs给出这个错误:NodeJS 中的“错误:使用服务器端代码监听 EADDRINUSE”。

最佳答案

关于可能的解决方案就在答案中。我的错误非常简单...因为我做了代码复制,所以我在两台服务器的同一端口中定义了 httserver 。这就是错误发生的原因。尽管有一种可能的方法是使用 BinaryJS

关于node.js - NodeJS 服务器之间的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com