From 1a730fdb644f98e6764f67ee1ef8dc34f53f8350 Mon Sep 17 00:00:00 2001 From: yellows111 Date: Sat, 23 Mar 2024 16:25:57 +0000 Subject: [PATCH] beta 5 - breaking changes some things considered Channel_OnTypeChanged optional, grr. Make fourCC a thing so people don't manage to get the wrong channel for their activities and then break stuff --- .gitignore | 3 ++- client.js | 26 ++++++++++++++++---------- server.js | 10 ++++++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index bd9865a..2322d3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ tmp/* -node_modules/* \ No newline at end of file +node_modules/* +*.bak diff --git a/client.js b/client.js index 6f3a2e9..03c645b 100644 --- a/client.js +++ b/client.js @@ -10,6 +10,12 @@ const __messengerjs__ = { __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..."; +__messengerjs__.callIfExists = function(fnName) { + // THIS function exists because some of the events we call may not exist on the client... why? + if(typeof window[fnName] === "function") { + window[fnName](); + } +} const beforeUnloadHandler = function(ev) { Channel_OnAppClose(); @@ -84,8 +90,8 @@ window.external.Channel = {"Data": "", "SendData": null, "Initialize": null, "Er // things may want Channel.IM: string and Channel.FileInfo: object {Path: string, Size: number, Progress: number<0-100>, Incoming: bool, Status: num<0-3>} window.external.Channel.SendData = function(d) {channelSocket.send(d)}; // this is dumb but trying to redirect this gives you TypeError's window.external.Channel.Initialize = function() { - // no idea we're already pretty initalized by the time most things call this - Channel_OnTypeChanged(); // make client check if its even connected still + // no idea we're already pretty initalized by the time most things call + __messengerjs__.callIfExists("Channel_OnTypeChanged"); // make client check if its even connected still }; Object.defineProperty(window.external.Channel, "Type", { get: function Type() { @@ -165,7 +171,7 @@ channelSocket.onmessage = function(event) { __messengerjs__.fade.textContent = "the channel server was closed. please try again later."; __messengerjs__.fade.style.display = "block"; channelSocket.close(1000, "server shutdown acknoledged."); - Channel_OnTypeChanged(); + __messengerjs__.callIfExists("Channel_OnTypeChanged"); Channel_OnRemoteAppClosed(); // they're gone Channel_OnAppClose(); // we're gone too break; @@ -174,15 +180,15 @@ channelSocket.onmessage = function(event) { console.warn("channel was full, generating new room"); __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]))); + channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["connect", __messengerjs__.sessionID, window.GameCode]))); break; } default: { __messengerjs__.fade.textContent = "server error:" + args[1] + "code (" + args[2] + ")"; __messengerjs__.fade.style.display = "block"; channelSocket.close(1000, "server sent non-zero error"); - Channel_OnTypeChanged(); - Channel_OnDataError(); // this may just attempt to retry to send the failed data... when we're already closed; + __messengerjs__.callIfExists("Channel_OnTypeChanged"); + __messengerjs__.callIfExists("Channel_OnDataError"); // this may just attempt to retry to send the failed data... when we're already closed; } } break; @@ -209,7 +215,7 @@ channelSocket.onmessage = function(event) { __messengerjs__.onloadfunction(__messengerjs__.onloadargs); } Channel_OnRemoteAppLoaded(); - Channel_OnTypeChanged(); + __messengerjs__.callIfExists("Channel_OnTypeChanged"); break; } case "remuser": { @@ -240,18 +246,18 @@ channelSocket.onerror = function(ev) { Channel_OnDataError(); } channelSocket.onclose = function() { - Channel_OnTypeChanged(); + __messengerjs__.callIfExists("Channel_OnTypeChanged"); } 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, window.GameCode]))); } 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, window.GameCode]))); } } /** pure evil **/ diff --git a/server.js b/server.js index fba9ac5..498948e 100644 --- a/server.js +++ b/server.js @@ -87,7 +87,7 @@ const MessageParser = function(webSocket, message, isBinary) { break; } case "connect": { - if(args.length <= 1) { + if(args.length <= 2) { webSocket.send(EncodeCommandString(["error", "need more arguments", 768]), true); break; } @@ -105,8 +105,14 @@ const MessageParser = function(webSocket, message, isBinary) { if(ChannelStorage.has(getChannelId) === false) { // make the channel right then and there console.log(`creating #${getChannelId} for (${webSocket.getUserData().uid})`); - ChannelStorage.set(getChannelId, {"owner": webSocket.getUserData().uid}); + ChannelStorage.set(getChannelId, {"owner": webSocket.getUserData().uid, "fourCC": args[2]}); }; + if(ChannelStorage.get(getChannelId).fourCC !== args[2]) { + // if the fourCC is not the same as the channel, error as such + webSocket.send(EncodeCommandString(["error", `data is incompatible! (You: ${args[2]}, Remote: ${ChannelStorage.get(getChannelId).fourCC}.`, 256]), true); + break; + + } if(webSocket.isSubscribed(`channels/${getChannelId}`) === true) { // already in this channel webSocket.send(EncodeCommandString(["error", "already in this channel", 517]), true);