Guest User

Untitled

a guest
Nov 30th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.04 KB | None | 0 0
  1. <style type="text/css">
  2.     td
  3.     {
  4.         font-family: verdana;
  5.         font-size: 10pt;
  6.         border: 1px solid black;
  7.         padding: 8px;
  8.         background-color: #EEE;
  9.     }
  10. </style>
  11. <script type="text/javascript">
  12.     var last;
  13.    
  14.     function init()
  15.     {
  16.         websocket = new WebSocket("ws://localhost/chat");
  17.        
  18.         websocket.onopen    = function(data) { onOpen(data);    };
  19.         websocket.onclose   = function(data) { onClose(data);   };
  20.         websocket.onmessage = function(data) { onMessage(data); };
  21.         websocket.onerror   = function(data) { onError(data);   };
  22.     }
  23.    
  24.     function onOpen(data)
  25.     {
  26.         addLine("SERVER","Connected.");
  27.     }
  28.    
  29.     function onClose(data)
  30.     {
  31.         addLine("SERVER","Disconnected.");
  32.     }
  33.    
  34.     function onMessage(data)
  35.     {
  36.         data = data.data;
  37.        
  38.         switch(data)
  39.         {
  40.             case "\x02":
  41.                 sendMessage(last);
  42.                 break;
  43.            
  44.             case "\x03":
  45.                 input  = document.getElementById("input");
  46.                 button = document.getElementById("button");
  47.                
  48.                 input.value     = "";
  49.                 input.disabled  = false;
  50.                 button.disabled = false;
  51.                
  52.                 break;
  53.            
  54.             default:
  55.                 pos = data.indexOf(" ");
  56.                 addLine(data.substring(0,pos),data.substring(pos,data.length));
  57.                 break;
  58.         }
  59.     }
  60.    
  61.     function onError(data)
  62.     {
  63.         addLine("ERROR",data.data);
  64.     }
  65.    
  66.     function sendMessage(data)
  67.     {
  68.         websocket.send(data);
  69.         last = data;
  70.     }
  71.    
  72.     function send()
  73.     {
  74.         input  = document.getElementById("input");
  75.         button = document.getElementById("button");
  76.        
  77.         sendMessage(input.value);
  78.        
  79.         input.disabled  = true;
  80.         button.disabled = true;
  81.     }
  82.    
  83.     function addLine(user,line)
  84.     {
  85.         log = document.getElementById("log");
  86.         tr  = document.createElement("tr");
  87.         td1 = document.createElement("td");
  88.         td2 = document.createElement("td");
  89.        
  90.         td1.innerHTML = "<b>" + user + "</b>";
  91.         td2.innerHTML = line;
  92.        
  93.         tr.appendChild(td1);
  94.         tr.appendChild(td2);
  95.         log.appendChild(tr);
  96.     }
  97. </script>
  98. <body onLoad="init()">
  99.     <table id="log" cellspacing="8" cellpadding="0">
  100.     </table>
  101.     <input id="input" type="text"/>
  102.     <button id="button" onClick="send()">Send</button>
  103. </body>
Advertisement
Add Comment
Please, Sign In to add comment