当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Duplex.once方法代码示例

本文整理汇总了TypeScript中stream.Duplex.once方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Duplex.once方法的具体用法?TypeScript Duplex.once怎么用?TypeScript Duplex.once使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stream.Duplex的用法示例。


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

示例1: connect

  /**
   * Starts the connection establishment to the proxy and destination.
   * @param existing_socket Connected socket to use instead of creating a new one (internal use).
   */
  public connect(existing_socket?: Duplex) {
    this._onDataReceived = (data: Buffer) => this.onDataReceived(data);
    this._onClose = () => this.onClose();
    this._onError = (err: Error) => this.onError(err);
    this._onConnect = () => this.onConnect();

    // Start timeout timer (defaults to 30 seconds)
    const timer = setTimeout(
      () => this.onEstablishedTimeout(),
      this._options.timeout || DEFAULT_TIMEOUT
    );

    // check whether unref is available as it differs from browser to NodeJS (#33)
    if (timer.unref && typeof timer.unref === 'function') {
      timer.unref();
    }

    // If an existing socket is provided, use it to negotiate SOCKS handshake. Otherwise create a new Socket.
    if (existing_socket) {
      this._socket = existing_socket;
    } else {
      this._socket = new net.Socket();
    }

    // Attach Socket error handlers.
    this._socket.once('close', this._onClose);
    this._socket.once('error', this._onError);
    this._socket.once('connect', this._onConnect);
    this._socket.on('data', this._onDataReceived);

    this.state = SocksClientState.Connecting;
    this._receiveBuffer = new ReceiveBuffer();

    if (existing_socket) {
      this._socket.emit('connect');
    } else {
      (this._socket as net.Socket).connect(
        this._options.proxy.port,
        this._options.proxy.host || this._options.proxy.ipaddress
      );
      if (
        this._options.set_tcp_nodelay !== undefined &&
        this._options.set_tcp_nodelay !== null
      ) {
        (this._socket as net.Socket).setNoDelay(
          !!this._options.set_tcp_nodelay
        );
      }
    }

    // Listen for established event so we can re-emit any excess data received during handshakes.
    this.prependOnceListener('established', info => {
      setImmediate(() => {
        if (this._receiveBuffer.length > 0) {
          const excessData = this._receiveBuffer.get(
            this._receiveBuffer.length
          );

          info.socket.emit('data', excessData);
        }
        info.socket.resume();
      });
    });
  }
开发者ID:JoshGlazebrook,项目名称:socks,代码行数:68,代码来源:socksclient.ts


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