Permalink
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (33 sloc) 981 Bytes
// Socket server and client test
var result = 0;
var port = 41234;
let dgram = require('dgram');
let srv = dgram.createSocket('udp4');
srv = srv.bind(port, function() {
srv.on('message', function(msg, info) {
console.log("<"+JSON.stringify(msg));
console.log("<"+JSON.stringify(info));
srv.send(msg+'!', info.port, info.address);
});
});
srv.on('close', function() {
console.log('server disconnected');
});
let client = dgram.createSocket('udp4');
client.on('message', function(msg, info) {
console.log(">"+JSON.stringify(msg));
console.log(">"+JSON.stringify(info));
result = msg=="42!" && info.address=="127.0.0.1" && info.port==port;
clearTimeout(failTimeout); // stop the fail fast
srv.close();
client.close();
});
client.on('close', function() {
console.log('client disconnected');
});
// fail the test fast if broken
failTimeout = setTimeout(function() {
client.close();
srv.close();
}, 100);
client.send('42', port, 'localhost');