node.js 发送到 Socket IO socket.id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19333922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Emit to Socket IO socket.id
提问by Matthew Starkey
I am trying to emit to a particular socket ID:
我正在尝试发送到特定的套接字 ID:
socket(user[playID]).emit('correct', data);
But I'm getting:
但我得到:
TypeError: object is not a function
if I log out user[playID]I do get a valid socket ID.
如果我注销,user[playID]我会得到一个有效的套接字 ID。
Appreciated!
赞赏!
Here is my setup in case I'm missing something:.
这是我的设置,以防万一我遗漏了什么:。
// Tell Socket.io to pay attention
servio = io.listen(server);
// Tell HTTP Server to begin listening for connections on port 3250
sock = server.listen(3250);
回答by Jaz Lalli
This should do it
这应该做
servio.sockets.socket(id).emit('hello');
This answercovers the same/similar topic. In short, consider keeping a reference to the connected clients yourself and emit to them as desired, rather than relying on socket.io's internals, which could change.
这个答案涵盖了相同/相似的主题。简而言之,考虑自己保留对连接客户端的引用并根据需要向它们发出,而不是依赖 socket.io 的内部结构,这可能会改变。
回答by user2755758
Per http://socket.io/docs/rooms-and-namespaces/,
每个http://socket.io/docs/rooms-and-namespaces/,
Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id.
Socket.IO 中的每个 Socket 都由一个随机的、不可猜测的、唯一的标识符 Socket#id 标识。为方便起见,每个套接字都会自动加入由该 ID 标识的房间。
You emit to a room like:
你发射到一个房间,如:
io.to('some room').emit('some event')
If you want to emit to just a specific socket.id, just replace 'some room' with the associated id.
如果您只想发送到特定的 socket.id,只需将 'some room' 替换为关联的 id。
回答by Alex G
UPDATE: in socket.io-1.4 you have to prepend "/#" to socket id ( very frustrating that it doesnt work now). you can also view all connected sockets from backend as io.sockets.connected
更新:在 socket.io-1.4 中,你必须在 socket id 前面加上“/#”(非常令人沮丧的是它现在不起作用)。您还可以从后端查看所有连接的套接字作为 io.sockets.connected
io.to( "/#" + socket_id).emit("event_name",{data:true})
回答by alditis
Another way to do this is:
另一种方法是:
var players = [];
io.sockets.on('connection', function(socket){
socket.on('skt_init', function (data) {
var player = new Object();
player.id = data.id;
player.socket = socket.id;
players.push(player);
});
socket.on('disconnect', function() {
var len = 0;
for(var i=0, len=players.length; i<len; ++i ) {
var p = players[i];
if(p.socket == socket.id){
players.splice(i,1);
break;
}
}
});
socket.on('skt_event', function(data, id_player){
var len = 0;
for(var i=0, len=players.length; i<len; ++i ) {
var p = players[i];
if(p.id == id_player){
io.sockets.socket(p.socket).emit('correct', data);
break;
}
}
});
Hope that helps somewhat.
希望有所帮助。
回答by robertklep
This should work:
这应该有效:
servio.sockets.sockets[playId].emit(...)
回答by 1j01
Since socket.io doesn't provide a stable API to get the socket from a socket's ID, you can simply (and easily) keep track of them with an object keyed by socket IDs.
由于 socket.io 不提供稳定的 API 来从套接字的 ID 获取套接字,因此您可以简单地(并且轻松地)使用由套接字 ID 键控的对象来跟踪它们。
sockets_by_id = {}
io.on "connection", (socket)->
sockets_by_id[socket.id] = socket
sockets_by_id[socket_id].emit event, data...
(^CoffeeScript^)
(^CoffeeScript^)

