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


TypeScript Socket.setTimeout方法代码示例

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


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

示例1: guessIdleTimeout

/*
If we connect to the machine, send one initial query and then stop, how long
can we idle until the machine terminates our connection?

Testing shows that this doesn't seem to be one consistent timeout, and that
instead it's however long it takes until the wifi flakes out a little bit.
*/
function guessIdleTimeout() {
  log.info('Estimating idle time...');
  let start = Date.now();

  let sock = new Socket();
  sock.setTimeout(10000);
  killOnProcessExit(sock);
  sock.on('data', function (data) {
    log.inbound(escapeUnprintables(data));
    if (String(data) === '*HELLO*') {
      setTimeout(function () {
        let msg = Checksum.attach('r00000001');
        log.outbound(msg);
        sock.write(msg);
      }, 100);
    }
  });
  sock.on('error', err => {
    let elapsed = Date.now() - start;
    log.info('elapsed', elapsed, 'ms');
    log.error(err);
    process.exit(1);
  });
  log.info('Connecting to %s:%s ...', DEST_HOST, DEST_PORT);
  sock.connect(DEST_PORT, DEST_HOST, function () {
    log.info('Connected');
    setInterval(() => {
      let elapsed = Date.now() - start;
      log.info('elapsed', elapsed, 'ms');
    }, 10000);
  });
}
开发者ID:jffry,项目名称:rocket-r60v,代码行数:39,代码来源:measure.ts

示例2: createClientSocket

function createClientSocket(endpointUrl: string): Socket {
    // create a socket based on Url
    const ep = parseEndpointUrl(endpointUrl);
    const port = parseInt( ep.port!, 10);
    const hostname = ep.hostname!;
    let socket: Socket;
    switch (ep.protocol) {
        case "opc.tcp:":

            socket = createConnection({ host: hostname, port });

            // Setting true for noDelay will immediately fire off data each time socket.write() is called.
            socket.setNoDelay(true);

            socket.setTimeout(0);

            socket.on("timeout", () => {
                debugLog("Socket has timed out");
            });

            return socket;
        case "fake:":
            socket = getFakeTransport();
            assert(ep.protocol === "fake:", " Unsupported transport protocol");
            process.nextTick(() => socket.emit("connect"));
            return socket;

        case "websocket:":
        case "http:":
        case "https:FF":
        default:
            throw new Error("this transport protocol is currently not supported :" + ep.protocol);

    }
}
开发者ID:node-opcua,项目名称:node-opcua,代码行数:35,代码来源:client_tcp_transport.ts

示例3: memoryMap

function memoryMap() {
  log.info('Reading memory...');

  //build up the socket
  let sock = new Socket();
  sock.setTimeout(10000);
  killOnProcessExit(sock);
  sock.on('error', err => {
    log.error(err);
    process.exit(1);
  });
  log.info('Connecting to %s:%s ...', DEST_HOST, DEST_PORT);

  sock.connect(DEST_PORT, DEST_HOST, function () {
    log.info('Connected');
  });

  //wait for first *HELLO* before starting the polling
  sock.once('data', function(data) {
    if (data) data = String(data);
    log.inbound(data);
    setTimeout(pollingLoop, 150);
  });
  //polling loop

  //store succesfully-read values here
  let values:(number|null)[] = new Array(0xFFFF);
  for (let i = 0; i < values.length; i++) {
    values[i] = 0;
  }
  let offset = 0x00;
  const startTime = Date.now();
  function pollingLoop() {
    readByte(sock, offset, (readable:boolean, value?:number) => {
      log.normal('byte', to4hex(offset), readable ? STATUS_OK : STATUS_ERR, value);
      //forecast ETA
      let elapsed = Date.now() - startTime;
      let total = elapsed * (0xffff / (offset + 1));
      let remain = total - elapsed;
      log.info('progress: elapsed', Math.round(elapsed/1000), 'sec; estimated remain', Math.round(remain / 1000), 'sec');
      //write to file... occasionally
      values[offset] = readable ? value : null;
      if (offset % 0x100 === 0) {
        log.info('SAVING RESULTS TO DISK');
        fs.writeFileSync('memscan.json', JSON.stringify(values));
      }
      //next
      offset += 1;
      if (offset <= 0xffff) {
        setTimeout(pollingLoop, 150);
      } else {
        log.info('SAVING RESULTS TO DISK');
        fs.writeFileSync('memscan.json', JSON.stringify(values));
        process.exit(0);
      }
    });
  }
}
开发者ID:jffry,项目名称:rocket-r60v,代码行数:58,代码来源:measure.ts

示例4: _install_socket

    /**
     * @method _install_socket
     * @param socket {Socket}
     * @protected
     */
    protected _install_socket(socket: Socket) {

        assert(socket);
        this._socket = socket;
        if (doDebug) {
            debugLog("_install_socket ", this.name);
        }

        // install packet assembler ...
        this.packetAssembler = new PacketAssembler({
            readMessageFunc: readRawMessageHeader,

            minimumSizeInBytes: this.headerSize
        });

        if (!this.packetAssembler) {
            throw new Error("Internal Error");
        }
        this.packetAssembler.on("message", (messageChunk: Buffer) => this._on_message_received(messageChunk));

        this._socket
          .on("data", (data: Buffer) => this._on_socket_data(data))
          .on("close", (hadError) => this._on_socket_close(hadError))
          .on("end", (err: Error) => this._on_socket_end(err))
          .on("error", (err: Error) => this._on_socket_error(err));

        const doDestroyOnTimeout = false;
        if (doDestroyOnTimeout) {
            // set socket timeout
            debugLog("setting _socket.setTimeout to ", this.timeout);
            this._socket.setTimeout(this.timeout, () => {
                debugLog(` _socket ${this.name} has timed out (timeout = ${this.timeout})`);
                if (this._socket) {
                    this._socket.destroy();
                    // 08/2008 shall we do this ?
                    this._socket.removeAllListeners();
                    this._socket = null;
                }
            });
        }
    }
开发者ID:node-opcua,项目名称:node-opcua,代码行数:46,代码来源:tcp_transport.ts

示例5: setTimeout

    const connect = () => {
      attempt++;

      const socket = new net.Socket();
      socket.once('error', () => {
        if (attempt < maxAttempts) {
          setTimeout(connect, retryIntervalMs);
        } else {
          reject(new errors.ServerUnreachable());
        }
      });

      if (timeout > 0) {
        socket.setTimeout(timeout);
      }

      socket.connect({host, port}, () => {
        socket.end();
        fulfill();
      });
    };
开发者ID:hlyu368,项目名称:outline-client,代码行数:21,代码来源:connectivity.ts

示例6: function

 request.on("socket", function (socket: Socket) {
   socket.setTimeout(60 * 1000, () => {
     callback(new Error("Request timed out"))
     request.abort()
   })
 })
开发者ID:MatthijsvandenBosch,项目名称:electron-builder,代码行数:6,代码来源:httpRequest.ts


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