This commit is contained in:
CoolElectronics 2023-08-19 18:31:48 -04:00
commit 2c1bebcace
4 changed files with 18 additions and 8 deletions

View file

@ -182,8 +182,9 @@ export class AdriftBareClient extends Client {
let { send, close } = this.connection.wsconnect( let { send, close } = this.connection.wsconnect(
remote, remote,
protocols, protocols,
() => { (protocol: string) => {
onReadyState(WebSocket.OPEN); onReadyState(WebSocket.OPEN);
(ws as any).protocol = protocol;
ws.dispatchEvent(new Event("open")); ws.dispatchEvent(new Event("open"));
}, },
(code: number, reason: string, wasClean: boolean) => { (code: number, reason: string, wasClean: boolean) => {

View file

@ -9,6 +9,7 @@ import {
ProtoBareHeaders, ProtoBareHeaders,
S2CRequestType, S2CRequestType,
S2CRequestTypes, S2CRequestTypes,
S2CWSOpenPayload,
S2C_HELLO_ERR, S2C_HELLO_ERR,
S2C_HELLO_OK, S2C_HELLO_OK,
Transport, Transport,
@ -17,7 +18,7 @@ import {
} from "protocol"; } from "protocol";
type OpenWSMeta = { type OpenWSMeta = {
onopen: () => void; onopen: (protocol: string) => void;
onclose: (code: number, reason: string, wasClean: boolean) => void; onclose: (code: number, reason: string, wasClean: boolean) => void;
onmessage: (data: ReadableStream, isBinary: boolean) => void; onmessage: (data: ReadableStream, isBinary: boolean) => void;
onerror: (message: string) => void; onerror: (message: string) => void;
@ -126,12 +127,15 @@ export class Connection {
delete this.openRequestStreams[requestID]; delete this.openRequestStreams[requestID];
break; break;
case S2CRequestTypes.WSOpen: case S2CRequestTypes.WSOpen: {
const socketMeta = this.openingSockets[requestID]; const socketMeta = this.openingSockets[requestID];
if (!socketMeta) return;
const payload: S2CWSOpenPayload = msgJSON();
delete this.openingSockets[requestID]; delete this.openingSockets[requestID];
this.openSockets[requestID] = socketMeta; this.openSockets[requestID] = socketMeta;
setTimeout(() => socketMeta.onopen()); setTimeout(() => socketMeta.onopen(payload.protocol));
break; break;
}
case S2CRequestTypes.WSBinaryStart: case S2CRequestTypes.WSBinaryStart:
case S2CRequestTypes.WSTextStart: { case S2CRequestTypes.WSTextStart: {
@ -270,7 +274,7 @@ export class Connection {
wsconnect( wsconnect(
url: URL, url: URL,
protocols: string | string[], protocols: string | string[],
onopen: () => void, onopen: (protocol: string) => void,
onclose: (code: number, reason: string, wasClean: boolean) => void, onclose: (code: number, reason: string, wasClean: boolean) => void,
onmessage: (data: ReadableStream, isBinary: boolean) => void, onmessage: (data: ReadableStream, isBinary: boolean) => void,
onerror: (message: string) => void, onerror: (message: string) => void,

View file

@ -44,6 +44,10 @@ export type C2SWSOpenPayload = {
protocols: string | string[]; protocols: string | string[];
}; };
export type S2CWSOpenPayload = {
protocol: string;
};
export type WSClosePayload = { export type WSClosePayload = {
code: number; code: number;
reason: string; reason: string;

View file

@ -11,6 +11,7 @@ import {
ProtoBareHeaders, ProtoBareHeaders,
S2CRequestType, S2CRequestType,
S2CRequestTypes, S2CRequestTypes,
S2CWSOpenPayload,
S2C_HELLO_ERR, S2C_HELLO_ERR,
S2C_HELLO_OK, S2C_HELLO_OK,
WSClosePayload, WSClosePayload,
@ -199,8 +200,8 @@ export class AdriftServer {
this._sendSimpleRes(seq, S2CRequestTypes.HTTPResponseEnd); this._sendSimpleRes(seq, S2CRequestTypes.HTTPResponseEnd);
} }
sendWSOpen(seq: number) { sendWSOpen(seq: number, payload: S2CWSOpenPayload) {
this._sendSimpleRes(seq, S2CRequestTypes.WSOpen); this._sendJSONRes(seq, S2CRequestTypes.WSOpen, payload);
} }
sendWSClose(seq: number, payload: WSClosePayload) { sendWSClose(seq: number, payload: WSClosePayload) {
@ -318,7 +319,7 @@ export class AdriftServer {
// onclose will be called after this with code 1006, reason "" and wasClean false // onclose will be called after this with code 1006, reason "" and wasClean false
}; };
ws.onopen = () => { ws.onopen = () => {
this.sendWSOpen(seq); this.sendWSOpen(seq, { protocol: ws.protocol });
}; };
ws.onclose = (e) => { ws.onclose = (e) => {
this.sendWSClose(seq, { this.sendWSClose(seq, {