codingOL

javascript for chat widget (chatfrontend.js, thsst2)

Jun 10th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() ) {
  2.     "use strict";
  3.  
  4.     var content = $( 'content' );
  5.     var input   = $( 'input' );
  6.     var status  = $( 'status' );
  7.  
  8.     var myColor = false;
  9.     var myName  = false;
  10.  
  11.     window.WebSocket = window.WebSocket || window.MozWebSocket;
  12.  
  13.     if(!window.WebSocket) {
  14.  
  15.         content.html($('<p>', {text: 'Your browser does not support WebSockets.'}) );
  16.         input.hide();
  17.         $('span').hide();
  18.         return;
  19.     }
  20.  
  21.     var connection = new WebSocket(''); // decide the Port with which to run the WebSocket
  22.  
  23.     // connection.onOpen = function() {}
  24.  
  25.     connection.onError = function(error) {
  26.         content.html($('<p>', {text: 'There was a problem with connecting.'}) );
  27.     }
  28.  
  29.     // I can't do this section until I get the JSON for chat messages on the server
  30.     // backend.
  31.     // connection.onMessage = function(message) {}
  32.  
  33.     // ENTER key = 13, send a msg when user hits enter
  34.     input.keydown(function(e) {
  35.         if(e.keyCode === 13) {
  36.  
  37.             var msg = $(this).val();
  38.             if(!msg) {
  39.                 return;
  40.             }
  41.  
  42.             connection.send(msg);
  43.             $(this).val('');
  44.             input.attr('disabled','disabled');
  45.  
  46.             if(myName === false) {
  47.                 myName = msg;
  48.             }
  49.         }
  50.     } );
  51.  
  52.     // Throw back a message if the server couldn't respond in X ms
  53.     setInterval(function() {
  54.         if(connection.readyState !== 1) {
  55.             status.exit('Error!');
  56.             input.attr('disabled','disabled').val('Communication with server lost');
  57.         }
  58.     }, 3000) // 1000ms = 1sec, changeable. Default = 3000ms or 3sec
  59.  
  60.     // Add message to screen
  61.     /*
  62.     function addMessage(author, message, color, dt) {
  63.  
  64.     }*/
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment