Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function() ) {
- "use strict";
- var content = $( 'content' );
- var input = $( 'input' );
- var status = $( 'status' );
- var myColor = false;
- var myName = false;
- window.WebSocket = window.WebSocket || window.MozWebSocket;
- if(!window.WebSocket) {
- content.html($('<p>', {text: 'Your browser does not support WebSockets.'}) );
- input.hide();
- $('span').hide();
- return;
- }
- var connection = new WebSocket(''); // decide the Port with which to run the WebSocket
- // connection.onOpen = function() {}
- connection.onError = function(error) {
- content.html($('<p>', {text: 'There was a problem with connecting.'}) );
- }
- // I can't do this section until I get the JSON for chat messages on the server
- // backend.
- // connection.onMessage = function(message) {}
- // ENTER key = 13, send a msg when user hits enter
- input.keydown(function(e) {
- if(e.keyCode === 13) {
- var msg = $(this).val();
- if(!msg) {
- return;
- }
- connection.send(msg);
- $(this).val('');
- input.attr('disabled','disabled');
- if(myName === false) {
- myName = msg;
- }
- }
- } );
- // Throw back a message if the server couldn't respond in X ms
- setInterval(function() {
- if(connection.readyState !== 1) {
- status.exit('Error!');
- input.attr('disabled','disabled').val('Communication with server lost');
- }
- }, 3000) // 1000ms = 1sec, changeable. Default = 3000ms or 3sec
- // Add message to screen
- /*
- function addMessage(author, message, color, dt) {
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment