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';