Socket.io服务器到服务器(向特定服务器发送消息)

时间:2016-03-19 21:37:22

标签: node.js express socket.io

我有具体情况。我有连接到服务器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);
});

2 个答案:

答案 0 :(得分:2)

您与变量名称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);
    });
});

答案 1 :(得分:-1)

使用一个变量和配置来跟踪其他主机,请尝试:

更新以修复一些错误:

_head.haml