Skip to content

Commit

Permalink
remove socket.connectionStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong committed May 13, 2024
1 parent 538c4dc commit d9d5d1b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
17 changes: 8 additions & 9 deletions packages/base/src/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ export abstract class SocketStoreBase {
// initial retry interval.
private retryInterval = INIT_RETRY_INTERVAL;

// indicated connected whether or not
public connectionStatus: boolean = false;

// response message filters, to handle some wired messages
public static messageFilters: ((data: any) => any)[] = [];

Expand Down Expand Up @@ -174,9 +171,11 @@ export abstract class SocketStoreBase {
this.handleMessage(evt);
});
this.socketWrapper?.onClose(() => {
psLog.unproxy.log('onClose');
this.connectOffline();
});
this.socketWrapper?.onError(() => {
psLog.unproxy.log('onError');
// we treat on error the same with on close.
this.connectOffline();
});
Expand Down Expand Up @@ -221,16 +220,16 @@ export abstract class SocketStoreBase {
}

private connectOnline() {
this.connectionStatus = true;
this.retryInterval = INIT_RETRY_INTERVAL;
this.updateRoomInfo();
this.ping();
}

private connectOffline() {
this.connectionStatus = false;
psLog.unproxy.log('connectOffline');
this.socketConnection = null;
this.debuggerConnection = null;

this.clearPing();
if (this.retryTimer) {
clearTimeout(this.retryTimer);
Expand Down Expand Up @@ -343,7 +342,9 @@ export abstract class SocketStoreBase {
// if (result.content.code === 'RoomNotFoundError') {
// // Room not exist, might because used an old connection.
// }
this.connectOffline();
psLog.unproxy.log('触发错误事件', type);
this.socketWrapper.close();
// this.connectOffline();
break;
/* c8 ignore start */
case PONG:
Expand Down Expand Up @@ -461,9 +462,7 @@ export abstract class SocketStoreBase {
this.socketWrapper?.send(dataString);
} catch (e) {
psLog.error(`Incompatible: ${(e as Error).message}`);
if (this.connectionStatus) {
this.connectOffline();
}
this.connectOffline();
}
/* c8 ignore stop */
}
Expand Down
5 changes: 4 additions & 1 deletion packages/page-spy-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ class PageSpy {
// reconnect when page switch to front-ground.
document.addEventListener('visibilitychange', () => {
// For browser, if the connection exist, no need to recreate.
if (!document.hidden && !socketStore.connectionStatus) {
if (
!document.hidden &&
socketStore.getSocket().getState() !== SocketState.OPEN
) {
this.useOldConnection();
}
});
Expand Down

0 comments on commit d9d5d1b

Please sign in to comment.