我正在使用Node.js,Socket,io,Redis开发实时应用程序。我尝试从工作人员向一个客户端发送私人消息,但它不起作用。 worker上的数组 io.sockets.connected 为空,我无法引用客户端。
主人代码:
var io = require('socket.io')(30042); //socket.io(<port>) will create a http server
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
io.on('connection', function (socket) {
io.sockets.connected[socket.id].adapter.remoteJoin(socket.id, 'new_room', function (err) {});
});
工人代码:
var SocketIO = require('socket.io');
var redis = require('socket.io-redis');
io = new SocketIO(); //start redis socket-io
io.adapter(redis({host: 'localhost', port: 6379})); // where your redis server is located
//send to all users in room - works perfectly
io.to('new_room').emit('chat message', 'Test global msg from worker');
//????
//dont work - io.sockets.connected is empty
io.sockets.connected['socket_id_from_master'].emit('chat message', 'Test private msg from worker');
我做错了什么?
答案 0 :(得分:0)
由于您的工作人员对连接的套接字一无所知,您需要从适配器获取它们,在本例中是适配器。
要获取需要调用的连接套接字: io.sockets.adapter.clients