From bb6eb218d0e8bce7f4b9ff1f8bd762ba024620ef Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:30:50 -0700 Subject: [PATCH 1/3] Client -> AdriftServer --- server/src/main.ts | 12 ++++----- server/src/{client.ts => server.ts} | 38 ++++++++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-) rename server/src/{client.ts => server.ts} (89%) diff --git a/server/src/main.ts b/server/src/main.ts index e2aa9f5..fe0d5a6 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -4,10 +4,10 @@ import expressWs from "express-ws"; import { signInWithEmailAndPassword } from "firebase/auth"; import wrtc from "wrtc"; -import { Client } from "./client"; import { auth } from "firebase-config"; import { getDatabase, onValue, ref, set } from "firebase/database"; +import { AdriftServer } from "./server"; const configuration = { iceServers: [ @@ -98,20 +98,20 @@ async function answerRtc(data: any, onrespond: (answer: any) => void) { } }); - let client: Client; + let server: AdriftServer; dataChannel.onopen = () => { console.log("opened"); - client = new Client((msg) => dataChannel.send(msg)); + server = new AdriftServer((msg) => dataChannel.send(msg)); }; dataChannel.onclose = () => { console.log("closed"); - client.onClose(); + server.onClose(); }; dataChannel.onmessage = (event) => { console.log("messaged"); if (event.data instanceof Buffer) { - client.onMsg(bufferToArrayBuffer(event.data)); + server.onMsg(bufferToArrayBuffer(event.data)); } throw new Error("Unexpected datachannel message type"); }; @@ -127,7 +127,7 @@ app.post("/connect", (req, res) => { app.ws("/dev-ws", (ws, _req) => { console.log("ws connect"); - const client = new Client((msg) => ws.send(msg)); + const client = new AdriftServer((msg) => ws.send(msg)); ws.on("message", (msg) => { if (typeof msg === "string") { diff --git a/server/src/client.ts b/server/src/server.ts similarity index 89% rename from server/src/client.ts rename to server/src/server.ts index c3c2eb3..0fe5e68 100644 --- a/server/src/client.ts +++ b/server/src/server.ts @@ -12,7 +12,21 @@ import { import { Readable } from "stream"; import { BareError, bareFetch, options } from "./http"; -export class Client { +function bareErrorToResponse(e: BareError): { + payload: HTTPResponsePayload; + body: AsyncIterable; +} { + return { + payload: { + status: e.status, + statusText: STATUS_CODES[e.status] || "", + headers: {}, + }, + body: Readable.from(JSON.stringify(e.body)), + }; +} + +export class AdriftServer { send: (msg: ArrayBuffer) => void; events: EventEmitter; @@ -58,20 +72,6 @@ export class Client { return payload; } - static bareErrorToResponse(e: BareError): { - payload: HTTPResponsePayload; - body: AsyncIterable; - } { - return { - payload: { - status: e.status, - statusText: STATUS_CODES[e.status] || "", - headers: {}, - }, - body: Readable.from(JSON.stringify(e.body)), - }; - } - async handleHTTPRequest(payload: HTTPRequestPayload): Promise<{ payload: HTTPResponsePayload; body: AsyncIterable; @@ -93,7 +93,7 @@ export class Client { ); } catch (e) { if (e instanceof BareError) { - return Client.bareErrorToResponse(e); + return bareErrorToResponse(e); } this.events.off("close", onClose); throw e; @@ -149,7 +149,7 @@ export class Client { } async onMsg(msg: ArrayBuffer) { - const init = Client.parseMsgInit(msg); + const init = AdriftServer.parseMsgInit(msg); if (!init) return; const { cursor, seq, op } = init; switch (op) { @@ -158,7 +158,7 @@ export class Client { payload: HTTPResponsePayload; body: AsyncIterable; }; - const reqPayload = Client.parseHttpReqPayload(msg.slice(cursor)); + const reqPayload = AdriftServer.parseHttpReqPayload(msg.slice(cursor)); if (!reqPayload) return; try { resp = await this.handleHTTPRequest(reqPayload); @@ -184,7 +184,7 @@ export class Client { }); } - resp = Client.bareErrorToResponse(bareError); + resp = bareErrorToResponse(bareError); } const { payload, body } = resp; From 296288467beb6c3b9a5b19fdfc3e4707e4711540 Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:53:20 -0700 Subject: [PATCH 2/3] bit of cleanup --- client/src/AdriftClient.ts | 51 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/client/src/AdriftClient.ts b/client/src/AdriftClient.ts index f2cd4f1..7df88d1 100644 --- a/client/src/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -5,8 +5,8 @@ import { GetRequestHeadersCallback, MetaCallback, ReadyStateCallback, + WebSocketFields, WebSocketImpl, - WebSocketFields } from "bare-client-custom"; import { Connection } from "./Connection"; @@ -67,6 +67,7 @@ export class AdriftBareClient extends Client { headers, }) as BareResponse; } + connect( remote: URL, protocols: string[], @@ -75,26 +76,29 @@ export class AdriftBareClient extends Client { onReadyState: ReadyStateCallback, webSocketImpl: WebSocketImpl ): WebSocket { - const ws = new WebSocket("ws:null"); + const ws = new webSocketImpl("ws:null"); // this will error. that's okay + let send = this.connection.wsconnect( + remote, + () => { + onReadyState(WebSocketFields.OPEN); + ws.dispatchEvent(new Event("open")); + }, + () => { + onReadyState(WebSocketFields.CLOSED); + ws.dispatchEvent(new Event("close")); - let send = this.connection.wsconnect(remote, () => { - onReadyState(WebSocketFields.OPEN); - ws.dispatchEvent(new Event('open')); - - }, () => { - onReadyState(WebSocketFields.CLOSED); - ws.dispatchEvent(new Event('close')); - - // what do i do for WebSocketFields.closing? - }, (data) => { - ws.dispatchEvent(new MessageEvent("message", { - data, - // might be other shit here idk - })); - }); - + // what do i do for WebSocketFields.closing? + }, + (data) => { + ws.dispatchEvent( + new MessageEvent("message", { + data, + }) + ); + } + ); // const cleanup = () => { // ws.removeEventListener('close', closeListener); @@ -105,17 +109,15 @@ export class AdriftBareClient extends Client { // cleanup(); // }; - (ws as any).__defineGetter__("send", () => (data: any) => { send(data); }); - (ws as any).__defineSetter__("send", () => { }); + (ws as any).__defineSetter__("send", () => {}); // ws.send = (data) => { // console.log("sending data to server:" + data); // }; // console.log(ws.send); - const messageListener = (event: MessageEvent) => { // cleanup(); @@ -139,13 +141,11 @@ export class AdriftBareClient extends Client { // // now we want the client to see the websocket is open and ready to communicate with the remote // onReadyState(WebSocketFields.OPEN); - ws.dispatchEvent(new Event('open')); + ws.dispatchEvent(new Event("open")); }; - setTimeout(messageListener, 30); - // ws.addEventListener('close', closeListener); - ws.addEventListener('message', messageListener); + ws.addEventListener("message", messageListener); // // CONNECTED TO THE BARE SERVER, NOT THE REMOTE // ws.addEventListener( @@ -177,7 +177,6 @@ export class AdriftBareClient extends Client { // { once: true } // ); - return ws; } } From d330c9346ece23fbf7453691de07fa1b456ba277 Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:55:54 -0700 Subject: [PATCH 3/3] use protocols --- client/src/AdriftClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/AdriftClient.ts b/client/src/AdriftClient.ts index 7df88d1..23eb614 100644 --- a/client/src/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -76,7 +76,7 @@ export class AdriftBareClient extends Client { onReadyState: ReadyStateCallback, webSocketImpl: WebSocketImpl ): WebSocket { - const ws = new webSocketImpl("ws:null"); + const ws = new webSocketImpl("ws:null", protocols); // this will error. that's okay let send = this.connection.wsconnect(