import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, HTTP_SERVER_KEY, MidwayInvokeForbiddenError, DecoratorManager, MetadataManager, MidwayTraceService, } from '@midwayjs/core'; import { debuglog } from 'util'; const debug = debuglog('midway:socket.io'); import { Application, IMidwaySocketIOOptions, Context, NextFunction, } from './interface'; import { Server } from 'socket.io'; import { WS_CONTROLLER_KEY, WS_EVENT_KEY, WSControllerOption, WSEventInfo, WSEventTypeEnum, Framework, } from '@midwayjs/core'; import { getFreePort } from './utils'; @Framework() export class MidwaySocketIOFramework extends BaseFramework< Application, Context, IMidwaySocketIOOptions > { private namespaceList = []; protected connectionMiddlewareManager = this.createMiddlewareManager(); protected socketServerPort: number; configure(): IMidwaySocketIOOptions { return this.configService.getConfiguration('socketIO'); } async applicationInitialize() { this.app = new Server(this.configurationOptions) as Application; this.defineApplicationProperties({ useConnectionMiddleware: ( middleware: CommonMiddlewareUnion ) => { return this.useConnectionMiddleware(middleware); }, getConnectionMiddleware: (): ContextMiddlewareManager< Context, NextFunction, undefined > => { return this.getConnectionMiddleware(); }, }); } public async run(): Promise { await this.loadMidwayController(); if (this.configurationOptions.adapter) { this.app.adapter(this.configurationOptions.adapter); this.logger.debug('[midway:socketio] init socket.io-redis ready!'); } // listen port when http server not exist if (typeof this.configurationOptions.port === 'number') { let customPort: number = this.configurationOptions.port; if (customPort === 0) { customPort = await getFreePort(); this.logger.info( `[midway:socketio] server has auto-assigned port ${customPort}` ); } this.app.listen(customPort, this.configurationOptions); this.socketServerPort = customPort; this.logger.info( `[midway:socketio] Socket.io server port = ${customPort} start success` ); } else if (this.applicationContext.hasObject(HTTP_SERVER_KEY)) { this.app.attach( this.applicationContext.get(HTTP_SERVER_KEY), this.configurationOptions ); this.logger.info( '[midway:socketio] Socket.io server start success and attach to web server' ); } } public getSocketServerPort(): number { return this.socketServerPort; } protected async beforeStop(): Promise { return new Promise(resolve => { this.app.close(() => { setTimeout(() => { resolve(); }, 1000); }); }); } private async loadMidwayController() { // create room const controllerModules = DecoratorManager.listModule(WS_CONTROLLER_KEY); for (const module of controllerModules) { await this.addNamespace(module); } } private async addNamespace(target: any) { const traceService = this.applicationContext.get(MidwayTraceService); const traceMetaResolver = (this.configurationOptions as any)?.tracing?.meta; const traceEnabled = (this.configurationOptions as any)?.tracing?.enable !== false; const traceExtractor = (this.configurationOptions as any)?.tracing ?.extractor; const controllerOption: WSControllerOption = MetadataManager.getOwnMetadata( WS_CONTROLLER_KEY, target ); const nsp = this.app.of(controllerOption.namespace); this.namespaceList.push(controllerOption.namespace); const controllerMiddleware = controllerOption.routerOptions.middleware ?? []; const controllerConnectionMiddleware = controllerOption.routerOptions.connectionMiddleware ?? []; nsp.use((socket: any, next) => { const entryCarrierDefault = socket?.handshake?.headers ?? {}; const entryCarrier = typeof traceExtractor === 'function' ? traceExtractor({ ctx: socket, carrier: entryCarrierDefault, request: socket?.handshake, custom: { namespace: controllerOption.namespace || '/', eventName: 'connect', }, }) : entryCarrierDefault; traceService .runWithEntrySpan( `socketio.connect ${controllerOption.namespace || '/'}`, { enable: traceEnabled, carrier: entryCarrier ?? entryCarrierDefault, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.namespace': controllerOption.namespace || '/', }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier: entryCarrier ?? entryCarrierDefault, custom: { namespace: controllerOption.namespace || '/', eventName: 'connect', }, }, }, async () => { this.app.createAnonymousContext(socket); socket.requestContext.registerObject('socket', socket); socket.app = this.app; next(); } ) .catch(err => next(err)); }); // `connect`事件应该使用**同步**的方式调用以保证后续事件监听能够正常得到处理 nsp.on('connect', (socket: Context) => { // 异步执行全局和Controller级的连接中间件 let connMiddlewarePromise = this.middlewareService .compose( [ ...this.connectionMiddlewareManager, ...controllerConnectionMiddleware, ], this.app ) .then(connectFn => connectFn(socket)); const wsEventInfos: WSEventInfo[] = MetadataManager.getMetadata( WS_EVENT_KEY, target ); // 存储方法对应的响应处理 const methodMap = {}; // 优先处理`@OnWSConnection` const wsOnConnectionEventInfos = wsEventInfos.filter( wsEventInfo => wsEventInfo.eventType === WSEventTypeEnum.ON_CONNECTION ); if (wsOnConnectionEventInfos.length) { // `@OnWSConnection`应该在之前的连接中间件执行完成后继续 connMiddlewarePromise = connMiddlewarePromise.then(() => // 可能存在多个`@OnWSConnection`,这时不保障它们的执行顺序 Promise.all( wsOnConnectionEventInfos.map(async wsEventInfo => { methodMap[wsEventInfo.propertyName] = methodMap[ wsEventInfo.propertyName ] || { responseEvents: [] }; const controller = await socket.requestContext.getAsync(target); try { const onConnectionCarrierDefault = socket?.handshake?.headers ?? {}; const onConnectionCarrier = typeof traceExtractor === 'function' ? traceExtractor({ ctx: socket, carrier: onConnectionCarrierDefault, custom: { eventName: wsEventInfo.propertyName, }, }) : onConnectionCarrierDefault; const result = await traceService.runWithEntrySpan( `socketio.event ${wsEventInfo.propertyName}`, { enable: traceEnabled, carrier: onConnectionCarrier ?? onConnectionCarrierDefault, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.event': wsEventInfo.propertyName, }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier: onConnectionCarrier ?? onConnectionCarrierDefault, custom: { eventName: wsEventInfo.propertyName, }, }, }, async () => { const fn = await this.middlewareService.compose( [ ...(wsEventInfo?.eventOptions?.middleware || []), async (ctx, next) => { return controller[wsEventInfo.propertyName].apply( controller, [socket] ); }, ], this.app ); return await fn(socket); } ); await this.bindSocketResponse( result, socket, wsEventInfo.propertyName, methodMap ); } catch (err) { this.logger.error(err); } }) ) ); } // 监听和处理其它非`@OnWSConnection`的逻辑 const wsRestEventInfos = wsEventInfos.filter( wsEventInfo => wsEventInfo.eventType !== WSEventTypeEnum.ON_CONNECTION ); if (wsRestEventInfos.length) { for (const wsEventInfo of wsRestEventInfos) { methodMap[wsEventInfo.propertyName] = methodMap[ wsEventInfo.propertyName ] || { responseEvents: [] }; if (wsEventInfo.eventType === WSEventTypeEnum.ON_MESSAGE) { // on user custom event socket.on(wsEventInfo.messageEventName, async (...args) => { // 需要等待所有连接中间件和@OnWSConnection执行完成后再触发@OnWSMessage await connMiddlewarePromise; const controller = await socket.requestContext.getAsync(target); debug('got message', wsEventInfo.messageEventName, args); try { const onMessageCarrierDefault = socket?.handshake?.headers ?? {}; const onMessageCarrier = typeof traceExtractor === 'function' ? traceExtractor({ ctx: socket, carrier: onMessageCarrierDefault, custom: { eventName: wsEventInfo.messageEventName, }, }) : onMessageCarrierDefault; const result = await traceService.runWithEntrySpan( `socketio.message ${wsEventInfo.messageEventName}`, { enable: traceEnabled, carrier: onMessageCarrier ?? onMessageCarrierDefault, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.event': wsEventInfo.messageEventName, }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier: onMessageCarrier ?? onMessageCarrierDefault, custom: { eventName: wsEventInfo.messageEventName, }, }, }, async () => { return await ( await this.applyMiddleware(async (ctx, next) => { // add controller middleware const fn = await this.middlewareService.compose( [ ...controllerMiddleware, ...(wsEventInfo?.eventOptions?.middleware || []), async (ctx, next) => { const isPassed = await this.app .getFramework() .runGuard( ctx, target, wsEventInfo.propertyName ); if (!isPassed) { throw new MidwayInvokeForbiddenError( wsEventInfo.propertyName, target ); } // eslint-disable-next-line prefer-spread return controller[wsEventInfo.propertyName].apply( controller, args ); }, ], this.app ); return await fn(ctx, next); }) )(socket); } ); if (typeof args[args.length - 1] === 'function') { // ack args[args.length - 1](result); } else { // emit await this.bindSocketResponse( result, socket, wsEventInfo.propertyName, methodMap ); } } catch (error) { this.logger.error(error); } }); } else if ( wsEventInfo.eventType === WSEventTypeEnum.ON_DISCONNECTION ) { // on socket disconnect socket.on('disconnect', async (reason: string) => { const controller = await socket.requestContext.getAsync(target); try { const onDisconnectCarrierDefault = socket?.handshake?.headers ?? {}; const onDisconnectCarrier = typeof traceExtractor === 'function' ? traceExtractor({ ctx: socket, carrier: onDisconnectCarrierDefault, custom: { eventName: 'disconnect', }, }) : onDisconnectCarrierDefault; const result = await traceService.runWithEntrySpan( `socketio.disconnect ${wsEventInfo.propertyName}`, { enable: traceEnabled, carrier: onDisconnectCarrier ?? onDisconnectCarrierDefault, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.event': 'disconnect', }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier: onDisconnectCarrier ?? onDisconnectCarrierDefault, custom: { eventName: 'disconnect', }, }, }, async () => await controller[wsEventInfo.propertyName].apply( controller, [reason] ) ); await this.bindSocketResponse( result, socket, wsEventInfo.propertyName, methodMap ); } catch (err) { this.logger.error(err); } }); } else { // 存储每个方法对应的后置响应处理,供后续快速匹配 methodMap[wsEventInfo.propertyName].responseEvents.push( wsEventInfo ); } } } }); if (nsp.adapter && nsp.adapter.on) { nsp.adapter.on('error', err => { this.logger.error(err); }); } nsp.on('error', err => { this.logger.error(err); }); } private async bindSocketResponse( result: any, socket: Context, propertyName: string, methodMap: { responseEvents?: WSEventInfo[]; } ) { const traceService = this.applicationContext.get(MidwayTraceService); const traceMetaResolver = (this.configurationOptions as any)?.tracing?.meta; const traceEnabled = (this.configurationOptions as any)?.tracing?.enable !== false; const traceInjector = (this.configurationOptions as any)?.tracing?.injector; if (result && methodMap[propertyName]) { for (const wsEventInfo of methodMap[propertyName].responseEvents) { if (wsEventInfo.eventType === WSEventTypeEnum.EMIT) { if (wsEventInfo.roomName.length) { socket = wsEventInfo.roomName.reduce((socket, name) => { return socket.to(name); }, socket); } const carrier = typeof traceInjector === 'function' ? traceInjector({ ctx: socket, request: result, custom: { eventName: wsEventInfo.messageEventName, }, }) || {} : {}; await traceService.runWithExitSpan( `socketio.emit ${wsEventInfo.messageEventName}`, { enable: traceEnabled, carrier, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.event': wsEventInfo.messageEventName, }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier, custom: { eventName: wsEventInfo.messageEventName, }, }, }, async () => { // eslint-disable-next-line prefer-spread socket.emit.apply( socket, [wsEventInfo.messageEventName].concat(result) ); } ); } else if (wsEventInfo.eventType === WSEventTypeEnum.BROADCAST) { const carrier = typeof traceInjector === 'function' ? traceInjector({ ctx: socket, request: result, custom: { eventName: propertyName, }, }) || {} : {}; await traceService.runWithExitSpan( `socketio.broadcast ${propertyName}`, { enable: traceEnabled, carrier, attributes: { 'midway.protocol': 'socketio', 'midway.socketio.event': propertyName, }, meta: traceMetaResolver, metaArgs: { ctx: socket, carrier, custom: { eventName: propertyName, }, }, }, async () => { // eslint-disable-next-line prefer-spread socket.nsp.emit.apply(socket.nsp, [].concat(result)); } ); } } } } public getFrameworkName() { return 'socketIO'; } public useConnectionMiddleware( middleware: CommonMiddlewareUnion ) { this.connectionMiddlewareManager.insertLast(middleware); } public getConnectionMiddleware(): ContextMiddlewareManager< Context, NextFunction, undefined > { return this.connectionMiddlewareManager; } }