var page = '<html><body><script>var ws;setTimeout(function(){';
page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 2000);';
page += '},2000);</script></body></html>';
var onPageRequest = function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(page);
};
var server = require('ws').createServer(onPageRequest);
server.listen(8000);
server.on("websocket", function(ws) {
var count = 0;
console.log("Connection upgraded");
ws.on('message',function(msg) {
console.log("Message received");
print("[WS] "+JSON.stringify(msg));
});
ws.on('close',function(msg) {
if (intr) clearInterval(intr);
});
var intr = setInterval(function() {
ws.send("Hello "+count++);
}, 1000);
});
and it works fine - I don't see any leak at all.
However, when I remove ws.on('close' and close the connection then it starts to leak memory.
So yeah, that's it. I reckon the connection is actually closing properly, we're just accepting writes on a closed connection when we should be erroring.
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.
I just tried this:
and it works fine - I don't see any leak at all.
However, when I remove
ws.on('close'
and close the connection then it starts to leak memory.So yeah, that's it. I reckon the connection is actually closing properly, we're just accepting
write
s on a closed connection when we should be erroring.I filed an issue: https://github.com/espruino/Espruino/issues/1220