javascript node.js 服务器之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17196161/
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
Communication between node.js servers
提问by prakash
I'm quite a bit new to node.js. I have question, can we connect two node.js servers? these 2 servers handle clients and perform there individual action I want to establish a connection between these two servers so that these 2 servers can share there status to each other.
我对 node.js 有点陌生。我有问题,我们可以连接两个 node.js 服务器吗?这两个服务器处理客户端并在那里执行单独的操作我想在这两个服务器之间建立连接,以便这两个服务器可以相互共享状态。
Can anyone please help me?
谁能帮帮我吗?
it is somewhat like this
有点像这样
server1 ==> room1[client1, client2,client3]
server1 ==> room1[client1, client2,client3]
server2 ==> room2[client4,client5, client6]
server2 ==> room2[client4,client5,client6]
here I want to make a communication between these two servers.
在这里我想在这两个服务器之间进行通信。
回答by eepp
Sure: just use a socketas you would with any other programming language that's capable of network communications.
当然:只需像使用任何其他能够进行网络通信的编程语言一样使用套接字即可。
One of the servers will need to listen on a TCP port (using net.createServer) and the other one connects to it using net.connect.
其中一台服务器需要侦听 TCP 端口(使用net.createServer),而另一台使用 连接到它net.connect。
This is easy if you really only have two servers. If you have more, you will need either a main "arbiter", i.e. a (listening) relay server that receives messages from other servers and transfer them to real recipients, or a mesh network(not a good starting point if you're a networking newbie).
如果您真的只有两台服务器,这很容易。如果您有更多,您将需要一个主要的“仲裁者”,即从其他服务器接收消息并将其传输给实际收件人的(侦听)中继服务器,或网状网络(如果您是网络新手)。
JsonSocketseems to be an interesting project for transfering JSON messages using raw TCP sockets, although I didn't test it myself.
JsonSocket似乎是一个使用原始 TCP 套接字传输 JSON 消息的有趣项目,尽管我自己没有测试过。

