S
S
sferos2016-11-24 22:18:28
Redis
sferos, 2016-11-24 22:18:28

How to solve problem with redis?

The bottom line is this: the 1st user creates a game room (game_id) and connects to it, immediately entered into redis

client.rpush("game_id:"+game_id, username);
client.rpush("status:enable", array); //открыть комнату для подключения
.
After that, 2 users (and subsequent ones) are connected to it in the room, it is also entered into the radish client.rpush("game_id:"+game_id, username);.
Through client.lrange, the length of the list of people in the room is found out, after enumeration of names and sending to the client.
client.lrange("game_id:"+game_id, "0", "-1", function(error, result){
for(var i=0; i<result.length;i++{
//Получаем имена
//Успешно отправляем клиенту
}
}

Next, I'm trying to set a limit on the number of people in a room.
if(result.length==8){
client.lrem("status:enable", "0",  array_id); //удалить комнату из "доступных к подключению".
...//основные действия
}
.
But I ran into such a problem that when 8 players connect, nothing happens, the room closes only when the 9th user enters. At the same time, the 9th user himself does not register in the radish (because he entered after the room was closed). I tried to set if(result==7), but then the room closes with 8 users, and the last (8) is missing in redis. I checked through the console, I did not seem to find any errors. those. if there are 8 people in the room, then the list of 8 people is spelled out. But node for some reason wants a new connection to close. The code is massive, if you need additional information, then write.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yeti357, 2016-11-25
@sferos

You don't understand the sequence of client.rpush and client.lrange
If your asynchronous requests are not executed sequentially, it is quite likely that lrange is executed first, and then only rpush.
User 8 came, checked lrange == 7, does not close the room. Next, rpush is executed.
Then the 9th user comes and then lrange becomes 8 and the room closes

V
Vitaly, 2016-11-24
@vitali1995

Why not use socket.io? There is built-in support for authentication with the ability to work with personal data (set / get by string key). In addition, the exchange through sockets is faster than http requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question