0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-dgram-connect-send-empty-packet.js
Santiago Gimeno 9e960175d1 dgram: add support for UDP connected sockets
Added the `dgram.connect()` and `dgram.disconnect()` methods that
associate/disassociate a udp socket to/from a remote address.
It optimizes for cases where lots of packets are sent to the same
address.
Also added the `dgram.remoteAddress()` method to retrieve the associated
remote address.

PR-URL: https://github.com/nodejs/node/pull/26871
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2019-04-03 06:48:13 +02:00

29 lines
821 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
client.bind(0, common.mustCall(function() {
client.connect(client.address().port, common.mustCall(() => {
client.on('message', common.mustCall(callback));
const buf = Buffer.alloc(1);
const interval = setInterval(function() {
client.send(buf, 0, 0, common.mustCall(callback));
}, 10);
function callback(firstArg) {
// If client.send() callback, firstArg should be null.
// If client.on('message') listener, firstArg should be a 0-length buffer.
if (firstArg instanceof Buffer) {
assert.strictEqual(firstArg.length, 0);
clearInterval(interval);
client.close();
}
}
}));
}));