chore: network 初步完成

This commit is contained in:
手瓜一十雪
2024-08-11 00:24:00 +08:00
parent 1cbfccc4eb
commit 44a8c8e35d

View File

@@ -26,7 +26,6 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
} }
registerHeartBeat() { registerHeartBeat() {
// WS反向心跳
if (this.connection) { if (this.connection) {
this.heartbeatTimer = setInterval(() => { this.heartbeatTimer = setInterval(() => {
if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) { if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) {
@@ -42,8 +41,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
onEvent<T extends OB11EmitEventContent>(event: T) { onEvent<T extends OB11EmitEventContent>(event: T) {
if (this.connection) { if (this.connection) {
const wrappedEvent = this.wrapEvent(event); this.connection.send(JSON.stringify(event));
this.connection.send(JSON.stringify(wrappedEvent));
} }
} }
@@ -70,7 +68,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
} }
private async tryConnect() { private async tryConnect() {
while (!this.connection) { while (!this.connection && !this.isClosed) {
try { try {
this.connection = new NodeWebSocket(this.url); this.connection = new NodeWebSocket(this.url);
this.connection.on('message', (data) => { this.connection.on('message', (data) => {
@@ -102,12 +100,4 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
console.error('Failed to handle message:', e); console.error('Failed to handle message:', e);
} }
} }
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
// Wrap the event as needed
return {
type: 'event',
data: event
};
}
} }