Pregunta

I'm still getting the basics of SignalR down. I'd like to use it in different parts of my website, so I was thinking I could start the connection in the main layout, and add client methods in sub views.

As per this answer I have :

window.hub = $.connection.hub.start();

in my Layout.cshtml, and

window.hub.done(function () {
   $.connection.notificationHub.server.joinScannerGroup(1);
   alert("in sub view");
});

in my subview, which works. The hub is connected, and the method JoinScannerGroup() is called. However, if i try to add:

$.connection.notificationHub.client.scanReceived = function (text) {
    alert("scan received");
};

anywhere in the javascript for the subview, it never gets called. Maybe I'm undertanding it wrong, but I'd like to connect on the mainpage, and then allow any subpages to receive client calls.

Also, if I move the $.connection.start() into the subview, it does work correctly. Am I understanding it wrong?

¿Fue útil?

Solución

You need to either add all client hub methods before you call hub.start(), or you need to add at least one hub method before calling hub.start() (so SignalR will subscribe to the hub) and then add all other hub methods like this:

$.connection.notificationHub.on('scanReceived ', function (text) {
    alert("scan received");
});

(also see documentation)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top