是否可以同时从节点/套接字向2个客户端发送数据?

时间:2016-06-24 12:06:51

标签: angularjs node.js socket.io

当客户端连接时,我使用emit来发送大量数据〜1mb。出于某种原因,我预计不会有多少客户同时这样做,但现在我发现我可能错了。似乎第二个客户端必须等到第一个客户端收到所有数据才开始接收他们的数据。这是对的吗?有什么方法吗?

var moment = require('moment');

var connect = require('connect'),
    socketio = require('socket.io'),
    mysql = require('mysql'),
    connectionsArray    = [],
    connection          = mysql.createPool({
        connectionLimit : 50,
        host        : '127.0.0.1',
        user        : 'root',
        password    : '',
        database    : 'hideout',
        port        : 3306
    }),
    POLLING_INTERVAL = 500,
    pollingTimer;

var port = process.env.PORT || 3000;
var server = connect(
  connect.static(__dirname + '/public')
).listen(port);

var io = socketio.listen(server);

function initializeDatabase(){
    orders=[];
    var query2 = connection.query('SELECT open_orders.*,categories.`parent` as parent FROM `open_orders` LEFT JOIN categories ON categories.`id`=open_orders.`category` WHERE `item_parent` IS NULL AND `void` IS NULL ORDER BY `table`,`timestamp`');
    query2
    .on('error', function(err) {
        console.log( err );
        updateSocket( err );

    })
    .on('result', function( order ) {
        var theDatetime = moment(order.datetime).format();
        var order_item = {
            "id":order.id,
            "cover":order.cover,
            "table_number":order.table,
            "seat":order.seat,
            "name":order.name,
            "price":order.price,
            "item_parent":order.item_parent,
            "category":order.category,
            "non_taxable":order.non_taxable,
            "parent":order.parent,
            "datetime":theDatetime,
            "op_pin":order.op_pin,
            "timestamp":order.timestamp,
            "menu_id":order.menu_id,
            "mod_group_id":order.mod_group_id,
            "mod_required":order.mod_required,
            "check":order.check,
            "split_parent":order.split_parent,
            "sent":order.sent,
            "complete":order.complete,
            "mods":[]
        };
        orders.push(order_item);
    })
    .on('end',function(){
    });
}
initializeDatabase();

function ordersView(socket){
    updateSocket(socket, {orders:orders});
}

io.sockets.on( 'connection', function ( socket ) {
    console.log('Number of connections:' + connectionsArray.length);

    socket.on('tablesView', function(){
        ordersView(socket);
    });

    socket.on('disconnect', function () {
        var socketIndex = connectionsArray.indexOf( socket );
        console.log('socket = ' + socketIndex + ' disconnected');
        if (socketIndex >= 0) {
            connectionsArray.splice( socketIndex, 1 );
        }
    });

    console.log( 'A new socket is connected!' );
    connectionsArray.push( socket );
});

var updateSocket = function ( socket, data ) {
    data.time = moment(new Date()).format();
    socket.emit( 'notification' , data );
};

var updateSockets = function ( socket, data ) {
    data.time = moment(new Date()).format();
    connectionsArray.forEach(function( tmpSocket ){
        if(tmpSocket!==socket){
            tmpSocket.volatile.emit( 'notification' , data );
        }
    });
};

0 个答案:

没有答案