Skip to content

Instantly share code, notes, and snippets.

@kamlekar
Created December 7, 2012 10:15
Show Gist options
  • Save kamlekar/4232310 to your computer and use it in GitHub Desktop.
Save kamlekar/4232310 to your computer and use it in GitHub Desktop.
Chat Application (work in progress) file.js
//Added References
/*
* jquery-1.6.4.min.js
* jquery.signalR-1.0.0-alpha2.min.js
*/
var showChatName = new Boolean();
showChatName = true;
var chatUsername = window.prompt("Enter Username:", "");
var sameChatName;
$(function () {
chatUsername = chatUsername.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
});
sameChatName = chatUsername;
//show Username in Title bar div
$('#chat-title-name').text(chatUsername);
// Proxy created on the fly
var chat = $.connection.chat; //calling the created class "Chat"
chat.state.username = chatUsername;
$.connection.hub.qs = "name=" + chatUsername;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (chatUsername, message, showChatName) {
if (showChatName) {
$('#messages').append('<b>' + chatUsername + '</b>' + ':&nbsp;' + message + '</br>');
showChatName = false;
}
else {
$('#messages').append('&nbsp;&nbsp;' + message + '</br>');
}
//To keep scroll always bottom
$("#messages").scrollTop($("#messages")[0].scrollHeight);
};
$('#txtmsg').keypress(function (e) {
if (e.which == 13) {
e.preventDefault();
var message = $('#txtmsg').val();
if (message.replace(/\s/g, "").length !== 0) {
chat.server.send(chatUsername, $('#txtmsg').val(), showChatName);
}
$('#txtmsg').val('');
}
});
// Start the connection
$.connection.hub.start()
.done(function () {
chat.server.getConnectedUsers().done(function () {
/*display your contacts*/
//------------------------------
// I cant figure it out what should I call here?
//-----------------------------
// I tried the below code but this doesnt work
/*
* $('#showUsernames').text(function () {
* chat.server.getConnectedUsers();
* });
*/
});
}).done(function () {
chat.server.joined();
});
});
@vgheri
Copy link

vgheri commented Dec 7, 2012

chat.server.GetConnectedUsers() You need to use lowercase style, so chat.server.getConnectedUsers()

The other error is due to the too many and wrong }); });

@kamlekar
Copy link
Author

kamlekar commented Dec 8, 2012

Yes thank you very much no errors now..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment