I was trying to make a Read\Write function for websocket but i’ve a problem…
var inarrivo = 0;
var risposta = "";
function RDW_Command(Stringa) {
var Risposta = "";
Stringa = "$" + Stringa;
socket.send(Stringa);
inarrivo = 0;
while (inarrivo == 0) {
doNothing();
}
return risposta;
}
function doNothing() {}
socket.onmessage = function (msg) {
risposta = msg.data;
inarrivo = 1;
};
The problem is that it freeze when it enter in the while loop….
Any idea to fix it? >.< thank you!!
Andrea
You program websockets in javascript in the usual (in javascript) event/callback based way.
Here’s an example :
You call
somePackage.connectto open the connection.After that, the message arrivals are handled in the
onmessagehandler and to send a message to the server you just callsomePackage.send.Even if this is full of asynchronous calls, that doesn’t mean that your program won’t immediately react to the arrival of a message (as soon as your other functions have stopped working because you have only one thread).