Re: Button vs Websocket: Round 1
Posted: Thu Nov 16, 2017 7:57 am
Thanks for putting up with me, i begin to realize that i basically dance around the same question in different threads. If we ignore the "wrongness" of the handlePhysicalButton() in the loop for a second, in the output you quoted can you think of any reason those messages should not reappear in the console when i do a site reload (thus calling start(), creating a new websocket connection and listen, intercept and print those messages)? I'm asking because before continuing i would like to understand the 50:50 chance of it looping (and therefore all hardwarebutton feedback). And my understanding on creating/closing the websocket connection from the browser hinges on that.
On the javascript side I created a check for any possible readystate of the websocket connection with a callback to wait and a open/close for the websocket and put some printouts in the console. That's a bit overkill, but at least it kinda works. It still doesn't lift the mystery of the handlePhysicalButton() loop playing hard to get.
On the javascript side I created a check for any possible readystate of the websocket connection with a callback to wait and a open/close for the websocket and put some printouts in the console. That's a bit overkill, but at least it kinda works. It still doesn't lift the mystery of the handlePhysicalButton() loop playing hard to get.
Code: Select all
function sendMessage(msg){
// Wait until the state of the socket is not ready and send the message when it is...
waitForSocket(websock, function(){
console.log("sendMsg(): (" + msg + ")");
websock.send(msg);
});
}
// Make the function wait until the connection is made...
function waitForSocket(websock, callback){
setTimeout(
function() {
switch(websock.readyState) {
case 0:
console.log("websock.readyState = " + websock.readyState + " - The connection is not yet open")
console.log("Waiting for connection...")
waitForSocketConnection(websock, callback);
break;
case 1:
console.log("Case 1: websock.readyState = " + websock.readyState + " - The connection is open and ready to communicate")
console.log("Connection is made")
if(callback != null){
callback();
}
break;
case 2:
console.log("websock.readyState = " + websock.readyState + " - The connection is in the process of closing.")
console.log("Waiting for connection...")
waitForSocketConnection(websock, callback);
break;
case 3:
console.log("websock.readyState = " + websock.readyState + " - The connection is closed or couldn't be opened.")
console.log("Restarting Websocket...")
start();
break;
default:
console.log("¯\\_(ツ)_/¯");
}
}, 1000);