websocket Inverisfy express实用程序未连接到Web Socket socket.io服务器

mf98qq94  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(256)
import 'module-alias/register';
import 'reflect-metadata';
import sourceMapSupport from 'source-map-support';
import { LIB_ENV_CONFIG, logger } from '@sn/shared';
import { AppServer, IExpressMiddleware, InversifyFactory } from '@sn/server';
import { AsyncContainerModule } from 'inversify';
import { customMiddleware } from './shared/middleware/AppMiddleware';
import swaggerMiddleware from './shared/middleware/Swagger';
import { AppModule } from '@ioc/IocConfig';
import { Server, Socket } from 'socket.io';
import * as http from 'http';

sourceMapSupport.install();

const expressApp = (async (): Promise<void> => {
  try {
    logger.pushContext(LIB_ENV_CONFIG.LIB_SERVICE_NAME);
    const serviceGlobalCustomMiddlewares: IExpressMiddleware[] = [
      customMiddleware,
      swaggerMiddleware
    ];
    const containers: AsyncContainerModule[] = await InversifyFactory.create(AppModule);
    const app = new AppServer(containers, serviceGlobalCustomMiddlewares);
    await app.startServer();

    const expressServerInstance = app.getServer();
    const httpServer = http.createServer(expressServerInstance);
    const io: Server = new Server(httpServer);

    io.on('connection', (socket: Socket) => {
      console.log('a user connected');

      socket.on('disconnect', () => {
        console.log('user disconnected');
      });

      socket.on('chat message', (msg: string) => {
        io.emit('chat message', msg);
      });
    });
    io.on('listening', () => {
      console.log('WebSocket server is listening for connections');
    });
  } catch (err) {
    logger.error(`Server setup error in apps/${LIB_ENV_CONFIG.LIB_SERVICE_NAME}`, err);
  }
})();

expressApp;

字符串
这是我如何设置inversify express服务器的

import 'reflect-metadata';
/* eslint-disable  @typescript-eslint/no-explicit-any */
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('source-map-support').install();
import * as dotenv from 'dotenv';
import { InversifyExpressServer } from 'inversify-express-utils';
import { ExpressMiddleware, errorMiddleware } from './../middleware';
import { LIB_ENV_CONFIG, logger } from '@sn/shared';
import { AsyncContainerModule, Container } from 'inversify';
import { IExpressMiddleware } from './../interface/IMiddleware';
import { HealthCheckRouter } from './HealthCheckRouter';

dotenv.config();

export class AppServer {
  private readonly servicePort = LIB_ENV_CONFIG.LIB_PORT;
  private readonly serviceName = LIB_ENV_CONFIG.LIB_SERVICE_NAME;
  private readonly serviceUrl = LIB_ENV_CONFIG.LIB_SERVICE_URL;
  private readonly serviceApiVersionUri = LIB_ENV_CONFIG.LIB_CLIENT_API_VERSION;
  private readonly serviceBaseUrl = LIB_ENV_CONFIG.LIB_SERVICE_BASE_URL;
  private readonly serviceEnv = LIB_ENV_CONFIG.LIB_NODE_ENV || 'development';
  private rootPath = this.serviceBaseUrl + this.serviceApiVersionUri;

  private server: InversifyExpressServer;
  private serverInstance;
  constructor(
    serviceInversifyContainers: AsyncContainerModule[],
    serviceGlobalCustomMiddlewares: IExpressMiddleware[]
  ) {
    this.loadInversifyServer(serviceInversifyContainers);
    this.loadLibMiddlewares(serviceGlobalCustomMiddlewares);
    this.loadErrorMiddleware();
  }

  private loadInversifyServer = (serviceInversifyContainers: AsyncContainerModule[]) => {
    const container = new Container();

    HealthCheckRouter;
    serviceInversifyContainers.forEach((moduleConfigContainer: any) => {
      container.loadAsync(moduleConfigContainer);
    });

    this.server = new InversifyExpressServer(container, null, {
      rootPath: this.rootPath
    });
  };

  private loadLibMiddlewares = (serviceGlobalCustomMiddlewares: IExpressMiddleware[]) => {
    this.server.setConfig((app) => {
      logger.info(`LOADING LIBRARY LEVEL MIDDLEWARES ...........`);
      new ExpressMiddleware(app);
      serviceGlobalCustomMiddlewares.forEach((serviceMiddleware) => {
        serviceMiddleware(app);
      });
    });
  };

  private loadErrorMiddleware = () => {
    this.server.setErrorConfig(errorMiddleware);
  };

  startServer = async () => {
    const serverInstance = this.server.build();
    this.serverInstance = serverInstance;
    serverInstance.listen(this.servicePort, () => {
      logger.info(
        `FOR SERVICE: [ ${this.serviceName} ] -> Server running at ${this.serviceUrl}:${this.servicePort}${this.rootPath} -> on ${this.serviceEnv} environment`
      );
    });
  };

  getServer = () => {
    return this.serverInstance;
  };
}


Express服务器工作正常,但无法设置socket.io连接
服务器构建中没有错误,但连接不起作用
网址:127.0.0.1:3008/web-socket-service/api/v1/socket.io?EIO=3&transport=websocket
这是断开连接错误

zbdgwd5y

zbdgwd5y1#

它工作,需要添加一些变化
应用服务器正在使用基本路径,必须将该基本路径添加到socket.io命名空间
另外,不是将express请求处理函数传递给服务器示例this.serverInstance = serverInstance;
我们需要通过服务器

startServer = async () => {
    const serverInstance = this.server.build();
    this.serverInstance = serverInstance.listen(this.servicePort, () => {
      logger.info(
        `FOR SERVICE: [ ${this.serviceName} ] -> Server running at ${this.serviceUrl}:${this.servicePort}${this.rootPath} -> on ${this.serviceEnv} environment`
      );
    });
  };

字符串

相关问题