我是 node.js 和 socket.io 的新手
我有两个客户端,它们的 ID 为 1 和 2。我需要通过 socket.io 发送数据,其中包含:user_from、user_to、action
并且数据是从双方发送的。
如何使用 socket.emit() 从客户端 1 发送这些数据,以便设置变量:数据?
如何使用 socket.get 在服务器上读取该数据?
并再次从服务器发送到客户端 2 ?(我找到了这段代码:io.sockets.volatile.emit('broadcast_msg', msg); 但我只想将它从特定用户发送到特定用户,而不是所有连接的用户。)
谢谢你的帮助,我看到很多例子,但不是我需要的。
更新:
我不明白这段代码:
// on server started we can load our client.html page
function handler(req, res) {
fs.readFile(__dirname + '/client.html', function(err, data) {
if(err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
为什么我需要这样做?我有 ex: index.php?menu=30 或 index.php?menu=30&action=12