fix reconnect logic

isekai-main
Barış Soner Uşaklı 2 years ago
parent 9044e10e64
commit 5a42d37e0f

@ -191,7 +191,6 @@ app = window.app || {};
} }
function onReconnecting() { function onReconnecting() {
reconnecting = true;
const reconnectEl = $('#reconnect'); const reconnectEl = $('#reconnect');
const reconnectAlert = $('#reconnect-alert'); const reconnectAlert = $('#reconnect-alert');
@ -207,8 +206,9 @@ app = window.app || {};
} }
function onDisconnect() { function onDisconnect() {
reconnecting = true;
setTimeout(function () { setTimeout(function () {
if (socket.disconnected) { if (!socket.connected) {
onReconnecting(); onReconnecting();
} }
}, 2000); }, 2000);

@ -1,61 +1,27 @@
'use strict'; 'use strict';
const os = require('os');
const nconf = require('nconf');
const topics = require('../../topics'); const topics = require('../../topics');
const pubsub = require('../../pubsub'); const io = require('..');
const utils = require('../../utils');
const stats = {};
const totals = {}; const totals = {};
const SocketRooms = module.exports; const SocketRooms = module.exports;
SocketRooms.stats = stats;
SocketRooms.totals = totals; SocketRooms.totals = totals;
pubsub.on('sync:stats:start', () => { SocketRooms.getTotalGuestCount = async function () {
const stats = SocketRooms.getLocalStats(); const s = await io.in('online_guests').fetchSockets();
pubsub.publish('sync:stats:end', { return s.length;
stats: stats,
id: `${os.hostname()}:${nconf.get('port')}`,
});
});
pubsub.on('sync:stats:end', (data) => {
stats[data.id] = data.stats;
});
pubsub.on('sync:stats:guests', (eventId) => {
const Sockets = require('../index');
const guestCount = Sockets.getCountInRoom('online_guests');
pubsub.publish(eventId, guestCount);
});
SocketRooms.getTotalGuestCount = function (callback) {
let count = 0;
const eventId = `sync:stats:guests:end:${utils.generateUUID()}`;
pubsub.on(eventId, (guestCount) => {
count += guestCount;
});
pubsub.publish('sync:stats:guests', eventId);
setTimeout(() => {
pubsub.removeAllListeners(eventId);
callback(null, count);
}, 100);
}; };
SocketRooms.getAll = async function () { SocketRooms.getAll = async function () {
pubsub.publish('sync:stats:start'); const sockets = await io.server.fetchSockets();
totals.onlineGuestCount = 0; totals.onlineGuestCount = 0;
totals.onlineRegisteredCount = 0; totals.onlineRegisteredCount = 0;
totals.socketCount = 0; totals.socketCount = sockets.length;
totals.topics = {}; totals.topics = {};
totals.topTenTopics = [];
totals.users = { totals.users = {
categories: 0, categories: 0,
recent: 0, recent: 0,
@ -63,30 +29,39 @@ SocketRooms.getAll = async function () {
topics: 0, topics: 0,
category: 0, category: 0,
}; };
const userRooms = {};
for (const instance of Object.values(stats)) { const topicData = {};
totals.onlineGuestCount += instance.onlineGuestCount; for (const s of sockets) {
totals.onlineRegisteredCount += instance.onlineRegisteredCount; for (const key of s.rooms) {
totals.socketCount += instance.socketCount; if (key === 'online_guests') {
totals.users.categories += instance.users.categories; totals.onlineGuestCount += 1;
totals.users.recent += instance.users.recent; } else if (key === 'categories') {
totals.users.unread += instance.users.unread; totals.users.categories += 1;
totals.users.topics += instance.users.topics; } else if (key === 'recent_topics') {
totals.users.category += instance.users.category; totals.users.recent += 1;
} else if (key === 'unread_topics') {
instance.topics.forEach((topic) => { totals.users.unread += 1;
totals.topics[topic.tid] = totals.topics[topic.tid] || { count: 0, tid: topic.tid }; } else if (key.startsWith('uid_')) {
totals.topics[topic.tid].count += topic.count; userRooms[key] = 1;
}); } else if (key.startsWith('category_')) {
totals.users.category += 1;
} else {
const tid = key.match(/^topic_(\d+)/);
if (tid) {
totals.users.topics += 1;
topicData[tid[1]] = topicData[tid[1]] || { count: 0 };
topicData[tid[1]].count += 1;
}
} }
}
}
totals.onlineRegisteredCount = Object.keys(userRooms).length;
let topTenTopics = []; let topTenTopics = [];
Object.keys(totals.topics).forEach((tid) => { Object.keys(topicData).forEach((tid) => {
topTenTopics.push({ tid: tid, count: totals.topics[tid].count || 0 }); topTenTopics.push({ tid: tid, count: topicData[tid].count });
}); });
topTenTopics = topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); topTenTopics = topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10);
const topTenTids = topTenTopics.map(topic => topic.tid); const topTenTids = topTenTopics.map(topic => topic.tid);
const titles = await topics.getTopicsFields(topTenTids, ['title']); const titles = await topics.getTopicsFields(topTenTids, ['title']);
@ -94,6 +69,7 @@ SocketRooms.getAll = async function () {
topic.title = titles[index].title; topic.title = titles[index].title;
return topic; return topic;
}); });
return totals; return totals;
}; };

Loading…
Cancel
Save