我正在使用 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');
我究竟做错了什么?