gpt4 book ai didi

python - Redis 和套接字仅发送给某些客户端

转载 作者:可可西里 更新时间:2023-11-01 11:00:46 26 4
gpt4 key购买 nike

我在 node.js 中有一个这样的服务器:

socket.on('connection', function(client) {
const subscribe = redis.createClient(6379, '127.0.0.1')

subscribe.psubscribe('kitchen_*');

subscribe.on("pmessage", function(pattern, channel, message) {
client.emit(channel, message);
log('msg', "received from channel #" + channel + " : " + message);
});
});

在客户端我有这样的:

socket.on('kitchen_companyName', function (data) {
console.log('received a message: ', data);
});

现在我只想在客户端接收 kitchen_companyName 的消息,但即使公司名称是 kitchen_hello 我也会收到消息。我正在使用 pyredis 从 python 发布到 redis。

r = redis.StrictRedis(host='localhost', port=6379)
r.publish('kitchen_'+request.user.user_company, message)

最佳答案

您可以尝试添加一个条件来检查 channel 并仅向该 channel 发送,

subscribe.on("pmessage", function(pattern, channel, message) {
if(channel == "kitchen_companyName") {
client.emit(channel, message);
log('msg', "received from channel #" + channel + " : " + message);
}
});

关于python - Redis 和套接字仅发送给某些客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631769/

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