如何在不同服务器上相互通信的2个节点应用程序?

时间:2022-05-10 14:03:57

Basically I have the middle-man webpage which is a website that people can go to, login and create servers (for a game) in different locations, e.g. Texas, New York, Chicago.

基本上我有中间人网页,这是一个人们可以去不同地点登录和创建服务器(用于游戏)的网站,例如:德克萨斯州,纽约,芝加哥。

Right now I have my main app running on New York

现在我的主应用程序在纽约运行

I want to have a sub-app that runs on those servers which is solely responsible for creating child_processes to start servers.

我希望有一个在这些服务器上运行的子应用程序,它独自负责创建启动服务器的child_processes。

I was playing around with having the sub-apps run off of get/post requests, but trying to send get/post requests from the Node side is just painful, and trying to secure it so other people don't just CURL a request and do something malicious is there a way to maybe do this with sockets or something so that the sub-apps connect to the main socket of the main web-app and then can send out a start emit or something to the sub-apps to create a server? Sorry if this doesn't make sense I'm fairly new to having apps talk to each other.

我正在玩子应用程序运行get / post请求,但尝试从Node端发送get / post请求只是痛苦,并试图保护它,以便其他人不只是CURL请求和做一些恶意的东西有可能通过套接字或其他方式做到这一点,以便子应用程序连接到主web应用程序的主套接字,然后可以发送一个启动发射或东西到子应用程序创建一个服务器?对不起,如果这没有意义,我相当新,让应用程序互相交流。

2 个解决方案

#1


3  

you can make your two node servers communicate with the websocket node module 'ws'. you can install it by typing this command:

您可以使两个节点服务器与websocket节点模块“ws”进行通信。您可以通过键入以下命令来安装它:

npm install --save ws

To connect your node apps, one node app should be listening on a port (for exapmle: 3001), which make it acting as a server for the other node app :

要连接节点应用程序,一个节点应用程序应该在端口上侦听(例如:3001),这使得它充当其他节点应用程序的服务器:

var webSocketServer = require('ws').Server,
wss = new WebSocketServer ({port: 3001, path: '/path'});
wss.on('listening', function() {
    console.info('ws server is listening')
})

And then you can connect the other node app like this:

然后您可以像这样连接其他节点应用程序:

var WebSocket = require('ws');
var ws  = new WebSocket('ws://www.yourServerDomain.com:3001/path');

now you can send and receive data from both sides, you can see in this link to see how to do it.

现在您可以从双方发送和接收数据,您可以在此链接中看到如何执行此操作。

#2


1  

I would suggest you to use Message Queues

我建议你使用消息队列

At the simplest level, a queue messages is a way for applications and discrete components to send messages between one another in order to reliably communicate.

在最简单的层面上,队列消息是应用程序和分立组件在彼此之间发送消息以便可靠地通信的一种方式。

And in NodeJS you can realize queue jobs with kue library

在NodeJS中,您可以使用kue库实现队列作业

#1


3  

you can make your two node servers communicate with the websocket node module 'ws'. you can install it by typing this command:

您可以使两个节点服务器与websocket节点模块“ws”进行通信。您可以通过键入以下命令来安装它:

npm install --save ws

To connect your node apps, one node app should be listening on a port (for exapmle: 3001), which make it acting as a server for the other node app :

要连接节点应用程序,一个节点应用程序应该在端口上侦听(例如:3001),这使得它充当其他节点应用程序的服务器:

var webSocketServer = require('ws').Server,
wss = new WebSocketServer ({port: 3001, path: '/path'});
wss.on('listening', function() {
    console.info('ws server is listening')
})

And then you can connect the other node app like this:

然后您可以像这样连接其他节点应用程序:

var WebSocket = require('ws');
var ws  = new WebSocket('ws://www.yourServerDomain.com:3001/path');

now you can send and receive data from both sides, you can see in this link to see how to do it.

现在您可以从双方发送和接收数据,您可以在此链接中看到如何执行此操作。

#2


1  

I would suggest you to use Message Queues

我建议你使用消息队列

At the simplest level, a queue messages is a way for applications and discrete components to send messages between one another in order to reliably communicate.

在最简单的层面上,队列消息是应用程序和分立组件在彼此之间发送消息以便可靠地通信的一种方式。

And in NodeJS you can realize queue jobs with kue library

在NodeJS中,您可以使用kue库实现队列作业