feat(sdk): accept reverse WebSocket connections

Refs #617
This commit is contained in:
DrSmoothl committed 2026-11-01 23:26:50 +08:00
1 parent 5f1700fc1b
commit 0fa912a32e
14 files changed
+1405 -11

No files matched your search

+26 -9
View File
@@ -414,6 +414,16 @@ export class SnowLumaWebSocketClient extends SnowLumaApiClient {
appendAccessToken(this.url, this.accessToken),
this.protocols,
);
return this.attachSocket(socket);
}
/** Shared protocol session for outgoing and already accepted connections. */
protected attachSocket(socket: WebSocketLike): Promise<void> {
if (this.connection) {
throw new SnowLumaConnectionError('A WebSocket is already attached');
}
this.closedByUser = false;
let resolveOpening!: () => void;
let rejectOpening!: (error: Error) => void;
const opened = new Promise<void>((resolve, reject) => {
@@ -430,16 +440,19 @@ export class SnowLumaWebSocketClient extends SnowLumaApiClient {
},
};
this.connection = connection;
listeners.push(
addSocketListener(socket, 'open', () => {
if (this.connection !== connection) {
return;
}
this.reconnectAttempts = 0;
resolveOpening();
this.emitter.emit('open', undefined);
}),
const openedSocket = (): void => {
if (this.connection !== connection) {
return;
}
this.reconnectAttempts = 0;
resolveOpening();
this.emitter.emit('open', undefined);
};
listeners.push(
addSocketListener(socket, 'open', openedSocket),
addSocketListener(socket, 'message', (...args) => {
if (this.connection === connection) {
this.handleMessage(args);
@@ -461,6 +474,10 @@ export class SnowLumaWebSocketClient extends SnowLumaApiClient {
}),
);
if (socket.readyState === WEBSOCKET_OPEN) {
openedSocket();
}
return opened;
}