Skip to content

Instantly share code, notes, and snippets.

@jfarcand
Created March 26, 2014 17:13
Show Gist options
  • Save jfarcand/9788354 to your computer and use it in GitHub Desktop.
Save jfarcand/9788354 to your computer and use it in GitHub Desktop.
(function () {
"use strict";
var content = $('#content');
var input = $('#input');
var status = $('#status');
var myName = false;
var author = null;
var logged = false;
var socket = $.atmosphere;
var request = { url: document.location.toString() + 'meteor',
contentType : "application/json",
logLevel : 'debug',
transport : 'streaming' ,
reconnectInterval : 5000,
trackMessageLength : true,
fallbackTransport: 'long-polling'};
request.onOpen = function(response) {
content.html($('<p>', { text: 'Atmosphere connected using ' + response.transport }));
input.removeAttr('disabled').focus();
status.text('Choose name:');
};
request.onMessage = function (response) {
input.removeAttr('disabled').focus();
var message = response.responseBody;
content.append(message.substring(0,1));
};
request.onError = function(response) {
content.html($('<p>', { text: 'Sorry, but there\'s some problem with your '
+ 'socket or the server is down' }));
};
request.onClose = function(response) {
logged = false;
}
var subSocket = socket.subscribe(request);
input.keydown(function(e) {
if (e.keyCode === 13) {
var msg = $(this).val();
// First message is always the author's name
if (author == null) {
author = msg;
}
subSocket.push(jQuery.stringifyJSON({ author: author, message: msg }));
$(this).val('');
input.attr('disabled', 'disabled');
if (myName === false) {
myName = msg;
}
}
});
function addMessage(author, message, color, datetime) {
content.append('<p><span style="color:' + color + '">' + author + '</span> @ ' +
+ (datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours()) + ':'
+ (datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes())
+ ': ' + message + '</p>');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment