Thank you Gordon for your helpful reply, it (your example) still is not working and "hangs" the espruino. I even put the whole thing into an onInit function without success:
function onInit() {
var eth = require("WIZnet").connect();
eth.setIP({ ip : "192.168.0.10" });
var data = JSON.stringify([{
topic: 'espruino',
messages: ["199-hello\nldskjfds"]
}]);
var t = setInterval(function() {
console.log("Write should be done every 2 seconds");
//ws.send(data);
}, 2000);
var WebSocket = require("ws");
var ws;
setTimeout(function() {
ws = new WebSocket("192.168.0.6",{
port: 8080,
protocolVersion: 13,
origin: 'Espruino',
keepAlive: 30 // Ping Interval in seconds.
});
ws.on('open', function() {
console.log("Connected to server");
//Send message to server
ws.send(data);
console.log("Sent first time");
});
ws.on('message', function(msg) {
console.log("MSG: " + msg);
});
ws.on('close', function() {
console.log("Connection closed");
});
}, 10000); // I extended this to 1o secs to see what below setInterval function delivers
}
setInterval(function() { console.log("Halo"); }, 1000);
onInit();
The output is as follows:
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Connected to server
Sent first time
So as long as ws = new WebSocket("192.168.0.6",... is not executed times work well and the event loop runs, but after 10 Seconds ws is created the ws.on("open", ... is executed correctly but afterwards the espruino hangs and does not execute any more, as well as I have to plug usb out and in again to renew the script. To me it seems on ws.on("open",... the event loop gets blocked.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Thank you Gordon for your helpful reply, it (your example) still is not working and "hangs" the espruino. I even put the whole thing into an onInit function without success:
The output is as follows:
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Halo
Halo
Write should be done every 2 seconds
Connected to server
Sent first time
So as long as ws = new WebSocket("192.168.0.6",... is not executed times work well and the event loop runs, but after 10 Seconds ws is created the ws.on("open", ... is executed correctly but afterwards the espruino hangs and does not execute any more, as well as I have to plug usb out and in again to renew the script. To me it seems on ws.on("open",... the event loop gets blocked.
Another thing I encountered is on the general Info page (http://www.espruino.com/Notes) it says:
In my case I needed to run the function manually otherwise onInit did not get fired (see last line).
Thanks for your help!
Best regards Manuel