mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Added the required jsvLockAgain(parent). The tests/test_dgram_socket.js now also works directly in node.jsl (tested in v6.9.4).
42 lines
981 B
JavaScript
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');
|