Advertisement
Guest User

Client exemple in HTML/JS

a guest
Jun 2nd, 2014
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.06 KB | None | 0 0
  1. <html>
  2. <head><title>Web Socket Test</title></head>
  3. <body>
  4. <script>
  5.     var socket;
  6.     if (window.WebSocket) {
  7.         socket = new WebSocket("ws://localhost:9090/");
  8.         socket.onmessage = function(event) {
  9.             alert("Received data from websocket: " + event.data);
  10.         }
  11.         socket.onopen = function(event) {
  12.             alert("Web Socket opened!");
  13.         };
  14.         socket.onclose = function(event) {
  15.             alert("Web Socket closed.");
  16.         };
  17.     } else {
  18.         alert("Your browser does not support Websockets. (Use Chrome)");
  19.     }
  20.  
  21.     function send(message) {
  22.         if (!window.WebSocket) {
  23.             return;
  24.         }
  25.         if (socket.readyState == WebSocket.OPEN) {
  26.             socket.send(message);
  27.         } else {
  28.             alert("The socket is not open.");
  29.         }
  30.     }
  31. </script>
  32. <form onsubmit="return false;">
  33.     <input type="text" name="message" value="Hello, World!"/>
  34.     <input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)"/>
  35. </form>
  36. </body>
  37. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement