0

I have a single hub using In-Memory association between users and connections. This hub has two instances of ConnectionMapping - one for Chat connections, one for Navbar connections. The goal is:

  • When a user is on any page other than Chat, they will have one signalR connection open, stored in navbarConnections
  • When a user is in Chat, they will have two signalR connections open - one in navbarConnections, one in chatConnections

These work great independently, but together they get a little glitchy. The way my app is structured is with a shell that contains the following (in a layoutVM knockout viewModel):

$.connection.hub.qs = {
        "isChat": false,
        "username": self.emailAddress(),
        "baseUrl": self.baseUrl()
    };
// define client methods
$.connection.hub.start().done(function () {
  // nothing
});

And then, when chat is loaded within the shell, this is defined (in a chatVm knockout viewmodel):

$.connection.hub.qs = {
    "isChat": true,
    "username": self.currentUsername(),
    "baseUrl": self.baseUrl()
};
$.connection.hub.start().done(function () {
    self.chatIsReady(true);
});

The problem that I'm encountering is that the query strings are sometimes incorrect when calls to signalR are made from Chat - it seems the querystring defined in LayoutVm is overriding those in ChatVm. What can I do to resolve this?

SB2055
  • 12,272
  • 32
  • 97
  • 202

2 Answers2

1

What you're doing doesn't make sense to me - you should only start the connection once (all hubs will use the same connection, and even if it would work without problems, there's no advantage in using multiple connections); once your connection is established, your client should interact with the server via hub methods.

So instead of trying to start another connection when the user joins the chat, you should do something like this:

chatHub.server.joinChat(userName);

Then do whatever you're doing in your OnConnected method when isChat == true there instead. Your main method of passing data to the hub should be the parameters of your hub methods, not the query string.

Lars Höppner
  • 18,252
  • 2
  • 45
  • 73
  • Thanks Lars. I could really use some code review... are you available for ~1 hour of paid consultation? Your recommendation raises some issues that I'm not sure how to handle, I'd love the chance to show you what I'm working with. – SB2055 Jan 12 '14 at 20:17
  • you can send me an e-mail (see bio), then we can see if we can work this out – Lars Höppner Jan 13 '14 at 13:00
  • Messaged :) Thank you Lars – SB2055 Jan 14 '14 at 01:34
0

It would appear that you are using the same connection to do both - so yes, the query string will get overwritten.

Gjohn
  • 1,261
  • 1
  • 8
  • 12
  • What would you recommend as a way to use different querystrings for two different connections to the same hub? – SB2055 Jan 12 '14 at 03:45
  • Looking at your code it would seem that the issue lies with the fact that you have the connection string defined in the layout as well as the chat views. Shouldn't it be just in one or the other? – Gjohn Jan 12 '14 at 13:17