gpt4 book ai didi

javascript - nodeJS - ioredis NPM 模块 - 订户事件有问题

转载 作者:可可西里 更新时间:2023-11-01 11:23:45 27 4
gpt4 key购买 nike

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') 事件似乎没有触发。

最佳答案

像这样尝试:

sub.subscribe(channel, (error, count) => {
if (error) {
throw new Error(error);
}
console.log(`Subscribed to ${count} channel. Listening for updates on the ${channel} channel.`);
});

关于javascript - nodeJS - ioredis NPM 模块 - 订户事件有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com