diff --git a/client/src/AdriftClient.ts b/client/src/AdriftClient.ts index 723189b..832c132 100644 --- a/client/src/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -148,7 +148,7 @@ export class AdriftBareClient extends Client { connect( remote: URL, - protocols: string[], + protocols: string | string[], getRequestHeaders: GetRequestHeadersCallback, onMeta: MetaCallback, onReadyState: ReadyStateCallback, @@ -177,6 +177,7 @@ export class AdriftBareClient extends Client { let { send, close } = this.connection.wsconnect( remote, + protocols, () => { onReadyState(WebSocket.OPEN); ws.dispatchEvent(new Event("open")); diff --git a/client/src/Connection.ts b/client/src/Connection.ts index 996be0d..a282d8f 100644 --- a/client/src/Connection.ts +++ b/client/src/Connection.ts @@ -183,6 +183,7 @@ export class Connection { wsconnect( url: URL, + protocols: string | string[], onopen: () => void, onclose: (code: number, reason: string, wasClean: boolean) => void, onmessage: (data: any) => void, @@ -191,7 +192,7 @@ export class Connection { send: (data: any) => void; close: (code?: number, reason?: string) => void; } { - const payload: C2SWSOpenPayload = { url: url.toString() }; + const payload: C2SWSOpenPayload = { url: url.toString(), protocols }; const payloadJSON = JSON.stringify(payload); let seq = this.nextSeq(); // todo: onerror diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 394c557..a3b9cf7 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -187,7 +187,7 @@ const url = "ws://127.0.0.1:3002/"; const ws = ((window as any).ws = ( (window as any).bare as BareClient - ).createWebSocket(url, [], {})); + ).createWebSocket(url, ["a", "b"], {})); ws.onopen = () => console.log("onopen"); ws.addEventListener("open", () => console.log("open listener")); ws.onclose = () => console.error(new Error("onclose")); diff --git a/protocol/src/index.ts b/protocol/src/index.ts index 1d27843..756f5b4 100644 --- a/protocol/src/index.ts +++ b/protocol/src/index.ts @@ -39,6 +39,7 @@ export type HTTPResponsePayload = { export type C2SWSOpenPayload = { url: string; + protocols: string | string[]; }; export type WSClosePayload = { diff --git a/server/src/main.ts b/server/src/main.ts index 1bdee2c..bbe0650 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -9,9 +9,6 @@ import { auth } from "firebase-config"; import { getDatabase, onValue, ref, set } from "firebase/database"; import { AdriftServer } from "./server"; -import { WebSocket } from "isomorphic-ws"; - - const configuration = { iceServers: [ { @@ -175,7 +172,7 @@ async function connectFirebase() { } connectFirebase(); -let tracker = new WebSocket("ws://localhost:17776/join"); +/*let tracker = new WebSocket("ws://localhost:17776/join"); tracker.on("message", (str: string) => { if (!str) return; let data = JSON.parse(str); @@ -187,6 +184,6 @@ tracker.on("message", (str: string) => { tracker.send(JSON.stringify(answer)); }) -}); +});*/ app.listen(3000, () => console.log("listening")); diff --git a/server/src/server.ts b/server/src/server.ts index 208a541..45c73eb 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -283,7 +283,10 @@ export class AdriftServer { case C2SRequestTypes.WSOpen: { const payload = AdriftServer.tryParseJSONPayload(msg.slice(cursor)); - const ws = (this.sockets[seq] = new WebSocket(payload.url)); + const ws = (this.sockets[seq] = new WebSocket( + payload.url, + payload.protocols + )); ws.binaryType = "arraybuffer"; ws.onerror = (e) => { this.sendWSError(seq, { message: e.message }); diff --git a/server/src/test-ws-server.ts b/server/src/test-ws-server.ts index 075f97c..020f554 100644 --- a/server/src/test-ws-server.ts +++ b/server/src/test-ws-server.ts @@ -3,7 +3,7 @@ import { WebSocketServer } from "ws"; const wss = new WebSocketServer({ host: "127.0.0.1", port: 3002 }); wss.on("connection", (ws) => { - console.log("new connection"); + console.log("new connection", { protocol: ws.protocol }); ws.binaryType = "nodebuffer"; ws.on("message", (data: Buffer, isBinary: boolean) => {