It's a NodeMCU. I think it may have been that I had a persistent wifi connection with wifi.save() at the time. Anyway I've stripped that out so wifi connection is established at code send, but I'm still having issues - not related to ports but with the websocket module in general.
I worked it into my code last night and found it would not work in Chrome. In Firefox, the server can send, but the client cannot send back - or at least the message is never received.
I've stripped my code back to basically just run the server example on the websockets page and still no joy.
In chrome, nothing from the server and I get this in console.
WebSocket connection to 'ws://192.168.1.16:8000/my_websocket' failed: A server must not mask any frames that it sends to the client.
And an attempt to send with ws.send("...") gets this in response:-
WebSocket is already in CLOSING or CLOSED state.
In Firefox I get the server sent message MSG:Hello from Espruino!
But nothing back from the client. Also trying ws.send("...") from console also does not get received.
The connection appears to get upgraded based on my logging, but the ws.on('message', function(){...}) event handler never fires.
This is the code, almost identical to the example
var wifi = require("Wifi");
var ap2 = {
"ssid": "xxxx",
"pwd": "xxxx"
};
// Wifi connection
wifi.connect(ap2.ssid, {"password": ap2.pwd}, function(err){
if(!err){
console.log("Connected to Wifi");
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) {
console.log("Connection upgraded");
ws.on('message',function(msg) {
console.log("Message received");
print("[WS] "+JSON.stringify(msg));
});
ws.send("Hello from Espruino!");
});
}
});
Could there be a problem with the module? I'm struggling to see how I could be going wrong?
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.
It's a NodeMCU. I think it may have been that I had a persistent wifi connection with
wifi.save()
at the time. Anyway I've stripped that out so wifi connection is established at code send, but I'm still having issues - not related to ports but with the websocket module in general.I worked it into my code last night and found it would not work in Chrome. In Firefox, the server can send, but the client cannot send back - or at least the message is never received.
I've stripped my code back to basically just run the server example on the websockets page and still no joy.
In chrome, nothing from the server and I get this in console.
And an attempt to send with
ws.send("...")
gets this in response:-In Firefox I get the server sent message
MSG:Hello from Espruino!
But nothing back from the client. Also trying
ws.send("...")
from console also does not get received.The connection appears to get upgraded based on my logging, but the
ws.on('message', function(){...})
event handler never fires.This is the code, almost identical to the example
Could there be a problem with the module? I'm struggling to see how I could be going wrong?