Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/connection/src/browser/ws-channel-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import ReconnectingWebSocket from 'reconnecting-websocket';
import { uuid } from '@opensumi/ide-core-common';
import { IReporterService, REPORT_NAME, UrlProvider } from '@opensumi/ide-core-common';

import { stringify, parse } from '../common/utils';
import { stringify, parse, WSCloseInfo, ConnectionInfo } from '../common/utils';
import { WSChannel, MessageString } from '../common/ws-channel';

// 前台链接管理类
export class WSChannelHandler {
public connection: WebSocket;
private channelMap: Map<number | string, WSChannel> = new Map();
private channelCloseEventMap: Map<number | string, WSCloseInfo> = new Map();
private logger = console;
public clientId: string;
private heartbeatMessageTimer: NodeJS.Timer | null;
Expand Down Expand Up @@ -79,7 +80,9 @@ export class WSChannelHandler {
if (this.channelMap.size) {
this.channelMap.forEach((channel) => {
channel.onOpen(() => {
this.reporterService && this.reporterService.point(REPORT_NAME.CHANNEL_RECONNECT);
const closeInfo = this.channelCloseEventMap.get(channel.id);
this.reporterService &&
this.reporterService.point(REPORT_NAME.CHANNEL_RECONNECT, REPORT_NAME.CHANNEL_RECONNECT, closeInfo);
this.logger && this.logger.log(`channel reconnect ${this.clientId}:${channel.channelPath}`);
});
channel.open(channel.channelPath);
Expand All @@ -91,6 +94,14 @@ export class WSChannelHandler {
});
}
});

this.connection.addEventListener('close', (event) => {
if (this.channelMap.size) {
this.channelMap.forEach((channel) => {
channel.close(event.code, event.reason);
});
}
});
});
}
private getChannelSend = (connection) => (content: string) => {
Expand All @@ -110,6 +121,14 @@ export class WSChannelHandler {
channel.onOpen(() => {
resolve(undefined);
});
channel.onClose((code: number, reason: string) => {
this.channelCloseEventMap.set(channelId, {
channelPath,
closeEvent: { code, reason },
connectInfo: (navigator as any).connection as ConnectionInfo,
});
this.logger.log('channel close: ', code, reason);
});
channel.open(channelPath);
});

Expand Down
13 changes: 13 additions & 0 deletions packages/connection/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export interface ICapturedMessage {
error?: any;
}

export interface ConnectionInfo {
type: string;
downlink: number;
uplink: number;
rtt: number;
}

export interface WSCloseInfo {
channelPath: string;
closeEvent: { code: number; reason: string };
connectInfo: ConnectionInfo;
}

export function stringify(obj: any): string {
return JSON.stringify(obj);
}
Expand Down