index.html 2.06 KB
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="jquery.js"></script>
    <script type="text/javascript">
window.WebSocket=(window.WebSocket||window.MozWebSocket);

var websocket=new WebSocket('ws://127.0.0.1:9000','myprotocol');

websocket.onopen=function(){ $('h1').css('color','green'); };

websocket.onerror=function(){ $('h1').css('color','red'); };

websocket.onmessage=function(message){
console.log(message.data);
var recep = message.data;
var instruction = recep-10*Math.trunc(recep/10);
var donnee = Math.trunc(recep/10);
if (instruction == 0){
$('#messages').html(donnee);
}
else if (instruction == 1){
$('#messages2').html(donnee);
}
else if (instruction == 2){
var temperatureC = Math.round(((donnee*5/1024)-0.5)*10000) / 100
$('#messages3').html(temperatureC);
}
else if (instruction == 3){
	if(recep==3){
	$('#messages4').html("Light OFF");
	}
	else{
	$('#messages4').html("Light ON");
	}
}
else if (instruction == 4){
	if(recep==4){
	$('#messages5').html("HEAT OFF");
	}
	else{
	$('#messages5').html("HEAT ON");
	}
}
/*else if (instruction == 4){
$('#messages5').html(donnee);
}
*/
};

function sendmessage(){
websocket.send($('#message').val());
$('#message').val('');
}





function autoSend0(){
websocket.send('0');
setTimeout('autoSend1();', 200);
}
function autoSend1(){
websocket.send('1');
setTimeout('autoSend2();', 200);
}
function autoSend2(){
websocket.send('2');
setTimeout('autoSend3();', 200);
}
function autoSend3(){
websocket.send('3');
setTimeout('autoSend4();', 200);
}
function autoSend4(){
websocket.send('4');
//setTimeout('autoSend0();', 500);
}
setInterval(autoSend0, 1000);




    </script>
  </head>
  <body>
    <h1>WebSockets test</h1>
    <input type="text" id="message"/>
    <button onclick="sendmessage();">Send</button>
    <h2>Potentiometre :</h2>
    <h2><span id="messages"></span></h2>
    <h2>Luminosite :</h2>
    <h2><span id="messages2"></span></h2>
    <h2>Temperature :</h2>
    <h2><span id="messages3"></span></h2>
    <h2><span id="messages4"></span></h2>
    <h2><span id="messages5"></span></h2>
  </body>
</html>