• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript socket.io.Socket类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中socket.io.Socket的典型用法代码示例。如果您正苦于以下问题:TypeScript io.Socket类的具体用法?TypeScript io.Socket怎么用?TypeScript io.Socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了io.Socket类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1:

 this._io.on("connection", (socket: Socket) => {
     socket.on("register", (req: IRegisterRequest) => {
         this.emitEvents(this._sessionManager.registerClient(socket, req));
     });
     socket.on("roomList", () => {
         this.emitEvents(this._sessionManager.sendLobby(socket));
     });
     socket.on("joinRoom", (req: IJoinRoomRequest) => {
         this.emitEvents(this._sessionManager.joinRoom(socket, req));
     });
     socket.on("leaveRoom", (req: ILeaveRoomRequest) => {
         this.emitEvents(this._sessionManager.leaveRoom(socket, req));
     });
     socket.on("startGame", (req: IStartGameRequest) => {
         this.emitEvents(this._sessionManager.startGame(socket, req));
     });
     socket.on("placeTile", (req: IPlaceTileRequest) => {
         this.emitEvents(this._sessionManager.placeTile(socket, req));
     });
     socket.on("roomMessage", (req: IRoomMessageRequest) => {
         this.emitEvents(this._sessionManager.roomMessage(socket, req));
     });
     socket.on("roomStats", (req: IRoomStatsRequest) => {
         this.emitEvents(this._sessionManager.sendRoomStats(socket, req));
     });
     socket.on("userStats", (req: IUserStatsRequest) => {
         this.emitEvents(this._sessionManager.sendUserStats(socket, req));
     });
     socket.on("globalStats", (async (req: IGlobalStatsRequest) => {
         this.emitEvents(await this._sessionManager.sendGlobalStats(socket, req));
     }));
 });
开发者ID:Parakoopa,项目名称:WhoSquares,代码行数:32,代码来源:RequestManager.ts


示例2:

        this.socket.on('connection', (client: Socket) => {
            this.bus.addClient(client)

            client.on('api.subscription', (subscription: Subscription) => {
                this.bus.subscribe(client.id, subscription)
            })
            client.on('api.unsubscription', (subscription: Subscription) => {
                this.bus.unsubscribe(client.id, subscription.id)
            })
            client.on('disconnect', () => {
                this.bus.removeClient(client.id)
            })
        })
开发者ID:plouc,项目名称:mozaik,代码行数:13,代码来源:mozaik.ts


示例3: Error

  io.on('connection', (socket: Socket) => {
    const openWatchers = new Map<string, WatcherAndSubscribeHook>();

    const unsubscribeWatcher = ({
      watcher,
      subscribeHook,
    }: WatcherAndSubscribeHook) => {
      watcher.stop();
      if (subscribeHook) {
        subscribeHook.unsubscribed(socket.user);
      }
    };

    socket.on('disconnect', () => {
      openWatchers.forEach(unsubscribeWatcher);
    });

    socket.on(
      'resource',
      (
        { type, resourceName, json }: EventResourceParams,
        callback: Callback,
      ): void => {
        try {
          const value = json && decode(json);

          switch (type) {
            case 'cursor toArray': {
              const resource = resourcesService.getCursorResource(resourceName);
              resourcesService
                .createCursor(resource, socket.user, value)
                .then((cursor) => cursor.toArray())
                .then((results) => callback(null, encode(results)))
                .catch((err) => {
                  logger.error(type, err);
                  callback(err.message);
                });
              break;
            }

            case 'fetch':
            case 'subscribe':
            case 'fetchAndSubscribe':
              try {
                const resource = resourcesService.getServiceResource(
                  resourceName,
                );
                logger.info('resource', { type, resourceName, value });

                const [key, params, eventName] = value;

                if (!key.startsWith('query')) {
                  throw new Error('Invalid query key');
                }

                const query = resource.queries[key](params, socket.user);

                if (type === 'fetch') {
                  query
                    .fetch((result: any) =>
                      callback(null, result && encode(result)),
                    )
                    .catch((err: any) => {
                      logger.error(type, { err });
                      callback(err.message || err);
                    });
                } else {
                  const watcherKey = `${resourceName}__${key}`;
                  if (openWatchers.has(watcherKey)) {
                    logger.warn(
                      'Already have a watcher for this key. Cannot add a new one',
                      { watcherKey, key },
                    );
                    callback(
                      'Already have a watcher for this key. Cannot add a new one',
                    );
                    return;
                  }
                  const watcher = query[type](
                    (err: Error | null, result: any) => {
                      if (err) {
                        logger.error(type, { err });
                      }

                      socket.emit(eventName, err, result && encode(result));
                    },
                  );

                  watcher.then(
                    () => callback(null),
                    (err: Error) => {
                      logger.error(type, { err });
                      callback(err.message);
                    },
                  );

                  const subscribeHook =
                    resource.subscribeHooks && resource.subscribeHooks[key];
                  openWatchers.set(watcherKey, { watcher, subscribeHook });
                  if (subscribeHook) {
//.........这里部分代码省略.........
开发者ID:liwijs,项目名称:liwi,代码行数:101,代码来源:index.ts


示例4: encode

                    (err: Error | null, result: any) => {
                      if (err) {
                        logger.error(type, { err });
                      }

                      socket.emit(eventName, err, result && encode(result));
                    },
开发者ID:liwijs,项目名称:liwi,代码行数:7,代码来源:index.ts



注:本文中的socket.io.Socket类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript socket.io.default类代码示例发布时间:2022-05-25
下一篇:
TypeScript socket.io-parser.Decoder类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap