Espruino/tests/test_dgram_socket.js
Standa Opichal c3667d8b34 dgramSocket.bind() returns the dgramSocket instance
Added the required jsvLockAgain(parent).

The tests/test_dgram_socket.js now also works directly
in node.jsl (tested in v6.9.4).
2017-11-17 15:35:19 +01:00

42 lines
981 B
JavaScript

// 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');