Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'server' of undefined

Tags:

signalr

I'm trying to leverage SignalR while developing Chrome Extensions. I can run sample successfully but when I try to change the client from webpage to chrome extesion, I got some trouble. I define the connection the same as the sample like below:

var chat = $.connection.myHub;

console.log("start connect");
$.connection.hub.start().done(function () {
  // Call the Send method on the hub. 
  console.log("Test");
  //chat.server.send("extension", "start");
  chat.server.send("succ");
 });

But I always get this error: Uncaught TypeError: Cannot read property 'server' of undefined.

I have already enabled CrossDomain in my server side. Since `chat.server' is invoked, it seems the connection is established successfully. Did I miss adding some files/scripts in my extension folder?

like image 249
Rebornix Avatar asked Nov 01 '22 09:11

Rebornix


2 Answers

It seems that you are not bringing in the /signalr/hubs file. The auto-generated hubs file is what adds the .server and the .client properties to the connection object. Therefore if you're correctly including the /signalr/hubs file the next step is to ensure that your hub is being included in the dynamically generated JS file.

like image 52
N. Taylor Mullen Avatar answered Nov 28 '22 19:11

N. Taylor Mullen


In my case the problem is that C# automatically change the first letter of Hub name and methods to lower case in hub.js. MyHub1 changes to myHub1 and Hello() changed to hello() in client side automatically.

if you have:

public class MyHub1 : Hub
{
    public void Hello()
    {
        
    }
}

in your Hub.

in client side it must be used like this:

var simpleHubProxy = $.connection.myHub1;
$.connection.hub.start().done(function () {
    console.log("Connected.");
    simpleHubProxy.server.hello();
});
like image 23
Amin Karampour Avatar answered Nov 28 '22 21:11

Amin Karampour