forward ws protocols

This commit is contained in:
Spencer Pogorzelski 2023-08-14 21:20:47 -07:00
parent eae44fef4b
commit ccf556118b
7 changed files with 13 additions and 10 deletions

View file

@ -148,7 +148,7 @@ export class AdriftBareClient extends Client {
connect( connect(
remote: URL, remote: URL,
protocols: string[], protocols: string | string[],
getRequestHeaders: GetRequestHeadersCallback, getRequestHeaders: GetRequestHeadersCallback,
onMeta: MetaCallback, onMeta: MetaCallback,
onReadyState: ReadyStateCallback, onReadyState: ReadyStateCallback,
@ -177,6 +177,7 @@ export class AdriftBareClient extends Client {
let { send, close } = this.connection.wsconnect( let { send, close } = this.connection.wsconnect(
remote, remote,
protocols,
() => { () => {
onReadyState(WebSocket.OPEN); onReadyState(WebSocket.OPEN);
ws.dispatchEvent(new Event("open")); ws.dispatchEvent(new Event("open"));

View file

@ -183,6 +183,7 @@ export class Connection {
wsconnect( wsconnect(
url: URL, url: URL,
protocols: string | string[],
onopen: () => void, onopen: () => void,
onclose: (code: number, reason: string, wasClean: boolean) => void, onclose: (code: number, reason: string, wasClean: boolean) => void,
onmessage: (data: any) => void, onmessage: (data: any) => void,
@ -191,7 +192,7 @@ export class Connection {
send: (data: any) => void; send: (data: any) => void;
close: (code?: number, reason?: string) => 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); const payloadJSON = JSON.stringify(payload);
let seq = this.nextSeq(); let seq = this.nextSeq();
// todo: onerror // todo: onerror

View file

@ -187,7 +187,7 @@
const url = "ws://127.0.0.1:3002/"; const url = "ws://127.0.0.1:3002/";
const ws = ((window as any).ws = ( const ws = ((window as any).ws = (
(window as any).bare as BareClient (window as any).bare as BareClient
).createWebSocket(url, [], {})); ).createWebSocket(url, ["a", "b"], {}));
ws.onopen = () => console.log("onopen"); ws.onopen = () => console.log("onopen");
ws.addEventListener("open", () => console.log("open listener")); ws.addEventListener("open", () => console.log("open listener"));
ws.onclose = () => console.error(new Error("onclose")); ws.onclose = () => console.error(new Error("onclose"));

View file

@ -39,6 +39,7 @@ export type HTTPResponsePayload = {
export type C2SWSOpenPayload = { export type C2SWSOpenPayload = {
url: string; url: string;
protocols: string | string[];
}; };
export type WSClosePayload = { export type WSClosePayload = {

View file

@ -9,9 +9,6 @@ import { auth } from "firebase-config";
import { getDatabase, onValue, ref, set } from "firebase/database"; import { getDatabase, onValue, ref, set } from "firebase/database";
import { AdriftServer } from "./server"; import { AdriftServer } from "./server";
import { WebSocket } from "isomorphic-ws";
const configuration = { const configuration = {
iceServers: [ iceServers: [
{ {
@ -175,7 +172,7 @@ async function connectFirebase() {
} }
connectFirebase(); connectFirebase();
let tracker = new WebSocket("ws://localhost:17776/join"); /*let tracker = new WebSocket("ws://localhost:17776/join");
tracker.on("message", (str: string) => { tracker.on("message", (str: string) => {
if (!str) return; if (!str) return;
let data = JSON.parse(str); let data = JSON.parse(str);
@ -187,6 +184,6 @@ tracker.on("message", (str: string) => {
tracker.send(JSON.stringify(answer)); tracker.send(JSON.stringify(answer));
}) })
}); });*/
app.listen(3000, () => console.log("listening")); app.listen(3000, () => console.log("listening"));

View file

@ -283,7 +283,10 @@ export class AdriftServer {
case C2SRequestTypes.WSOpen: { case C2SRequestTypes.WSOpen: {
const payload = AdriftServer.tryParseJSONPayload(msg.slice(cursor)); 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.binaryType = "arraybuffer";
ws.onerror = (e) => { ws.onerror = (e) => {
this.sendWSError(seq, { message: e.message }); this.sendWSError(seq, { message: e.message });

View file

@ -3,7 +3,7 @@ import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ host: "127.0.0.1", port: 3002 }); const wss = new WebSocketServer({ host: "127.0.0.1", port: 3002 });
wss.on("connection", (ws) => { wss.on("connection", (ws) => {
console.log("new connection"); console.log("new connection", { protocol: ws.protocol });
ws.binaryType = "nodebuffer"; ws.binaryType = "nodebuffer";
ws.on("message", (data: Buffer, isBinary: boolean) => { ws.on("message", (data: Buffer, isBinary: boolean) => {