SanSYS

Untitled

Apr 29th, 2015
16,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5.     <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  6.     <style>
  7.         h3{
  8.             font-size:12px;
  9.             margin-bottom:3px;
  10.             margin-top:0px;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15.  
  16.     <input type="text" value="" id="txtMsg" />
  17.     <button id="btnSend">Send</button>
  18.  
  19.     <div id="divHistory">
  20.         <h3>&nbsp;</h3>
  21.     </div>
  22.  
  23.     <script type="text/javascript">
  24.         (function () {
  25.             'use strict';
  26.  
  27.             var socket = new WebSocket("ws://" + window.location.host + "/CustomWS.ashx"),
  28.                 log = function (text) {
  29.                     var d = new Date();
  30.  
  31.                     $('<h3>' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds() + ' - ' + text + '</h3>').insertBefore("#divHistory h3:first-child");
  32.                 };
  33.  
  34.             socket.addEventListener("open", function (evt) {
  35.                 log('Connection Opened with the Echo server.');
  36.             }, false);
  37.  
  38.             socket.addEventListener("message", function (evt) {
  39.                 log(evt.data);
  40.             }, false);
  41.  
  42.             socket.addEventListener("error", function (evt) {
  43.                 log('Unexpected Error.');
  44.             }, false);
  45.  
  46.  
  47.             $("#btnSend").click(function () {
  48.                 if (socket.readyState == WebSocket.OPEN) {
  49.                     socket.send($("#txtMsg").val());
  50.                 }
  51.                 else {
  52.                     log('The underlying connection is closed.');
  53.                 }
  54.             });
  55.         })();
  56.     </script>
  57. </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment