If you try to post and then process the reply too fast, the header information winds up in the message from the client to the server.
Are Websockets faster?
To find out I went to the Espruino site to try a Websocket example https://www.espruino.com/ws
In the Websocket server example code:
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!"); }, 1000);';
page += '},1000);</script></body></html>';
This is the HTML code sent to the client. Note the line page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
Console.log is not a valid way to display a message in HTML/javascript.
The example works but nothing appears on the client screen.
A better example is in the attached file websockserve1a.js
Websockets seem to be faster
Reworked the HTML file into the attached file flight5a.html to use Websockets. It is incorporated into the server code as a string using the file converter site at https://www.espruino.com/File+Converter
The attached file websocketserver2.js contains the server code using Websockets.
Bugs
The airplane flies and flies but eventually stops communicating.
Using the debugger on the client side the following two errors were occurring
ws.onmessage=function(event){
var S=JSON.parse(event.data);
(index):1 Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at WebSocket.ws.onmessage ((index):214)
VM44:37 WebSocket connection to 'ws://192.168.1.6:8000/my_websocket' failed: A server must not mask any frames that it sends to the client
The JSON.parse bug has been fixed.
The file trycatch.js shows how this was done in the client code. The messages sometime come in garbled and the JSONparse throws an error. The code catches the error and discards the message.
ws.onmessage=function(event){
try{
var S=JSON.parse(event.data);
}
catch(e){return;}
CalStat=S.st;
The other bug I have no clue. Helpful suggestions are welcome.
Is this project the start of a 3D mouse?
The POST method fails with 500 ms sample intervals.
The Websockets method works well at 300ms and maybe 200ms.
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.
The HTML POST problem
If you try to post and then process the reply too fast, the header information winds up in the message from the client to the server.
Are Websockets faster?
To find out I went to the Espruino site to try a Websocket example
https://www.espruino.com/ws
In the Websocket server example code:
This is the HTML code sent to the client. Note the line page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
Console.log is not a valid way to display a message in HTML/javascript.
The example works but nothing appears on the client screen.
A better example is in the attached file websockserve1a.js
Websockets seem to be faster
Reworked the HTML file into the attached file flight5a.html to use Websockets. It is incorporated into the server code as a string using the file converter site at
https://www.espruino.com/File+Converter
The attached file websocketserver2.js contains the server code using Websockets.
Bugs
The airplane flies and flies but eventually stops communicating.
Using the debugger on the client side the following two errors were occurring
The JSON.parse bug has been fixed.
The file trycatch.js shows how this was done in the client code. The messages sometime come in garbled and the JSONparse throws an error. The code catches the error and discards the message.
The other bug I have no clue. Helpful suggestions are welcome.
Is this project the start of a 3D mouse?
The POST method fails with 500 ms sample intervals.
The Websockets method works well at 300ms and maybe 200ms.
4 Attachments