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(
remote,
protocols,
() => {
(protocol: string) => {
onReadyState(WebSocket.OPEN);
(ws as any).protocol = protocol;
ws.dispatchEvent(new Event("open"));
},
(code: number, reason: string, wasClean: boolean) => {

View file

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

View file

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

View file

@ -11,6 +11,7 @@ import {
ProtoBareHeaders,
S2CRequestType,
S2CRequestTypes,
S2CWSOpenPayload,
S2C_HELLO_ERR,
S2C_HELLO_OK,
WSClosePayload,
@ -199,8 +200,8 @@ export class AdriftServer {
this._sendSimpleRes(seq, S2CRequestTypes.HTTPResponseEnd);
}
sendWSOpen(seq: number) {
this._sendSimpleRes(seq, S2CRequestTypes.WSOpen);
sendWSOpen(seq: number, payload: S2CWSOpenPayload) {
this._sendJSONRes(seq, S2CRequestTypes.WSOpen, payload);
}
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
};
ws.onopen = () => {
this.sendWSOpen(seq);
this.sendWSOpen(seq, { protocol: ws.protocol });
};
ws.onclose = (e) => {
this.sendWSClose(seq, {