【问题标题】:Node.JS and Socket.io - Broadcast returns to senderNode.JS 和 Socket.io - 广播返回给发送者
【发布时间】:2012-11-05 13:12:24
【问题描述】:

我正在构建一个具有多个客户端和一个 node.js 服务器的实时 Web 应用程序来处理来自客户端的状态和广播事件更改。我已经实现了 Socket.io 作为客户端和服务器之间的传输机制。当客户端执行特定操作时,将向服务器发送一个事件,该事件被广播到网络上的其他客户端。

但是,有时当服务器接收到一系列事件时,服务器不会广播它,而是将其发送回发送客户端 - 或将其发送到所有连接的套接字,包括发送客户端。

这是一个服务端实现的 sn-p,它基本上监听客户端事件,将它们收集到一个数组中,每 2 秒广播一次。

如果你愿意,我可以从客户端实现中包含相关的 sn-ps。

// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io'), users = require('./users');

// Commands to client
var USER_LOGIN = 0,
    USER_LIST = 1,
    USER_CONNECTED = 2,
    USER_DISCONNECTED = 3,
    COMMAND = 4,
    COMMAND_SETNICKNAME = 0,
    COMMAND_POSITION = 1,
    COMMAND_MOUSEDOWN = 2,

var array = [];
var activeUsers = {};


// Start the server at port 8080
var server = http.createServer(function(req, res){ 
    res.writeHead(200,{ 'Content-Type': 'text/html' }); 
});
server.listen(8080);

// Create a Socket.IO instance, passing it our server
var io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(client){ 
    console.log("started");


    // Create periodical which ends a JSON message to the client every 2 seconds
    var interval = setInterval(function() {
        if(array.length >0) {
            console.log("Sending to clients " + array);
        //client.send(JSON.stringify(array));
        client.broadcast.send(JSON.stringify(array));
        array = [];
    }
},2000);

// Success!  Now listen to messages to be received
client.on('message',function(event){ 
        console.log("--Reading event " + event);
        var eventArray = JSON.parse(event);
    for(var i = 0; i < eventArray.length; i++) {
        var dataArray = eventArray[i];
        var userId = parseInt(dataArray[0]);
        var position = 1;

        console.log("current element in array " + dataArray);

        switch (parseInt(dataArray[position++])) {
            case USER_LOGIN: 
                // Log in user with USERID. 
                var username = dataArray[position++];
                console.log("Attempting to log in with user " + username);

                // If valid, send USER_LOGIN broadcast to clients
                if(login(username)) {
                    array.push([0, USER_LOGIN, login(username)]);
                }
                break;

【问题讨论】:

    标签: node.js socket.io broadcast


    【解决方案1】:
    io.sockets.on('connection', function(client){ 
        console.log("started");
    
    
        // Create periodical which ends a JSON message to the client every 2 seconds
        var interval = setInterval(function() {
            if(array.length >0) {
                console.log("Sending to clients " + array);
            //client.send(JSON.stringify(array));
            client.broadcast.send(JSON.stringify(array));
            array = [];
        }
    },2000);
    

    您正在为每个连接创建一个调度程序,这将是一团糟。因为您正在 on('connection',
    内部创建间隔 而且我看不到在一段时间内调用客户端有任何意义,因为您有一种方法可以在某些客户端更改事件上推送更改。这就是 socket.io 的优势。您可以像这样从客户端更改事件中广播另一个侦听器中的更改,而不是使用间隔。

    client.on('onSomechangeInClient',function(event){ 
      //update array here..
     client.broadcast.send(JSON.stringify(array));
    });
    

    【讨论】:

    • 谢谢 - 我明白你的观点,即只要更改到来就广播更改而不是使用调度程序。
    猜你喜欢
    • 2013-01-11
    • 2023-02-01
    • 2012-01-04
    • 1970-01-01
    • 2016-09-04
    • 2020-03-12
    • 2014-03-24
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多