summaryrefslogtreecommitdiffstats
path: root/src/civetweb/examples/_obsolete/websocket/websock.htm
blob: 4ff3a5faea4ced8f221e9a8831492fafccd655c1 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Test</title>
  <script type='text/javascript' language="javascript">
  <!--
  var connection;
  var keepAlive = false;

  function webSockKeepAlive() {
    if (keepAlive) {
      connection.send('ping'); // Send the message 'ping' to the server
      setTimeout("webSockKeepAlive()", 10000);
    }
  }

  function load() {
    connection = new WebSocket("ws://127.0.0.1/MyWebSock");

    connection.onopen = function () {
        var send = "init " + Math.round(Math.random()*4294967294+1);
        console.log('Client: ' + send);
        connection.send(send);
        keepAlive = true;
        webSockKeepAlive();
      };

    connection.onerror = function (error) {
        keepAlive = false;
        connection.close();
        console.log('WebSocket error: ' + error);
        alert("WebSocket error");
      };

    connection.onmessage = function (e) {
        console.log('Server: ' + e.data);
        if (e.data.substring(0,5) == "title") {window.document.title = e.data.substring(6);}
        else if (e.data.substring(0,3) == "msg") {
          var msgStr = document.getElementById('msg');
          msgStr.innerHTML = msgStr.innerHTML + e.data.substring(4);
        }        
      };
  }
  //-->
  </script>

</head>
<body onload="load()">
  <input type="button" onclick="connection.send('msg A');" value="A"></button>
  <input type="button" onclick="connection.send('msg B');" value="B"></button>
  <input type="button" onclick="connection.send('msg C');" value="C"></button>
  <input type="button" onclick="connection.send('msg D');" value="D"></button>
  <b id="msg"></b>
</body>
</html>