SignalR not working: $ .connection.client is null or undefined

After spending two days achieving this without success, I finally ask this question.

Reference Information. I am trying to use SignalR to send real-time data from my desktop application to all web pages.

What I have is a console application (this is just to launch the concept, then move it to the active project), which should send real-time data to a web page built in asp.net. Both use .Net 4.

In IE9, it shows me a debugging error in the line "chat.client.broadcastMessage =" saying that chat.client is null or undefined.

In firfox it does not show me this error, but it does not work / does not do anything, and the problem is the same as not showing me a warning ("ask for a name"); a window, so I think he doesn’t get to it and throws an error before that.

Here is my webpage code. This is a new separate website project.

<!DOCTYPE html> <html> <head> <title>SignalR Simple Chat</title> <style type="text/css"> .container { background-color: #99CCFF; border: thick solid #808080; padding: 20px; margin: 20px; } </style> </head> <body> <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. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script> <script src="Scripts/json2.js" type="text/javascript"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="signalr/hubs"></script> <!--Add script to update the page and send messages.--> <script type="text/javascript"> $(function () { alert('starting scrip'); // Declare a proxy to reference the hub. $.connection.hub.url = 'http://<ipaddressORlochost>:8080/chatroom'; alert($.connection.hub); alert($.connection.hub.url); var chat = $.connection.chatHub; // Create a function that the hub can call to broadcast messages. alert(chat); chat.client.broadcastMessage = function (name, message) { // Html encode display name and message. var encodedName = $('<div />').text(name).html(); var encodedMsg = $('<div />').text(message).html(); // Add the message to the page. $('#discussion').append('<li><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); }; alert('asking for name'); // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start({ jsonp:true}).done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); </script> </body> </html> ------------------------- 

I tried removing the lines below, and also adding "/ signalr" at the end.

$. connection.hub.url = 'http: //: 8080 / chatroom';

Here is my working application (server) code.

 class Program { static void Main(string[] args) { using (WebApplication.Start<Startup>(@"http://<ipaddressORlochost>:8080/chatroom")) { while (true) { // GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.addMessage("dsf","asdfd"); Console.WriteLine("Tags sent :" + DateTime.Now.ToString("HH:mm:ss")); Thread.Sleep(3000); } } } class Startup { public void Configuration(IAppBuilder app) { var config = new HubConfiguration { EnableCrossDomain=true }; app.MapHubs(config); } } public class ChatHub : Hub { public void Send(string b,string a) { try { Clients.All.broadcastMessage(b,a); } catch { } } } 

ANY HELP WAS REALLY DEFINED.

Thanks to everyone in advance.

-------------- FOUND ANSWER ---------- CHANGE THIS QUESTION HOW COULD NOT ANSWER MY OWN QUESTION FOR 8 HOURS

Hi ALL

Just in case someone else has the same problem. I found the answer / fixed.

THANKS AT Hatake Kakashi

The main problem was that $ .connection was now changed to $ .hubConnection

Here is what I had to change.

Script on my web page. I left commented code to show what was replaced by what.

  <script type="text/javascript"> $(function () { alert('starting scrip'); // Declare a proxy to reference the hub. // $.connection.hub.url = 'http://localhost:8080/chatroom'; var conn = $.hubConnection('http://localhost:8080/chatroom'); alert($.connection.hub); alert($.connection.hub.url); var chat = conn.createHubProxy('chatHub'); //$.connection.chatHub; // Create a function that the hub can call to broadcast messages. alert(chat); // chat.client.broadcastMessage = function (name, message) { // // Html encode display name and message. // var encodedName = $('<div />').text(name).html(); // var encodedMsg = $('<div />').text(message).html(); // // Add the message to the page. // $('#discussion').append('<li><strong>' + encodedName // + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); // }; chat.on('addMessage', function (a, message) { $('#discussion').append('<li><strong>' + $('<div />').text(a).html() + '</strong>:&nbsp;&nbsp;' + $('<div />').text(message).html() + '</li>'); }); alert('asking for name'); // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. conn.logging = true; // Start the connection conn.start().done(function () { alert("Now connected!"); }).fail(function () { alert("Could not Connect!"); }); // $.connection.hub.start({ jsonp: true }).done(function () { // $('#sendmessage').click(function () { // // Call the Send method on the hub. // chat.server.send($('#displayname').val(), $('#message').val()); // // Clear text box and reset focus for next comment. // $('#message').val('').focus(); // }); // }); }); </script> 

And on the server side (desktop application), because this is one way, I do not need anything in my ChatHub

  static void Main(string[] args) { using (WebApplication.Start<Startup>(@"http://localhost:8080/chatroom")) { while (true) { GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.addMessage("dsf", "Tags sent :" + DateTime.Now.ToString("HH:mm:ss")); Console.WriteLine("Tags sent :" + DateTime.Now.ToString("HH:mm:ss")); Thread.Sleep(500); } } } class Startup { public void Configuration(IAppBuilder app) { var config = new HubConfiguration { EnableCrossDomain=true }; app.MapHubs(config); } } public class ChatHub : Hub { /public void Send(string b, string a) //{ // try // { // Clients.All.addMessage(b, a); // } // catch { } //} } 

Hope this helps others.

+7
source share

All Articles