Guest User

TomcatWebSocketTest index.html

a guest
Jul 6th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.23 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <script type="text/javascript">
  4.             var TestWebSocket = {
  5.                 socket: null,
  6.                 connect: (function() {
  7.                     var host = 'ws://' + window.location.host + '/TomcatWebSocketTest/testWebSocketServlet';
  8.                     if ('WebSocket' in window) {
  9.                         TestWebSocket.socket = new WebSocket(host);
  10.                     } else if ('MozWebSocket' in window) {
  11.                         TestWebSocket.socket = new MozWebSocket(host);
  12.                     } else {
  13.                         alert('Error: WebSocket is not supported by this browser.');
  14.                         return;
  15.                     }
  16.                     // process message from server
  17.                     TestWebSocket.socket.onmessage = function (message) {
  18.                         document.getElementById('content').innerHTML = message.data;
  19.                     };
  20.                     /* optional
  21.                     // do something while onopen/onclose if needed
  22.                     TestWebSocket.socket.onopen = function () {
  23.                         alert('WebSocket opened.');
  24.                     };
  25.                     TestWebSocket.socket.onclose = function () {
  26.                         alert('WebSocket closed.');
  27.                     };
  28.                     */
  29.                 }),
  30.                 // send message to server
  31.                 trigger: (function() {
  32.                     TestWebSocket.socket.send('trigger');
  33.                 }),
  34.             };
  35.  
  36.             TestWebSocket.connect();
  37.         </script>
  38.     </head>
  39.     <body>
  40.         <div id="content"></div>
  41.         <button id="btn" onclick="TestWebSocket.trigger();">trigger</button>
  42.     </body>
  43. </html>
Advertisement
Add Comment
Please, Sign In to add comment