本文整理匯總了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);
});
}
示例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);
}
}
示例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);
}
});
}
}
示例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;
}
});
}
}
示例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();
});
};
示例6: function
request.on("socket", function (socket: Socket) {
socket.setTimeout(60 * 1000, () => {
callback(new Error("Request timed out"))
request.abort()
})
})