summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/examples/websocket/websocket.html
blob: d2db565056e399ac250831a6884cee5c6b24e0f8 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
  <!--
  Copyright (c) 2012 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
  -->
<head>
  <title>WebSocket</title>

  <script type="text/javascript">
    websocket = null;  // Global application object.
    statusText = 'NO-STATUS';

    // Indicate success when the NaCl module has loaded.
    function moduleDidLoad() {
      websocket = document.getElementById('websocket');
      updateStatus('SUCCESS');
    }

    // Handle a message coming from the NaCl module.
    function handleMessage(message_event) {
      console.log(message_event);
      document.getElementById('log').value += message_event.data + '\n';
    }

    function pageDidLoad() {
      if (websocket == null) {
        updateStatus('LOADING...');
      } else {
        updateStatus();
      }
    }

    function doConnect() {
      // Send a request message. See also websocket.cc for the request format.
      websocket.postMessage('o;' + document.getElementById('url').value);
    }

    function doSend() {
      // Send a request message. See also websocket.cc for the request format.
      websocket.postMessage(
          's;' + document.getElementById('message').value);
    }

    function doClose() {
      // Send a request message. See also websocket.cc for the request format.
      websocket.postMessage('c;');
    }

    function updateStatus(opt_message) {
      if (opt_message)
        statusText = opt_message;
      var statusField = document.getElementById('statusField');
      if (statusField) {
        statusField.innerHTML = statusText;
      }
    }
  </script>
</head>
<body onload="pageDidLoad()">

<h1>WebSocket API Example</h1>
<p>
  <form action="#" onsubmit="doConnect(); return false;">
    <input type="text" id="url"
      value="ws://html5rocks.websocket.org/echo?encoding=text" />
    <input type="submit" value="Connect" />
  </form>

  <form action="#" onsubmit="doSend(); return false;">
    <input type="text" id="message" value="hello" />
    <input type="submit" value="Send" />
  </form>

  <form action="#" onsubmit="doClose(); return false;">
    <input type="submit" value="Close" />
  </form>

  <textarea id="log" rows="10" cols="80"></textarea>

  <div id="listener">
    <script type="text/javascript">
      var listener = document.getElementById('listener')
      listener.addEventListener('load', moduleDidLoad, true);
      listener.addEventListener('message', handleMessage, true);
    </script>

    <embed name="nacl_module"
           id="websocket"
           width=0 height=0
           src="websocket.nmf"
           type="application/x-nacl" />
  </div>
</p>

<p>Set a server URL, then push "Connect" button to establish a connection.</p>
<p>"Send" button sends text message on the left text area.</p>
<p>"Close" button closes the connection/</p>

<h2>Status</h2>
<div id="statusField">NO-STATUS</div>
</body>
</html>