beta 3: changing queries to hashes
ughhhh stupid language select being on the query string... fine, have this then
This commit is contained in:
parent
a5a15e3839
commit
f9bd6c5b69
84
client.js
84
client.js
|
@ -1,12 +1,15 @@
|
||||||
const channelSocket = new WebSocket("ws://localhost:9091/connect");
|
const channelSocket = new WebSocket("ws://localhost:9091/connect");
|
||||||
var __messengerjs__sessionID = null;
|
const __messengerjs__ = {
|
||||||
var __messengerjs__imTheChannelOwner = false;
|
"sessionID": null,
|
||||||
var __messengerjs__myNickname = "";
|
"imTheChannelOwner": false,
|
||||||
var __messengerjs__onloadfunction = null;
|
"myNickname": "",
|
||||||
var __messengerjs__onloadargs = null;
|
"onloadfunction": null,
|
||||||
const __messengerjs__fade = document.createElement("div");
|
"onloadargs": null,
|
||||||
__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;";
|
"fade": null
|
||||||
__messengerjs__fade.textContent = "connecting to WebSocket server...";
|
}
|
||||||
|
__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) {
|
const beforeUnloadHandler = function(ev, reason) {
|
||||||
Channel_OnAppClose();
|
Channel_OnAppClose();
|
||||||
|
@ -132,7 +135,7 @@ channelSocket.onmessage = function(event) {
|
||||||
let args = ParseCommandString(new TextDecoder("utf8").decode(event.data));
|
let args = ParseCommandString(new TextDecoder("utf8").decode(event.data));
|
||||||
switch(args[0]) {
|
switch(args[0]) {
|
||||||
case "rename": {
|
case "rename": {
|
||||||
__messengerjs__myNickname = args[3];
|
__messengerjs__.myNickname = args[3];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "connect": {
|
case "connect": {
|
||||||
|
@ -140,19 +143,19 @@ channelSocket.onmessage = function(event) {
|
||||||
case "0": {
|
case "0": {
|
||||||
console.error("channel is full or not avaliable");
|
console.error("channel is full or not avaliable");
|
||||||
// regenerate channel ID if we get 0'd
|
// regenerate channel ID if we get 0'd
|
||||||
__messengerjs__sessionID = Math.random().toString(36).split('.')[1].substring(0,8);
|
__messengerjs__.sessionID = Math.random().toString(36).split('.')[1].substring(0,8);
|
||||||
history.pushState(null, "", `?channel=${__messengerjs__sessionID}`);
|
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])));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "1": {
|
case "1": {
|
||||||
console.log("joined channel successfully.");
|
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);
|
window.addEventListener("beforeunload", beforeUnloadHandler);
|
||||||
if(args[2] === "1") {
|
if(args[2] === "1") {
|
||||||
__messengerjs__imTheChannelOwner = false;
|
__messengerjs__.imTheChannelOwner = false;
|
||||||
} else {
|
} else {
|
||||||
__messengerjs__imTheChannelOwner = true;
|
__messengerjs__.imTheChannelOwner = true;
|
||||||
if(window.external.Users.Inviter !== window.external.Users.Me) {
|
if(window.external.Users.Inviter !== window.external.Users.Me) {
|
||||||
// This channel has already been closed. Create a new one.
|
// This channel has already been closed. Create a new one.
|
||||||
beforeUnloadHandler(null, "channel was empty when expecting host");
|
beforeUnloadHandler(null, "channel was empty when expecting host");
|
||||||
|
@ -175,33 +178,33 @@ channelSocket.onmessage = function(event) {
|
||||||
Channel_OnTypeChanged();
|
Channel_OnTypeChanged();
|
||||||
}
|
}
|
||||||
case "adduser": {
|
case "adduser": {
|
||||||
__messengerjs__fade.style.display = "none";
|
__messengerjs__.fade.style.display = "none";
|
||||||
if(__messengerjs__imTheChannelOwner === false) {
|
if(__messengerjs__.imTheChannelOwner === false) {
|
||||||
window.external.yellows111_Users[1].Name = __messengerjs__myNickname;
|
window.external.yellows111_Users[1].Name = __messengerjs__.myNickname;
|
||||||
window.external.yellows111_Users[0].Name = args[2];
|
window.external.yellows111_Users[0].Name = args[2];
|
||||||
} else {
|
} else {
|
||||||
window.external.yellows111_Users[1].Name = args[2];
|
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.Me = window.external.yellows111_Users[0];
|
||||||
window.external.Users.Inviter = window.external.yellows111_Users[0];
|
window.external.Users.Inviter = window.external.yellows111_Users[0];
|
||||||
} else {
|
} else {
|
||||||
window.external.Users.Me = window.external.yellows111_Users[1];
|
window.external.Users.Me = window.external.yellows111_Users[1];
|
||||||
window.external.Users.Inviter = window.external.yellows111_Users[0];
|
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 **/
|
/** beyond even hack territory **/
|
||||||
if(typeof __messengerjs__onloadfunction === "function") {
|
if(typeof __messengerjs__.onloadfunction === "function") {
|
||||||
__messengerjs__onloadfunction(__messengerjs__onloadargs);
|
__messengerjs__.onloadfunction(__messengerjs__.onloadargs);
|
||||||
}
|
}
|
||||||
Channel_OnRemoteAppLoaded();
|
Channel_OnRemoteAppLoaded();
|
||||||
Channel_OnTypeChanged();
|
Channel_OnTypeChanged();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "remuser": {
|
case "remuser": {
|
||||||
__messengerjs__fade.textContent = "opponent (" + args[1] + ") disconnected :(";
|
__messengerjs__.fade.textContent = "opponent (" + args[1] + ") disconnected. refresh to get a new session!";
|
||||||
__messengerjs__fade.style.display = "revert";
|
__messengerjs__.fade.style.display = "block";
|
||||||
window.removeEventListener("beforeunload", beforeUnloadHandler);
|
window.removeEventListener("beforeunload", beforeUnloadHandler);
|
||||||
Channel_OnRemoteAppClosed();
|
Channel_OnRemoteAppClosed();
|
||||||
beforeUnloadHandler(null, "other user disconnected"); // disconnect ourselves
|
beforeUnloadHandler(null, "other user disconnected"); // disconnect ourselves
|
||||||
|
@ -221,8 +224,8 @@ channelSocket.onmessage = function(event) {
|
||||||
}
|
}
|
||||||
channelSocket.onerror = function(ev) {
|
channelSocket.onerror = function(ev) {
|
||||||
window.external.Channel.Error.Data = "WebSocket generic error";
|
window.external.Channel.Error.Data = "WebSocket generic error";
|
||||||
__messengerjs__fade.textContent = "encountered a WebSocket error"
|
__messengerjs__.fade.textContent = "encountered a WebSocket error"
|
||||||
__messengerjs__fade.style.display = "revert";
|
__messengerjs__.fade.style.display = "block";
|
||||||
Channel_OnDataError();
|
Channel_OnDataError();
|
||||||
}
|
}
|
||||||
channelSocket.onclose = function() {
|
channelSocket.onclose = function() {
|
||||||
|
@ -233,27 +236,34 @@ function createWSEventsNowThatImReady() {
|
||||||
if(channelSocket.readyState === 1) {
|
if(channelSocket.readyState === 1) {
|
||||||
// if we're already there
|
// if we're already there
|
||||||
channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["rename"])));
|
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.onopen = function() {
|
||||||
channelSocket.send(new TextEncoder("utf8").encode(EncodeCommandString(["rename"])));
|
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 **/
|
/** pure evil **/
|
||||||
window.onload = function(ev) {
|
__messengerjs__.OnLoadInterrupt = function(ev) {
|
||||||
document.body.append(__messengerjs__fade);
|
document.body.appendChild(__messengerjs__.fade);
|
||||||
if(new URL(location.href).searchParams.get("channel") === null) {
|
if(new URL(location.href).hash.substring(1).split("=")[0] === "channel") {
|
||||||
__messengerjs__sessionID = Math.random().toString(36).split('.')[1].substring(0,8);
|
__messengerjs__.sessionID = new URL(location.href).hash.substring(1).split("=")[1];
|
||||||
history.pushState(null, "", `?channel=${__messengerjs__sessionID}`);
|
|
||||||
} else {
|
} 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();
|
createWSEventsNowThatImReady();
|
||||||
__messengerjs__onloadargs = ev;
|
__messengerjs__.onloadargs = ev;
|
||||||
}
|
}
|
||||||
|
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", {
|
Object.defineProperty(window, "onload", {
|
||||||
set: function onload(fn) {
|
set: function onload(fn) {
|
||||||
__messengerjs__onloadfunction = fn;
|
__messengerjs__.onloadfunction = fn;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
|
@ -104,7 +104,7 @@ const MessageParser = function(webSocket, message, isBinary) {
|
||||||
}
|
}
|
||||||
if(ChannelStorage.has(getChannelId) === false) {
|
if(ChannelStorage.has(getChannelId) === false) {
|
||||||
// make the channel right then and there
|
// 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});
|
ChannelStorage.set(getChannelId, {"owner": webSocket.getUserData().uid});
|
||||||
};
|
};
|
||||||
if(webSocket.isSubscribed(`channels/${getChannelId}`) === true) {
|
if(webSocket.isSubscribed(`channels/${getChannelId}`) === true) {
|
||||||
|
@ -141,7 +141,7 @@ const MessageParser = function(webSocket, message, isBinary) {
|
||||||
);
|
);
|
||||||
// if room is empty, delete it
|
// if room is empty, delete it
|
||||||
if(server.numSubscribers(`channels/${UserAttachedChannelStorage.get(webSocket.getUserData().uid)}`) === 0) {
|
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));
|
ChannelStorage.delete(UserAttachedChannelStorage.get(webSocket.getUserData().uid));
|
||||||
}
|
}
|
||||||
UserAttachedChannelStorage.delete(webSocket.getUserData().uid);
|
UserAttachedChannelStorage.delete(webSocket.getUserData().uid);
|
||||||
|
|
Loading…
Reference in New Issue