PadMessageHandler: Fix stats null dereference

It is possible for the stats to be read before the
`expressCreateServer` hook is called (in particular: when there is an
error during startup), which is when the `socketio` variable is set.
Check for non-null `socketio` before attempting to count the number of
socket.io connections.
pull/5121/head
Richard Hansen 2021-07-19 13:54:39 -04:00
parent 3d80409236
commit c83bb058d1
1 changed files with 2 additions and 6 deletions

View File

@ -41,6 +41,7 @@ const {RateLimiterMemory} = require('rate-limiter-flexible');
const webaccess = require('../hooks/express/webaccess'); const webaccess = require('../hooks/express/webaccess');
let rateLimiter; let rateLimiter;
let socketio = null;
exports.socketio = () => { exports.socketio = () => {
// The rate limiter is created in this hook so that restarting the server resets the limiter. The // The rate limiter is created in this hook so that restarting the server resets the limiter. The
@ -70,7 +71,7 @@ exports.socketio = () => {
const sessioninfos = {}; const sessioninfos = {};
exports.sessioninfos = sessioninfos; exports.sessioninfos = sessioninfos;
stats.gauge('totalUsers', () => Object.keys(socketio.sockets.sockets).length); stats.gauge('totalUsers', () => socketio ? Object.keys(socketio.sockets.sockets).length : 0);
stats.gauge('activePads', () => { stats.gauge('activePads', () => {
const padIds = new Set(); const padIds = new Set();
for (const {padId} of Object.values(sessioninfos)) { for (const {padId} of Object.values(sessioninfos)) {
@ -87,11 +88,6 @@ const padChannels = new channels.channels(
({socket, message}, callback) => nodeify(handleUserChanges(socket, message), callback) ({socket, message}, callback) => nodeify(handleUserChanges(socket, message), callback)
); );
/**
* Saves the Socket class we need to send and receive data from the client
*/
let socketio;
/** /**
* This Method is called by server.js to tell the message handler on which socket it should send * This Method is called by server.js to tell the message handler on which socket it should send
* @param socket_io The Socket * @param socket_io The Socket