Socket.IO TypeError:无法读取未定义的属性“广播”

我用socket.io使用redis:

var io = require('socket.io')(6001); var Redis = require('ioredis'), var redis = new Redis(); redis.psubscribe('*'); redis.on('pmessage', function (pattern, channel, message) { message = JSON.parse(message); io.sockets.connected[message.data.senderID].broadcast.emit(channel + ':' + message.event, message.data); }); 

我收到这个:

TypeError:无法在Redis中读取未定义的属性“广播”。 (Redis.exports.returnReply)上的Redis.emit(events.js:216:7)处的emitThree(events.js:135:13)处的C:\ socket \ htdocs \ project \ server.js:12:46。 。

怎么了? console.log(message.data.senderID); 打印插口确定。 谢谢..

首先,请检查您的socket.io版本。 io.sockets.connected[socketid]在socket.io 1.xx中支持

有更多的方法来获得参考。 只要看看console.log(io)。

在2.x中,您可以使用io.sockets.sockets[senserID].broadcast.emit 。 //发送给除发件人以外的所有人Refs this one