如何实现向特定用户发送消息?

时间:2013-11-23 20:59:24

标签: javascript node.js websocket

我想向特定客户端发送消息,但服务器脚本实现向连接ID发送消息,通过阵列上的所有键。

服务器代码:

//sets ID when connecting and adding to array:

var wsServer = new webSocketServer({ httpServer: server });

wsServer.on('request', function(request) {

    console.log((new Date()) + ' ws from origin ' + request.origin + '.');

    var ws = request.accept(null, request.origin); 
    var id = clients.push(ws)-1; 

    console.log((new Date()) + ' ws accepted. ID:' + id); // show ID

//////////


var json = JSON.stringify(res);
for (var i=0; i < clients.length; i++) {
    clients[i].send(json);  // sent to all connected clients
}

我想使用任意user_id向特定用户发送消息 示例:客户端[user_id_from_database] .send(json);

连接时写入数组“my_user_id_from_database”?

0 个答案:

没有答案