【问题标题】:Socket.IO TypeError: Cannot read property 'broadcast' of undefinedSocket.IO TypeError:无法读取未定义的属性“广播”
【发布时间】:2018-04-02 10:32:37
【问题描述】:

我将 redis 与 socket.io 一起使用:

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。 (C:\socket\htdocs\project\server.js:12:46) 在 emitThree (events.js:135:13) 在 Redis.emit (events.js:216:7) 在 Redis.exports.returnReply ...

怎么了? console.log(message.data.senderID); 打印套接字正常。谢谢..

【问题讨论】:

    标签: jquery node.js laravel socket.io


    【解决方案1】:

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

    还有更多获取参考的方法。只需查看 console.log(io)。

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

    【讨论】:

    • 我使用的是 2.0.3(看起来是最新的)。我从 laravel 发送套接字,而不是 js。 \Event::fire(new NewTopicCreated( $_COOKIE['io'] )); 所以我想从这个套接字( $_COOKIE['io'] )为每个人发送套接字。无论如何?谢谢!
    • 你可以试试这个。 io.to(message.data.senderID).emit(channel+":"+message.event,message.data.message)
    • 只是io.emit 工作,但它发送给发件人。 io.to 不工作.. 任何错误,但不发送。我应该移动到 1.x.x 版本吗?我需要 io.from(..).to 除了发件人以外的所有人。
    • @DisplayName:试试这个io.sockets.sockets[ID].broadcast.emit。它对我有用
    • 你用的是什么版本? TypeError: Cannot read property 'broadcast' of undefined 反正:(
    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2014-10-10
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多