【问题标题】:Messages Between NodeJS ServersNodeJS 服务器之间的消息
【发布时间】:2015-12-06 00:30:44
【问题描述】:

假设我有 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”。

【问题讨论】:

  • 我可能认为通过 node js 的内置是不可能的,但我认为您可能需要为它编写一些 api 并在服务器之间共享它
  • "如何在 NodeJS 服务器之间共享消息?" — 有很多不同的方法(投票关闭,因为它太宽泛了),一个简单、明显但低效的方法是侦听和发送 HTTP 请求。
  • 用新方法编辑

标签: node.js


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-12
    • 2011-02-10
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多