使用 POST 的 Electron NodeJS 服务器到服务器通信
Electron NodeJS server to server communication using POST
我正在为学校做作业,我决定使用 Electron 和 NodeJS 制作一个聊天应用程序。所有的 GUI 都是编程的,除了服务器端的东西。我的计划是有两台服务器,每台服务器都充当自己的客户端和服务器,仅相互通信以发送消息。
如何让每个服务器使用 POST 请求进行通信?有人知道可用于此的任何功能齐全的 npm 模块吗?
这可以用 React js
来完成,github 上有很多例子。
看看这个例子:
https://github.com/ncuillery/react-chat-project
https://github.com/keithyong/chat-room
很高兴看到有人使用 Electron,我刚刚用它完成了我的第一个项目,我很惊讶。
正如@Arcath 所说,您必须使用 socket.io
,它在前端和后端之间进行对话。每当有人发送聊天消息时,React.js 都会处理该消息,并发出服务器接收的套接字消息。服务器然后将套接字消息添加到数据库中。
您需要在服务器 A 中使用:socket.io
在服务器 B 中:socket.io-client
像这样:
server A
// Load requirements
var http = require('http'),
io = require('socket.io');
// Create server & socket
var server = http.createServer(function(req, res)
{
// Send HTML headers and message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>404</h1>');
});
server.listen(8080);
io = io.listen(server);
// Add a connect listener
io.sockets.on('connection', function(socket)
{
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
server B
// Connect to server
var io = require('socket.io-client');
var socket = io.connect('http://localhost:8080', {reconnect: true});
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
我正在为学校做作业,我决定使用 Electron 和 NodeJS 制作一个聊天应用程序。所有的 GUI 都是编程的,除了服务器端的东西。我的计划是有两台服务器,每台服务器都充当自己的客户端和服务器,仅相互通信以发送消息。
如何让每个服务器使用 POST 请求进行通信?有人知道可用于此的任何功能齐全的 npm 模块吗?
这可以用 React js
来完成,github 上有很多例子。
看看这个例子:
https://github.com/ncuillery/react-chat-project
https://github.com/keithyong/chat-room
很高兴看到有人使用 Electron,我刚刚用它完成了我的第一个项目,我很惊讶。
正如@Arcath 所说,您必须使用 socket.io
,它在前端和后端之间进行对话。每当有人发送聊天消息时,React.js 都会处理该消息,并发出服务器接收的套接字消息。服务器然后将套接字消息添加到数据库中。
您需要在服务器 A 中使用:socket.io
在服务器 B 中:socket.io-client
像这样:
server A
// Load requirements
var http = require('http'),
io = require('socket.io');
// Create server & socket
var server = http.createServer(function(req, res)
{
// Send HTML headers and message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>404</h1>');
});
server.listen(8080);
io = io.listen(server);
// Add a connect listener
io.sockets.on('connection', function(socket)
{
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
server B
// Connect to server
var io = require('socket.io-client');
var socket = io.connect('http://localhost:8080', {reconnect: true});
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});