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(
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"));

View file

@ -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

View file

@ -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"));

View file

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

View file

@ -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"));

View file

@ -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 });

View file

@ -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) => {