function connect(port) { return new Promise(resolve => { const socket = net.createConnection(port, '127.0.0.1', () => { socket.write('GET http://127.0.0.1:8080/ HTTP', () => { socket.destroy(); resolve(); }); }); }); }
constructor(frontend) { process.title = 'ndb/ndd_service'; this._disconnectPromise = new Promise(resolve => process.once('disconnect', () => resolve(DebugState.PROCESS_DISCONNECT))); this._connected = new Set(); this._frontend = frontend; const pipePrefix = process.platform === 'win32' ? '\\\\.\\pipe\\' : os.tmpdir(); const pipeName = `node-ndb.${process.pid}.sock`; this._pipe = path.join(pipePrefix, pipeName); const server = net.createServer(socket => { socket.on('data', async d => { const runSession = await this._startSession(JSON.parse(d), frontend); socket.write('run'); runSession(); }); socket.on('error', e => caughtErrorDebug(e)); }).listen(this._pipe); server.unref(); }
const TIMEOUT = 30000; const socket = net.createConnection(process.env.NDD_IPC, () => { socket.write(process.env.NDD_PUBLISH_DATA); const timeoutId = setTimeout(() => socket.destroy(), TIMEOUT); socket.on('data', () => { }); socket.on('error', err => { process.stderr.write('\u001b[31mndb is not found:\u001b[0m\n'); process.stderr.write('please restart it and update env variables or unset NDD_IPC and NODE_OPTIONS.\n'); process.exit(0); });
send(msg: any) { if (this.state !== ST_INITED) { return; } if (msg instanceof Buffer) { // if encoded, send directly this.socket.stream.write(msg); } else { this.adaptor.publish(this, msg); } }
onend(chunk: Buffer) { if (chunk) { this._socket.write(chunk); } this.state = ST_CLOSED; this.reset(); this.emit('end'); }
/** * Sends a heartbeat ping to the device * @private */ async _sendPing() { debug(`Pinging ${this.device.ip}`); // Create byte buffer const buffer = this.device.parser.encode({ data: Buffer.allocUnsafe(0), commandByte: CommandType.HEART_BEAT, sequenceN: ++this._currentSequenceN }); // Send ping await this.client.write(buffer); }
/** * Sends a query to a device. Helper function * that connects to a device if necessary and * wraps the entire operation in a retry. * @private * @param {Buffer} buffer buffer of data * @returns {Promise<Any>} returned data for request */ _send(buffer) { // Make sure we're connected if (!this.isConnected()) { throw new Error('No connection has been made to the device.'); } // Retry up to 5 times return pRetry(() => { return new Promise((resolve, reject) => { try { // Send data this.client.write(buffer); // Add resolver function this._resolvers[this._currentSequenceN] = data => resolve(data); } catch (error) { reject(error); } }); }, {retries: 5}); }
}); client.write('foobar');
send(msg: any, options: { binary?: boolean }, cb?: (err ?: Error) => void) { this._socket.write(msg, options as string, cb); }
it('should handle socket error', async function() { const address = urlparse('bolt://127.0.0.1:' + port + '?serialization=hessian2', true); const clientSocket = net.connect(address.port, address.hostname); const socket = await awaitEvent(server, 'connection'); connection = new RpcConnection({ logger, socket, classMap }); await connection.ready(); mm(connection.logger, 'warn', msg => { assert(msg === '[RpcConnection] error occured on socket: %s, errName: %s, errMsg: %s'); }); clientSocket.write(Buffer.from('hello world++++++++++++++++++++++++++++++++')); await connection.await('close'); });
rtmpSendPingResponse(time) { let packet = RtmpPacket.create(); packet.header.fmt = RTMP_CHUNK_TYPE_0; packet.header.cid = RTMP_CHANNEL_PROTOCOL; packet.header.type = RTMP_TYPE_EVENT; packet.payload = Buffer.alloc(6); packet.header.length = packet.payload.length; packet.payload.writeUInt16BE(0x07); packet.payload.writeUInt32BE(time, 2); let chunks = this.rtmpChunksCreate(packet); this.socket.write(chunks); }
net.connect(port, hostname, function () { socket.write('HTTP/1.1 200 Connection Established\r\n\r\n'); proxySocket.write(head); proxySocket.pipe(socket); }).on('error', function (e) { log.error('proxy error', e.message); log.detail('proxy error', e.stack); socket.end(); }).on('data', function (data) { // console.log('proxy socker data:', hostname, port, data.toString()); // socket.write(data); })
setInterval(() => { if (!this.heartBeatLock) { // prettier-ignore this.socket.write(Buffer([0xda, 0xbb, 0xe2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x4e])); } }, 5000)
net.createServer(function(connection) { console.log('client connected'); connection.on('end', function() { console.log('客户端关闭连接'); }); connection.write('Hello World!\r\n'); connection.pipe(connection); })