Skip to content

Commit

Permalink
Add networking in ws protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Aug 15, 2024
1 parent d6d1f53 commit 57a3607
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@typescript-eslint/parser": "^6.20.0",
"autoprefixer": "^10.4.17",
"daisyui": "^3.9.3",
"emulators": "8.1.4",
"emulators": "8.1.5",
"eslint": "^8.56.0",
"eslint-config-google": "^0.14.0",
"postcss": "^8.4.33",
Expand Down
33 changes: 33 additions & 0 deletions src/ws/ws-transport-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const clientMessageValues: ClientMessage[] = [
"wc-install", "wc-run", "wc-pack-fs-to-bundle", "wc-add-key", "wc-mouse-move", "wc-mouse-button", "wc-mouse-sync",
"wc-exit", "wc-sync-sleep", "wc-pause", "wc-resume", "wc-mute", "wc-unmute", "wc-connect", "wc-disconnect",
"wc-backend-event", "wc-asyncify-stats", "wc-fs-tree", "wc-fs-get-file", "wc-send-data-chunk",
"wc-net-connected", "wc-net-received",
];
const clientMessageEnum: { [msg: string]: number } = {};
clientMessageValues.forEach((v, i) => clientMessageEnum[v] = i);
Expand All @@ -40,6 +41,7 @@ const serverMessageValues: ServerMessage[] = [
"ws-stdout", "ws-exit", "ws-persist", "ws-sound-init", "ws-sound-push",
"ws-config", "ws-sync-sleep", "ws-connected", "ws-disconnected",
"ws-asyncify-stats", "ws-fs-tree", "ws-send-data-chunk",
"ws-net-connect", "ws-net-disconnect", "ws-net-send",
];
const serverMessageEnum: { [num: string]: ServerMessage } = {};
serverMessageValues.forEach((v, i) => serverMessageEnum[i] = v);
Expand Down Expand Up @@ -262,6 +264,27 @@ export class WsTransportLayer implements TransportLayer {
bundle: payload.length > 0 ? payload[0]! : null,
});
} break;
case "ws-net-connect": {
let address = textDecoder.decode(payload[0]!);
if (!address.startsWith("wss://") && !address.startsWith("ws://")) {
address = ((
window.location.protocol === "http:" &&
window.location.hostname !== "localhost"
) ? "ws://" : "wss://") + address;
}
this.handler("ws-net-connect", { address });
} break;
case "ws-net-send": {
this.handler("ws-net-send", {
networkId: this.readUint32(payload[0]!, 0),
data: payload[1],
});
} break;
case "ws-net-disconnect": {
this.handler("ws-net-disconnect", {
networkId: this.readUint32(payload[0]!, 0),
});
} break;
default: {
if (message === undefined) { // not standard messages
(async () => {
Expand Down Expand Up @@ -399,6 +422,16 @@ export class WsTransportLayer implements TransportLayer {
case "wc-pack-fs-to-bundle": {
this.sendMessageToSocket(messageId, new Uint8Array([props.onlyChanges ? 1 : 0]));
} break;
case "wc-net-connected": {
const id = new Uint8Array(4);
this.writeUint32(id, props.networkId + 1, 0);
this.sendMessageToSocket(messageId, id);
} break;
case "wc-net-received": {
const id = new Uint8Array(4);
this.writeUint32(id, props.networkId, 0);
this.sendMessageToSocket(messageId, id, new Uint8Array(props.data));
} break;
default: {
console.log("Unhandled client message (wc):", name, messageId, props);
} break;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,10 @@ electron-to-chromium@^1.4.648:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.653.tgz#832ab25e80ad698ac09c1ca547bd9ee6cce7df10"
integrity sha512-wA2A2LQCqnEwQAvwADQq3KpMpNwgAUBnRmrFgRzHnPhbQUFArTR32Ab46f4p0MovDLcg4uqd4nCsN2hTltslpA==

[email protected].4:
version "8.1.4"
resolved "https://registry.yarnpkg.com/emulators/-/emulators-8.1.4.tgz#31945d45081638135b049995fe5d1e6ca2961282"
integrity sha512-+DinIGKzC5TgGhg+/3l09oGO0q40G/VgW3cvWmHkRjD1x5CXOtnqRXgsmtEqtdN4FZDmdS1WhG/FJ2Tn+lD/SQ==
[email protected].5:
version "8.1.5"
resolved "https://registry.yarnpkg.com/emulators/-/emulators-8.1.5.tgz#1fc64ac44b0c0a877887b6798715b9dcf325b81c"
integrity sha512-2UmGHvDKkN01Pwvh45loHlbLArRj17MiuOdfJUjx0C7UMRojKaXPGpHYvHtZe46TY8itCio0ST84Y9KRb7288w==

entities@^4.2.0:
version "4.5.0"
Expand Down

0 comments on commit 57a3607

Please sign in to comment.