Skip to content

Commit

Permalink
godslayerakp/ws: A couple fixes and add sample project (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Nov 15, 2023
1 parent fd669f0 commit 6f1ef98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
2 changes: 2 additions & 0 deletions docs/godslayerakp/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,5 @@ when connection errors :: hat #307eff
Sometimes things don't go so well. Maybe your internet connection died, the server is down, or you typed in the wrong URL. There's a lot of things that can go wrong. These let you try to handle that.

Unfortunately we can't give much insight as to what caused the errors. Your browser tells us very little, but even if it did give us more information, it probably wouldn't be very helpful.

A connection can either close or error; it won't do both.
66 changes: 36 additions & 30 deletions extensions/godslayerakp/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

/**
* @typedef WebSocketInfo
* @property {boolean} instanceReplaced
* @property {boolean} manuallyClosed
* @property {boolean} destroyed
* @property {boolean} errored
* @property {string} closeMessage
* @property {number} closeCode
Expand Down Expand Up @@ -92,7 +91,7 @@
runtime.on("targetWasRemoved", (target) => {
const instance = this.instances[target.id];
if (instance) {
instance.manuallyClosed = true;
instance.destroyed = true;
if (instance.websocket) {
instance.websocket.close();
}
Expand Down Expand Up @@ -255,16 +254,15 @@

const oldInstance = this.instances[util.target.id];
if (oldInstance) {
oldInstance.instanceReplaced = true;
oldInstance.destroyed = true;
if (oldInstance.websocket) {
oldInstance.websocket.close();
}
}

/** @type {WebSocketInfo} */
const instance = {
instanceReplaced: false,
manuallyClosed: false,
destroyed: false,
errored: false,
closeMessage: "",
closeCode: 0,
Expand All @@ -282,11 +280,11 @@
.then(
(allowed) =>
new Promise((resolve) => {
if (
!allowed ||
instance.instanceReplaced ||
instance.manuallyClosed
) {
if (!allowed) {
throw new Error("Not allowed");
}

if (instance.destroyed) {
resolve();
return;
}
Expand Down Expand Up @@ -319,9 +317,8 @@
};

const onStopAll = () => {
instance.instanceReplaced = true;
instance.manuallyClosed = true;
instance.websocket.close();
instance.destroyed = true;
websocket.close();
};

vm.runtime.on("BEFORE_EXECUTE", beforeExecute);
Expand All @@ -339,7 +336,7 @@
};

websocket.onopen = (e) => {
if (instance.instanceReplaced || instance.manuallyClosed) {
if (instance.destroyed) {
cleanup();
websocket.close();
return;
Expand All @@ -359,24 +356,31 @@
};

websocket.onclose = (e) => {
if (instance.instanceReplaced) return;
instance.closeMessage = e.reason || "";
instance.closeCode = e.code;
runtime.startHats("gsaWebsocket_onClose", null, target);
cleanup();
if (!instance.errored) {
instance.closeMessage = e.reason || "";
instance.closeCode = e.code;
cleanup();

if (!instance.destroyed) {
runtime.startHats("gsaWebsocket_onClose", null, target);
}
}
};

websocket.onerror = (e) => {
if (instance.instanceReplaced) return;
console.error("websocket error", e);
instance.errored = true;
runtime.startHats("gsaWebsocket_onError", null, target);
cleanup();

if (!instance.destroyed) {
runtime.startHats("gsaWebsocket_onError", null, target);
}
};

websocket.onmessage = async (e) => {
if (instance.instanceReplaced || instance.manuallyClosed)
if (instance.destroyed) {
return;
}

let data = e.data;

Expand All @@ -402,6 +406,11 @@
)
.catch((error) => {
console.error("could not open websocket connection", error);

instance.errored = true;
if (!instance.destroyed) {
runtime.startHats("gsaWebsocket_onError", null, target);
}
});
}

Expand All @@ -422,10 +431,7 @@
isClosed(_, utils) {
const instance = this.instances[utils.target.id];
if (!instance) return false;
return (
!!instance.websocket &&
instance.websocket.readyState === WebSocket.CLOSED
);
return instance.closeCode !== 0;
}

closeCode(_, utils) {
Expand Down Expand Up @@ -466,7 +472,7 @@
closeWithoutReason(_, utils) {
const instance = this.instances[utils.target.id];
if (!instance) return;
instance.manuallyClosed = true;
instance.destroyed = true;
if (instance.websocket) {
instance.websocket.close();
}
Expand All @@ -475,7 +481,7 @@
closeWithCode(args, utils) {
const instance = this.instances[utils.target.id];
if (!instance) return;
instance.manuallyClosed = true;
instance.destroyed = true;
if (instance.websocket) {
instance.websocket.close(toCloseCode(args.CODE));
}
Expand All @@ -484,7 +490,7 @@
closeWithReason(args, utils) {
const instance = this.instances[utils.target.id];
if (!instance) return;
instance.manuallyClosed = true;
instance.destroyed = true;
if (instance.websocket) {
instance.websocket.close(
toCloseCode(args.CODE),
Expand Down
Binary file added samples/Cloud Variables.sb3
Binary file not shown.

0 comments on commit 6f1ef98

Please sign in to comment.