當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript socket.io.Socket類代碼示例

本文整理匯總了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;未經允許,請勿轉載。