gpt4 book ai didi

node.js - Socket.io 服务器到服务器(发送消息到特定服务器)

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:18 24 4
gpt4 key购买 nike

我有一个具体情况。我有连接到服务器 A 的客户端(浏览器)。服务器 A 连接到服务器 B 和 C。当客户端向服务器 A 发送消息时,我需要将消息发送到特定服务器,这意味着发送到服务器 B 或 C 之一,而不是两个都。一般来说,我需要服务器之间分开的通信,但我只有广播通信。从服务器 A 到另一台服务器的消息在所有连接的服务器上都看不到。

如何将消息直接发送到其中一台服务器?这是我的服务器代码片段。我只是更改每个服务器的端口并连接它们。

// Load requirements
var PORT = process.env.PORT || 8080;//just change port for other servers
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);

app.use(express.static(__dirname + '/public'));

var client = require('socket.io-client');

var socket1 = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C

// Add a connect listener
io.on('connection', function (socket) {
console.log('Client connected.');
//when server receive message from client
socket.on('message_from_browser', function (message) {
console.log("Message from browser broadcasted: " + message.text);
var updated_message = {
text: message.text,
port: PORT
};

// send message to server B
socket1.emit('server_message', updated_message);//send message to server B
});

// Disconnect listener
socket.on('disconnect', function () {
console.log('Client disconnected.');
});
});

socket1.on('connect', function () {
socket1.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
//change server name
console.log('Connected to server B!');
});

socket2.on('connect', function () {
socket2.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
//change server name
console.log('Connected to server C!');
});

http.listen(PORT, function (req, res) {
console.log("Server Started on port: " + PORT);
});

最佳答案

您与变量名 socket 存在命名冲突。它既被定义为与服务器 B 的连接,又被定义为 .on('connection', function(socket) {...}) 回调中的参数。

要解决这个问题,请更改以下内容:

var socket = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C

对此:

var socket1 = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C

然后,将服务器 B 连接引用为 socket1,而不是 socket

名为 socket 的变量已在此定义为参数,因此当外部 socket 变量具有冲突名称时,您无法访问该变量:

// Add a connect listener
io.on('connection', function (socket) {
// socket variable name ^^^^^^
console.log('Client connected.');
//when server receive message from client
socket.on('message_from_browser', function (message) {
console.log("Message from browser broadcasted: " + message.text);
var updated_message = {
text: message.text,
port: PORT
};

// send message to server B
socket1.emit('server_message', updated_message);//send message to server B
});

// Disconnect listener
socket.on('disconnect', function () {
console.log('Client disconnected.');
});
});

并且没有传入服务器消息的监听器,因此您需要为 socket1socket2 添加监听器。

io.on('connect', function(socket1){
socket1.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
});

io.on('connect', function(socket2){
socket2.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
});

关于node.js - Socket.io 服务器到服务器(发送消息到特定服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36107493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com