I am trying to create a chat application on my ASP.NET solution between users. With SignalR installed to help with real-time chatting. However the button created using JavaScript is not firing and the browser is not prompting to enter username as it is suppose to.
Below is my code. Check out the design here
C# code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace Synergince
{
// This is a class to handle the username and message entered by the username
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcast method to update clients
Clients.All.broadcastMessage(name, message);
}
}
}
Source Code - JavaScript
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden"id="displayname" />
<ul id="discussion">
</ul>
</div>
<!--Script references -->
<script src="Scripts/jquery-1.4.1.min.js"></script>
<script src="Scripts/jquery.signalR-1.2.1.js"></script>
<script src="/signalr/hubs"></script>
<script type="text/javascript"">
$(on('click',function () {
var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (name, message) {
var Name = $('<div />').text(name).html();
var Msg = $('<div />').text(message).html();
//add message to the page
$('#discussion').append('<li><strong>' + encodedName + '</strong>: ' + encodeMsg + '</li>');
};
//get username from user
$('#displayname').val(prompt('Enter preferred alias:', ''));
$('#message').focus();
//start connection
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
//call send method on the hub
chat.server.send($('#displayname').val(), $('#message').val());
//clear textbox
$('#message').val('').focus();
});
});
});
</script>