當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。