Внедренное приложение представляет собой чат-сервер, использующий сокеты io и redis, чтобы пометить пользователя как подключенного к сети с помощью хэш-ключа и количества подключенных пользователей как int.Хеш выглядит следующим образом:
userID <- hash key
total_connected <- Integer (Integer incremented on a connection from the same user id)
status <- "online"/"offline" (If the total_connected is 0 then offline otherwise online)
Параметры подключения клиента к серверу сокета io:
secure = true
transports = WebSocket
reconnection = true
forceNew = true
Параметры подключения к серверу
pingInterval: 5000,
pingTimeout: 2000,
Логика счетчика приращения total_connected записывается в событие отключения объекта сокета.Проблема в том, что счетчик total_connected в шатком интернет-соединении продолжает увеличиваться, не уменьшаясь. Код сервера выглядит следующим образом.
io.on('connection', async function(socket){
// Server emits initialization
socket.emit('initialization');
// This is to make sure we have a stable internet connection before
// Client emits initializationAcknowledged
socket.on('initializationAcknowledged', function(){
// Increment the total connected count on Redis by 1 on the user hash.
redisClient.watch(socket.userID, function(err) {
if (err) {
console.log('There was an error while watching the conversation with id', conversations.id);
}
redisClient.multi().hincrby(socket.userID, helper.count, 1).exec(function(err, result) {
if (err) {
console.log('There was an error while executing the multi.');
}
if (result === null) {
console.log('The transaction was not performed since the data was modified by someone else during the transaction and changed.');
}
});
});
});
socket.on('disconnect', function(){
// Decrement on redis
redisClient.watch(socket.userID, function(err) {
if (err) {
console.log('There was an error while watching the conversation with id', conversations[0].id);
}
redisClient.multi().hincrby(socket.userID, helper.count, -1).hset(socket.userID, helper.watching, JSON.stringify([])).exec(function(err, result) {
if (err) {
console.log('There was an error while executing the multi');
}
if (result === null) {
console.log('The transaction was not performed since the data was modified by someone else during the transaction and changed.');
}
});
});
});
Любая помощь приветствуется.