/** * Send concatenated data to StatsD server via UDP * * @param {Buffer} buf */ send(buf) { //this.logger.info("Buffer\n" + buf.toString()); const sock = dgram.createSocket("udp4"); sock.send(buf, 0, buf.length, this.opts.port, this.opts.host, (err, bytes) => { if (err) { this.logger.warn("Unable to send metrics to StatsD server. Error:" + err.message, err); } else { this.logger.debug("Metrics are uploaded to StatsD. Sent bytes:", bytes); } sock.close(); }); }
stop(force: boolean, cb: () => void) { this.socket.close(); process.nextTick(cb); }
removeUdpSubscriber(connId){ delete this._udpSubClients[connId] if(Object.keys(this._udpSubClients).length === 0){ this.udpSocket.close(); } }
//UDP CLIENT SERVER server.on('error', (err) => { console.log(`server error:\n${err.stack}`); server.close(); });
sendAndClose(callback) { this.callback = callback || this._defaultCallback; this._debug('Sending last messages and closing...'); this._popMsgsAndSend(); clearTimeout(this.timer); if (this.protocol === 'udp') { this.udpClient.close(); } }
// Start an UDP server to mock calls. const startUdpServer = async function() { const requests = [] const server = createSocket('udp4', buffer => { requests.push(buffer.toString()) }) await promisify(server.bind.bind(server))(0) const { address, port } = server.address() const stopServer = promisify(server.close.bind(server)) return { host: address, port, requests, stopServer } }
destroy() { this.walkTimeout && clearTimeout(this.walkTimeout) this.joinInterval && clearInterval(this.joinInterval) this.udp.close() }
close() { // clearing the timer allows the node event loop to quit when needed clearTimeout(this.timer); // send pending messages, if any if (this.messages.length > 0) { this._debug('Closing, purging messages.'); this._popMsgsAndSend(); } if (this.protocol === 'udp') { this.udpClient.close(); } // no more logging allowed this.closed = true; }
client.send(message, 0, message.length, 12345, "localhost", function (err, bytes) { client.close(); });
close() { this.udf.close(); this.udf = null; clearInterval(this.chunkInterval); }
this.wifiServerUDP.on('error', (err) => { if (this.options.verbose) console.log(`server error:\n${err.stack}`); this.wifiServerUDP.close(); });
end() { this.socket.close(); }
//CLIENT SERVER server.on('error', (err) => { console.log(`server error:\n${err.stack}`); server.close(); });
server.on("error", function (err) { console.log("server error:\n" + err.stack); server.close(); });