Alex Twitch
Alex Twitch

Reputation: 11

Trouble with SignalR 2.0 One Connection w/ Multiple Pages

I'm working on an application using signalR, 2.0 specifically, where the signalR connection is created on the first page. The connection is being used throughout the pages and has multiple calls in the hub.

[HubName("broadcastHub")]
public class BroadcastHub : Hub
{

}

class SignalRHubInterface
{
    private static SignalRHubInterface self;
    public IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>();

    public static SignalRHubInterface Instance
    {
        get
        {
            if (self == null)
            {
                self = new SignalRHubInterface();
            }

            return self;
        }
    }

    public void BroadcastListA(List<string> listA)
    {
        context.Clients.All.listA(listA);
    }

    public void BroadcastListB(List<string> listB)
    {
        context.Clients.All.listB(listB);
    }

    public void BroadcastListC(List<string> listC)
    {
        context.Clients.All.listC(listC);
    }
}

There is a javascript file that contains all these functions, and more, that is loaded:

function getPageName() {
    var url = window.location.pathname;
    var index = url.lastIndexOf("/") + 1;
    var filenameWithExtension = url.substr(index);
    var filename = filenameWithExtension.split(".")[0];
    return filename;
}

$(document).ready(function () {
if (getPageName().toLowerCase() == "pagea") {
        loadSignalRMethods();
    }
    else
        if (getPageName().toLowerCase() == "pageb") {
            functionabc();
        }
}

function loadSignalRMethods() {

    var ObjsignalR = $.connection.broadcastHub;
    $.connection.hub.start();

    ObjsignalR.client.listA= function (listA) {
   *...Do something*
    };

ObjsignalR.client.listB= function (listB) {
    *...Do something*
};

ObjsignalR.client.listC= function (listC) {
    *...Do something*
};

}

The functions can be called in the first page but, not in any other page. Attempting to load the signalR functions in another page causes an error with the connection hub.

When attempting to call the signalR functions on a second page, the function can't be found. I believe it might be an issue with the functions not being loaded on subsequent pages.

Functions are called by the following in a separate class:

SignalRHubInterface.Instance.BroadcastListA(listA);

Been stuck on this for awhile so any help is greatly appreciated.

Upvotes: 1

Views: 816

Answers (1)

halter73
halter73

Reputation: 15244

You need to hook up your client methods in JavaScript before calling $.connection.hub.start().

I would also suggest hooking up .done() and .fail() callbacks to your call to .start() so you can verify the SignalR client has successfully established a connection.

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#establishconnection

Enabling logging in the SingalR JS client can also be useful when you run into problems:

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#logging

Upvotes: 1

Related Questions