3
我正在開發一個使用Node.js,Socket,io,Redis的實時應用程序。我嘗試從工作人員向一個客戶端發送私人消息,但它不起作用。陣列io.sockets.connected工作人員是空的,我不能引用客戶端。在主如何從socket.io-redis發送私人消息(發出)
代碼:
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');
我在做什麼錯了:對工人
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) {});
});
碼?
我找到了出路: 在Socket.IO每個插座是由隨機的,不可猜測,唯一標識符插槽#id標識。爲了您的方便,每個套接字都會自動加入由此ID標識的房間。 工作人員代碼:io.to(id).emit('聊天消息','從員工測試私人消息'); –