Blame view

index.html 1.3 KB
aa84636b   aunterei   first commit
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
33
34
35
36
37
38
  <!DOCTYPE html>
  <html>
      <head>
          <meta charset="utf-8">
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
          <script type="text/javascript">
              $(function() {
                  window.WebSocket = window.WebSocket || window.MozWebSocket;
                  var websocket = new WebSocket('ws://127.0.0.1:9000',
                                                'dumb-increment-protocol');
                  websocket.onopen = function () {
                      $('h1').css('color', 'green');
                  };
                  websocket.onerror = function () {
                      $('h1').css('color', 'red');
                  };
                  websocket.onmessage = function (message) {
                      console.log(message.data);
                      $('div').append($('<p>', { text: message.data }));
                  };
                  
                  $('input').click(function(e) {
                      e.preventDefault();
                      websocket.send($('input').val());
                      $('input').val('');
                  });
              });
          </script>
          </head>
      <body>
          <h1>WebSockets test</h1>
          <form>
              <input type="text" />
              <button>Send</button>
          </form>
          <div></div>
      </body>
  </html>