}
function writeMessage(responseAndType, data, terminate) {
+ if (!responseAndType[0]) {
+ return;
+ }
+
if (responseAndType[1] === REQUEST_TYPE_WEBSOCKET) {
webSocketWrite(responseAndType[0], data);
} else {
}
}
- console.log("Stopped", gamesCount, "games and", listeningPlayerCount, "player connections.");
+ console.log(
+ "Stopped", gamesCount, "game" + (gamesCount === 1 ? "" : "s"),
+ "and", listeningPlayerCount, "player connection" + (listeningPlayerCount === 1 ? "" : "s") +
+ "."
+ );
+
server.close(saveGames);
});
}
closed = true;
stopKeepAlive(responseAndType);
+ responseAndType[0] = null;
const index = that.listeningPlayers.indexOf(responseAndType);
if (index !== -1) {
// See https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
server.on("upgrade", function upgradeToWebsocket(request, socket) {
+ const responseAndType = [socket, REQUEST_TYPE_WEBSOCKET];
+
+ function closeSocket() {
+ responseAndType[0] = null;
+ }
+
socket.on("error", function (e) {
- console.error("Socket error", e);
+ console.error("Socket error", e.toString());
+ closeSocket();
});
- // socket.on("close", function () {});
+ socket.on("close", closeSocket);
const webSocketMatch = request.url.match(/\/(?::[^/]+\/)?ws\/(?<data>[\s\S]+)$/u);
debuglog("WS: established " + decodedData);
- const responseAndType = [socket, REQUEST_TYPE_WEBSOCKET];
-
let receivedBytes = null;
socket.on("data", function webSocketData(buffer) {
const POLLING_DELAY = 2000;
const ENABLE_WEBSOCKETS = true;
const ENABLE_EVENT_SOURCE = true;
- const MAX_WEBSOCKET_ERRORS = 2;
+ const MAX_WEBSOCKET_ERRORS = 1;
const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;};