diff --git a/client.js b/client.js index 6a432c8..c89c8bf 100644 --- a/client.js +++ b/client.js @@ -1,12 +1,15 @@ const channelSocket = new WebSocket("ws://localhost:9091/connect"); -var __messengerjs__sessionID = null; -var __messengerjs__imTheChannelOwner = false; -var __messengerjs__myNickname = ""; -var __messengerjs__onloadfunction = null; -var __messengerjs__onloadargs = null; -const __messengerjs__fade = document.createElement("div"); -__messengerjs__fade.style.cssText = "position: absolute;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.75);color:white;text-align:center;font-family:sans-serif;"; -__messengerjs__fade.textContent = "connecting to WebSocket server..."; +const __messengerjs__ = { + "sessionID": null, + "imTheChannelOwner": false, + "myNickname": "", + "onloadfunction": null, + "onloadargs": null, + "fade": null +} +__messengerjs__.fade = document.createElement("div"); +__messengerjs__.fade.style.cssText = "position: absolute;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.75);color:white;text-align:center;font-family:sans-serif;"; +__messengerjs__.fade.textContent = "connecting to WebSocket server..."; const beforeUnloadHandler = function(ev, reason) { Channel_OnAppClose(); @@ -132,7 +135,7 @@ channelSocket.onmessage = function(event) { let args = ParseCommandString(new TextDecoder("utf8").decode(event.data)); switch(args[0]) { case "rename": { - __messengerjs__myNickname = args[3]; + __messengerjs__.myNickname = args[3]; break; } case "connect": { @@ -140,19 +143,19 @@ channelSocket.onmessage = function(event) { case "0": { console.error("channel is full or not avaliable"); // regenerate channel ID if we get 0'd - __messengerjs__sessionID = Math.random().toString(36).split('.')[1].substring(0,8); - history.pushState(null, "", `?channel=${__messengerjs__sessionID}`); - channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__sessionID]))); + __messengerjs__.sessionID = Math.random().toString(36).split('.')[1].substring(0,8); + history.pushState(null, "", `#channel=${__messengerjs__.sessionID}`); + channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__.sessionID]))); break; } case "1": { console.log("joined channel successfully."); - __messengerjs__fade.textContent = "waiting for opponent to join this channel..."; + __messengerjs__.fade.textContent = "waiting for opponent to join this channel..."; window.addEventListener("beforeunload", beforeUnloadHandler); if(args[2] === "1") { - __messengerjs__imTheChannelOwner = false; + __messengerjs__.imTheChannelOwner = false; } else { - __messengerjs__imTheChannelOwner = true; + __messengerjs__.imTheChannelOwner = true; if(window.external.Users.Inviter !== window.external.Users.Me) { // This channel has already been closed. Create a new one. beforeUnloadHandler(null, "channel was empty when expecting host"); @@ -175,33 +178,33 @@ channelSocket.onmessage = function(event) { Channel_OnTypeChanged(); } case "adduser": { - __messengerjs__fade.style.display = "none"; - if(__messengerjs__imTheChannelOwner === false) { - window.external.yellows111_Users[1].Name = __messengerjs__myNickname; + __messengerjs__.fade.style.display = "none"; + if(__messengerjs__.imTheChannelOwner === false) { + window.external.yellows111_Users[1].Name = __messengerjs__.myNickname; window.external.yellows111_Users[0].Name = args[2]; } else { window.external.yellows111_Users[1].Name = args[2]; - window.external.yellows111_Users[0].Name = __messengerjs__myNickname; + window.external.yellows111_Users[0].Name = __messengerjs__.myNickname; } - if(__messengerjs__imTheChannelOwner === true) { + if(__messengerjs__.imTheChannelOwner === true) { window.external.Users.Me = window.external.yellows111_Users[0]; window.external.Users.Inviter = window.external.yellows111_Users[0]; } else { window.external.Users.Me = window.external.yellows111_Users[1]; window.external.Users.Inviter = window.external.yellows111_Users[0]; } - document.title = __messengerjs__myNickname + ": " + document.title; + document.title = __messengerjs__.myNickname + ": " + document.title; /** beyond even hack territory **/ - if(typeof __messengerjs__onloadfunction === "function") { - __messengerjs__onloadfunction(__messengerjs__onloadargs); + if(typeof __messengerjs__.onloadfunction === "function") { + __messengerjs__.onloadfunction(__messengerjs__.onloadargs); } Channel_OnRemoteAppLoaded(); Channel_OnTypeChanged(); break; } case "remuser": { - __messengerjs__fade.textContent = "opponent (" + args[1] + ") disconnected :("; - __messengerjs__fade.style.display = "revert"; + __messengerjs__.fade.textContent = "opponent (" + args[1] + ") disconnected. refresh to get a new session!"; + __messengerjs__.fade.style.display = "block"; window.removeEventListener("beforeunload", beforeUnloadHandler); Channel_OnRemoteAppClosed(); beforeUnloadHandler(null, "other user disconnected"); // disconnect ourselves @@ -221,8 +224,8 @@ channelSocket.onmessage = function(event) { } channelSocket.onerror = function(ev) { window.external.Channel.Error.Data = "WebSocket generic error"; - __messengerjs__fade.textContent = "encountered a WebSocket error" - __messengerjs__fade.style.display = "revert"; + __messengerjs__.fade.textContent = "encountered a WebSocket error" + __messengerjs__.fade.style.display = "block"; Channel_OnDataError(); } channelSocket.onclose = function() { @@ -233,27 +236,34 @@ function createWSEventsNowThatImReady() { if(channelSocket.readyState === 1) { // if we're already there channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["rename"]))); - channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__sessionID]))); + channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__.sessionID]))); } channelSocket.onopen = function() { channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["rename"]))); - channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__sessionID]))); + channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__.sessionID]))); } } /** pure evil **/ -window.onload = function(ev) { - document.body.append(__messengerjs__fade); - if(new URL(location.href).searchParams.get("channel") === null) { - __messengerjs__sessionID = Math.random().toString(36).split('.')[1].substring(0,8); - history.pushState(null, "", `?channel=${__messengerjs__sessionID}`); +__messengerjs__.OnLoadInterrupt = function(ev) { + document.body.appendChild(__messengerjs__.fade); + if(new URL(location.href).hash.substring(1).split("=")[0] === "channel") { + __messengerjs__.sessionID = new URL(location.href).hash.substring(1).split("=")[1]; } else { - __messengerjs__sessionID = new URL(location.href).searchParams.get("channel"); + __messengerjs__.sessionID = Math.random().toString(36).split('.')[1].substring(0,8); + history.pushState(null, "", `#channel=${__messengerjs__.sessionID}`); } createWSEventsNowThatImReady(); - __messengerjs__onloadargs = ev; + __messengerjs__.onloadargs = ev; } -Object.defineProperty(window, "onload", { - set: function onload(fn) { - __messengerjs__onloadfunction = fn; +document.addEventListener("DOMContentLoaded", (event) => { + // do document.body.onload too since some things suck + if(typeof document.body.onload === "function") { + __messengerjs__.onloadfunction = document.body.onload; } + window.onload = __messengerjs__.OnLoadInterrupt; + Object.defineProperty(window, "onload", { + set: function onload(fn) { + __messengerjs__.onloadfunction = fn; + } + }); }); \ No newline at end of file diff --git a/server.js b/server.js index 80062c4..0194f8c 100644 --- a/server.js +++ b/server.js @@ -104,7 +104,7 @@ const MessageParser = function(webSocket, message, isBinary) { } if(ChannelStorage.has(getChannelId) === false) { // make the channel right then and there - console.log(`creating #${getChannelId}`); + console.log(`creating #${getChannelId} for (${webSocket.getUserData().uid})`); ChannelStorage.set(getChannelId, {"owner": webSocket.getUserData().uid}); }; if(webSocket.isSubscribed(`channels/${getChannelId}`) === true) { @@ -141,7 +141,7 @@ const MessageParser = function(webSocket, message, isBinary) { ); // if room is empty, delete it if(server.numSubscribers(`channels/${UserAttachedChannelStorage.get(webSocket.getUserData().uid)}`) === 0) { - console.log(`deleting #${UserAttachedChannelStorage.get(webSocket.getUserData().uid)}`); + console.log(`deleting #${UserAttachedChannelStorage.get(webSocket.getUserData().uid)} since (${webSocket.getUserData().uid}) left, leaving it with no members`); ChannelStorage.delete(UserAttachedChannelStorage.get(webSocket.getUserData().uid)); } UserAttachedChannelStorage.delete(webSocket.getUserData().uid);