handleConnect: async()

in sdk/webpubsub-socketio-extension/src/EIO/components/web-pubsub-connection-manager.ts [73:125]


      handleConnect: async (req, res) => {
        let timeout: NodeJS.Timeout;
        let cleanup: (error: string) => void;
        try {
          const connectionId = req.context.connectionId;
          debug(`onConnect, connectionId = ${connectionId}`);

          cleanup = (error: string): void => {
            if (this._clientConnections.has(connectionId)) {
              this._clientConnections.delete(connectionId);
            }
            if (this._candidateSids.lastIndexOf(connectionId) === this._candidateSids.length - 1) {
              this._candidateSids.shift();
            }
            const connectionError = {
              req: req,
              code: CONNECTION_ERROR_WEBPUBSUB_CODE,
              message: CONNECTION_ERROR_WEBPUBSUB_MESSAGE,
              context: error,
            } as ConnectionError;
            this.eioServer.emit(CONNECTION_ERROR_EVENT_NAME, connectionError);
          };

          const context = new ClientConnectionContext(this.service, connectionId, res, cleanup);

          /**
           * Two conditions lead to returning reponse for connect event:
           *   1. The connection is accepted or refused by EIO Server and the corresponding events are triggered.
           *   2. Exception is thrown in following code
           * As a defensive measure, a timeout is set to return response in 30000ms in case of both conditions don't happen.
           */
          timeout = setTimeout(() => {
            if (!context.connectResponded) {
              const error = `EIO server cannot handle connect request with error: Timeout 30000ms`;
              cleanup(error);
              res.fail(500, error);
            }
          }, 30000);

          const connectReq = this.getEioHandshakeRequest(req, context);

          this._candidateSids.push(connectionId);
          this._clientConnections.set(connectionId, context);

          await this.eioServer.onConnect(connectionId, connectReq, context);
        } catch (error) {
          debug(`onConnect, req = ${req}, err = ${error}`);
          const errorMessage = `EIO server cannot handle connect request with error: ${error}`;
          clearTimeout(timeout);
          cleanup(errorMessage);
          res.fail(500, errorMessage);
        }
      },