0
const Redis = require('ioredis');

const sub = new Redis();
const pub = new Redis();

sub.on('subscribe', (channel, count) => {
    console.log("Subbed to channel: " + channel);
})

sub.on('message', (channel, message) => {
    console.log("Message recieved: " + message);
})

setTimeout(() => {
    console.log("Attempting");
    sub.subscribe("hey");
    pub.publish("hey", "msg");
}, 2000);

这是我一直用来测试的代码,它会产生以下控制台输出:

Attempting

Message recieved: msg

预期输出:

Attempting

Subbed to channel: hey

Message recieved: msg

sub.on('subscribe') 事件似乎没有触发。

4

1 回答 1

0

试试这样:

sub.subscribe(channel, (error, count) => {
    if (error) {
        throw new Error(error);
    }
    console.log(`Subscribed to ${count} channel. Listening for updates on the ${channel} channel.`);
});
于 2019-03-13T06:50:22.637 回答