Blame view

appWeb/testWebsockets.html 811 Bytes
ab869558   aknockae   La com série entr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  <!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
      <script src="js/jquery-3.2.1.min.js"></script>
      <script type="text/javascript">
  window.WebSocket=(window.WebSocket||window.MozWebSocket);
  
  var websocket=new WebSocket('ws://192.168.1.111:9000','myprotocol');
  
  websocket.onopen=function(){ $('h1').css('color','green'); };
  
  websocket.onerror=function(){ $('h1').css('color','red'); };
  
  websocket.onmessage=function(message){
  console.log(message.data);
  $('#messages').append($('<p>',{ text: message.data }));
  };
  
  function sendMessage(){
  websocket.send($('#message').val());
  $('#message').val('');
  }
      </script>
    </head>
    <body>
      <h1>WebSockets test</h1>
      <input type="text" id="message"/>
      <button onclick="sendMessage();">Send</button>
      <div id="messages"></div>
    </body>
  </html>