From d182825de6bda8fec8650e554184e693ad3f87ae Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 11 Jul 2021 17:44:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9B=91=E5=90=AC=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/api/socket.ts | 12 ++++++++++++ shared/utils/environment.ts | 2 ++ 2 files changed, 14 insertions(+) diff --git a/shared/api/socket.ts b/shared/api/socket.ts index 716fe3f9..05ad161c 100644 --- a/shared/api/socket.ts +++ b/shared/api/socket.ts @@ -1,6 +1,7 @@ import { io, Socket } from 'socket.io-client'; import _isNil from 'lodash/isNil'; import { getServiceUrl } from '../manager/service'; +import { isDevelopment } from '../utils/environment'; let socket: Socket; @@ -42,6 +43,10 @@ export class AppSocket { }); }); } + + listen(eventName: string, callback: (data: unknown) => void) { + this.socket.on(`notify:${eventName}`, callback); + } } /** @@ -60,6 +65,7 @@ export function createSocket(token: string): Promise { auth: { token, }, + forceNew: true, }); socket.once('connect', () => { // 连ζŽ₯成功 @@ -68,5 +74,11 @@ export function createSocket(token: string): Promise { socket.once('error', () => { reject(); }); + + if (isDevelopment) { + socket.onAny((...args) => { + console.log('Receive Notify:', args); + }); + } }); } diff --git a/shared/utils/environment.ts b/shared/utils/environment.ts index 1db842d7..1e3ee65c 100644 --- a/shared/utils/environment.ts +++ b/shared/utils/environment.ts @@ -1,3 +1,5 @@ export const isBrowser = typeof window !== 'undefined'; export const isNavigator = typeof navigator !== 'undefined'; + +export const isDevelopment = process.env.NODE_ENV === 'development';