3
votes

Basically I have 3 apps. A web app, Nservicebus, and a signalr self hosting server for my hubs. I am currently using signalr version .4. Basically the web app initiates a connection with the self hosted server on page load. The User does an action which sends a command down the service bus. The service bus then connects to the signalr server to notify all the web clients.

My Web page:

<script src="http://localhost:8081/test/signalr/hubs" type="text/javascript"></script>
<script>
    jQuery.support.cors = true;
    var myHub;  

    $.connection.hub.url = 'http://localhost:8081/test/signalr/hubs';

    $(function () {
        myHub = $.connection.userAccountHub;

        myHub.ChangeNameUpdated = function (connId) {
            alert("recieved signalr message");          
        };

        $.connection.hub.start().done(function() {
            var myClientId = $.connection.hub.id;
            setCookie("srconnectionid", myClientId);
        });
    });

</script>

My SignalR server:

      static void Main(string[] args)
    {
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.AutoFlush = true;

        string url = "http://localhost:8081/test/";
        var server = new Server(url);


        server.EnableHubs();
        server.Start();


        Console.WriteLine("Server running on {0}", url);

        while (true)
        strong text{
            ConsoleKeyInfo ki = Console.ReadKey(true);
            if (ki.Key == ConsoleKey.X)
            {
                break;
            }
        }
    }

My Hub inside my Signalr server project

 public class UserAccountHub : Hub
    {

        public void UsersNameChanged(string passedThroughConnectionId)
        {
            Clients.ChangeNameUpdated(passedThroughConnectionId);
        }
    }

My Call from my nservicebus

        //connect to signalRServer
        var connection = new HubConnection("http://localhost:8081/test/signalr/hubs");
        IHubProxy myHub = connection.CreateProxy("SignalRServer.Hubs.UserAccountHub");

        //Credentials are inherited from the application
        connection.Credentials = CredentialCache.DefaultNetworkCredentials;

        var test = message.GetHeader("signalrConnectionId");


        connection.Start().Wait();
        myHub.Invoke("UsersNameChanged", message.GetHeader("signalrConnectionId")).ContinueWith(task =>
        {
            //for debuging purposes
            if (task.IsFaulted)
            {
                Console.WriteLine(
                    "An error occurred during the method call {0}",
                    task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Successfully called MethodOnServer");
            }
        });

I open up 2 browsers. I initiate the process on the second browser, and only the first one receives notifications. The second browser doesnt.

I don't see any issues that stand out when I run fiddler either.

Is there a better approach to this implementation, or am I missing something?

1
Is any of that working? var connection = new HubConnection("http://localhost:8081/test/signalr/hubs"); The above line is incorrect. You Create a connection to the hub url like this: var connection = new HubConnection("http://localhost:8081/test/"); Same for your javascript hub url, it should be: $.connection.hub.url = 'localhost:8081/test/signalr';davidfowl
It works "sometimes" which made me think I was doing the connection part correctly. If I use just 1 browser everything appears to work correctly (the first time), if I try and do a second action, it stops receiving notifications. But then if I refresh the page from scratch, it will get 1 notification then not receive any more. After checking out fiddler on a different project I have, it seems to be a difference I see. This solution stops long polling after it receives the first notification.Etch
I will check out using the connections as you specified.Etch
That's strange. As it looks like it shouldn't work at all.davidfowl
Thanks david, your comments got me going in the right direction. I upgraded to .5 and then took a nice long look at the how I was dealing with the connection strings.Etch

1 Answers

2
votes

OK! David's comments got me going in the right direction. Also noticed this was updated, didnt notice this before. Scroll to the bottom for the cross domain support

1.) Updated to version .5

2.) Modified my connection information in javascript to

$.connection.hub.url = 'http://localhost:8081/test/signalr';

$.connection.hub.start({ transport: 'longPolling', xdomain: true }).done(function () {
    //alert("staring hub connection:  " + $.connection.hub.id);
    setCookie("srconnectionid", $.connection.hub.id);
});

3.) I had to change the HubConnection

var connection = new HubConnection("http://localhost:8081/test/");

4.) I had to change the IHubProxy

IHubProxy myHub = connection.CreateProxy("UserAccountHub");