[英]Node.js Remote Start and Communication between Servers
I am new to Node.js and also pretty new to server communication. 我是Node.js的新手,也是服务器通信的新手。 I have tried to find previous answers, but they are often concerned about communication between server and client.
我试图找到以前的答案,但是他们经常关心服务器和客户端之间的通信。 I have a different case, so need your considerate helps.
我的情况有所不同,因此需要您的体贴帮助。
Let's assume a scenario that we have three systems, localhost (ie, laptop) and two cloud servers. 让我们假设有三个系统,即本地主机(即笔记本电脑)和两个云服务器。 I want to code an js app in the localhost that will slice an array of data into two blocks and send them to the cloud servers (block #1 to the server #1 and block #2 to the server #2).
我想在本地主机中编写一个js应用程序,它将一个数据数组切成两个块并将其发送到云服务器 (将块#1发送到服务器#1,将块#2发送到服务器#2)。 Receiving them, two remote servers start to work at the same time .
接收到它们之后, 两个远程服务器同时开始工作 。 Then, they do the same computation and send their calculation results to each other if they have updated values.
然后,它们进行相同的计算,如果它们具有更新的值,则将其计算结果相互发送 。
In this scenario, I want to tackle bolded sentences. 在这种情况下,我想处理加粗的句子。 I believe using the module "socket.io" will be a proper approach to handle this (especially, remote start and communication) but do not have any clear idea in designing codes.
我相信使用模块“ socket.io”将是处理此问题的正确方法(尤其是远程启动和通信),但是在设计代码时没有明确的思路。 In addition, understanding "socket.io" itself is a bit tricky.
另外,了解“ socket.io”本身有点棘手。 If you need further specification on the scenario, please comment.
如果您需要有关该场景的进一步说明,请发表评论。
Along with socket.io, check out a module named Faye ( http://faye.jcoglan.com/node.html ). 与socket.io一起,检出名为Faye的模块( http://faye.jcoglan.com/node.html )。 I have been using it for a couple of years and really like it.
我已经使用了几年了,真的很喜欢它。 Faye is a publish subscribe communication scheme which would allow you to extend your scenario to as many clients as you need.
Faye是一种发布订阅通信方案,可让您将方案扩展到所需的任意数量的客户端。 To install faye on your system, run the following command:
要在系统上安装faye,请运行以下命令:
npm install -g faye
Now, here is your server code: 现在,这是您的服务器代码:
var faye = require('faye');
var Server = new faye.NodeAdapter({mount: ('/FayeServer'), timeout: 120});
//now fire the server up on the port 5555
Server.listen(5555);
//subscribe to channel DataChannel
var Subscription = Server.getClient().subscribe("DataChannel",
function(dataObject){ console.log(dataObject) },
function(status) {
console.log('Subscription Status: ' + status);
//send message with two numbers to any client listening to DataChannel
Server.getClient().publish('/DataChannel', {A:5,B:12});
});
Now, here is the client code: 现在,这是客户端代码:
var faye = require('faye');
//open client to server
var Client = new faye.Client('http://127.0.0.1:5555/FayeServer');
//now subscribe to the channel DataChannel
Client.subscribe('/DataChannel', function(dataObject)
{
Client.publish('/DataChannel', {C:(dataObject.A * dataObject.B)};
});
There is a lot more that can be done, but with this basic framework you can stand up server to N client programs that respond to messages from the server. 还有很多事情可以做,但是有了这个基本框架,您就可以将服务器与N个客户机程序站在一起,以响应来自服务器的消息。
You will need to replace 127.0.0.1 with your specific URL and use port numbers and channel names more applicable to your specific application. 您将需要用特定的URL替换127.0.0.1,并使用更适用于您的特定应用程序的端口号和通道名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.