发布:2023/11/2 22:57:10作者:管理员 来源:本站 浏览次数:621
封装Redis发布订阅时,SUB时,又想探测具体Channel的状态,于是执行PUBSUB CHNNALES命令,报
ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context 的错误。
原因:
发布订阅模式下不能共用一个TCP连接,也不能在其上面执行其他命令 only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed。
解决方法
1、修改Redis底层源码
2、重开一个连接,进行其他操作,加个Redis连接池。
client.on("connect", function () {
console.log("redis连接成功");
client.subscribe("channel");
});
client.on("error", function (err) {
console.log("Error " + err);
});
// 1 键值对
client.set("color", "red", redis.print);
client.hset('user:1','name','ejk5', redis.print);
client.hset('user:2','name','ejk6', redis.print);
client.hmset('android:1','Id','45645645456','IsOnline','在线','CreateTime','2023-11-02 22:35:00','UpdateTime','2023-11-02 22:35:00','SendType','[1,2]');
client.hmset('android:2','Id','797897897898','IsOnline','在线','CreateTime','2023-11-02 22:35:00','UpdateTime','2023-11-02 22:35:00','SendType','[1,2]');
client.hmset('android:797854','Id','797854','IsOnline','在线','CreateTime','2023-11-02 22:35:00','UpdateTime','2023-11-02 22:35:00','SendType','[1,2]');
client.hgetall("android:797854",function(err,reply) {
console.log(err);
console.log(reply);
client1.hmset('android:797854','Id','797854','IsOnline','离线','CreateTime','2023-11-02 22:35:00','UpdateTime','2023-11-02 22:35:00','SendType','[1,2]');
});
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4