[英]NodeJS reply to connected user by SocketID
in this below simple code i want to try after get connected user on nodeJs i can be reply any message after send pm
from android device, in this code how to send message for connected user by socket? 在下面的简单代码中,我想在nodeJs上获得连接用户后尝试尝试从Android设备发送pm
后可以回复任何消息,在此代码中如何通过套接字为连接的用户发送消息?
var io = require('socket.io');
var server = io.listen(4732);
var android_socket = undefined;
server.sockets.on('connection', function(socket) {
socket.on('user', function(data) {
if (data.type == "client")
//saving socket
android_socket = socket;
}
});
socket.on("pm", function(data) {
/* Reply message to android_socket */
});
});
console.log('server is connected...');
You can simply emit
an event to the same socket that received the pm
event: 你可以简单地emit
一个事件给收到同一插座pm
事件:
socket.on("pm", function(data) {
/* send reply event */
socket.emit('foo', {data: 123});
});
That socket is attached to an android client. 该套接字已附加到android客户端。
There is no need to store any android_socket
references. 无需存储任何android_socket
引用。 In fact, your approach (which stores a single socket reference) would not have worked properly anyway, since there could be multiple android socket connections open at the same time. 实际上,您的方法(存储单个套接字引用)无论如何都无法正常工作,因为可能会同时打开多个android套接字连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.