问题描述
我要在我的socket.io聊天应用程序的Redis存储中存储用户名/套接字ID对.
I am storing username/SocketID pairs in redis store for my socket.io chat application.
当用户断开连接时,我需要从Redis存储中删除用户名/socketID对.我已经看到了如何从键中获取值,但从未从值中获取键.是否有可能?或无论哪种方式,我怎么能只从值中删除键/值对.这是我的代码
When the user disconnects i need to remove the username/socketID pair from the redis store. I have seen how to get the value from a key but never a key from a value. Is it possible? or either way how can i delete the key/value pair from just the value. Here's my code
用于添加以存储在connect上
For adding to store on connect
socket.on('username', function (username) {
client.set(username, socket.id, function (err) {
console.log(username + ":" + socket.id);
});
});
对于断开连接,客户端将不知道断开连接的时间,这可能是由于Internet连接丢失而引起的,但是当套接字断开连接时,客户端总是会遇到断开连接"事件.在这种情况下,我需要删除用户名/socketID对.
For disconnect, the client wont know when the disconnect will happen, might happen due to loss of internet connectivity but when the socket disconnects it always hits the "disconnect" event. In this event i need to delete the username/socketID pair.
socket.on('disconnect', function () {
// dont know the username??
client.del(username, socket.id, function (err) {
if (err)
console.log(err);
else {
socket.disconnect();
console.log(socket.id + " DISCONNECTED");
}
});
});
推荐答案
最简单的方法是存储两个对.一个带有username/id
,另一个带有id/username
.因此,无论您拥有什么信息,都可以获取另一个,因此也可以获取另一个键/值对.
The easiest way is to store two pairs. One with username/id
and one with id/username
. So whatever information you have, you can get the other and as a result the other key/value pair too.
这篇关于如何从Redis Store Client中的键/值对中的值获取键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!