I've just found the solution. It's because I have to pass the query string via $.connection.hub
and not via chat.qs
So, the solution is
$.connection.hub.qs = { 'uid': '2541fdsf862d' };
Question
I'm going crazy here. I did the chatHub tutorial and everything works fine. I connect to the server. I can broadcast a message to every client.
Then I tried to add query strings because I would like to have some information on my user as many user can connect in the same session.
This is my javascript
(function ($) {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
//chat.state.userName = "test";
//chat.qs = "userToken3=blablabla";
chat.qs = { 'uid' : '2541fdsf862d' };
//$.connection.chatHub.qs = {"userToken2" : 125457};
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
//$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start({ 'uid': '2541fdsf862d' }).done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send("user", $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
})(jQuery);
If I take a look at my watchlist when I set a breakpoint in this function, I can see that the query string "uid" is perfectly set.
However on the server, I can't read the parameter.
public override Task OnConnected()
{
string name = Context.User.Identity.Name;
var caller = Clients.Caller;
var username = Clients.Caller.userName;
var queryString = Context.Request.QueryString;
var queryString2 = Context.QueryString;
var uid = Context.QueryString["uid"];
_connections.Add(name, Context.ConnectionId);
return base.OnConnected();
}
In this situation, uid is null. If I look at the send method on the server:
[Authorize]
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
var queryString = Context.Request.QueryString;
var context = Context;
}
uid
is not available in the queryString
variable.
What is wrong with my code? Does anyone have any clue?
Solution
I've just found the solution. It's because I have to pass the query string via $.connection.hub
and not via chat.qs
So, the solution is
$.connection.hub.qs = { 'uid': '2541fdsf862d' };
OTHER TIPS
If you want to pass parameters in the signalR OnConnect from JavaScript you add them to the connection.qs property. e.g.
self.hubConnection = $.hubConnection();
self.hubConnection.qs = { param1: value, param2: value };
Then in your OnConnect method in the signalR Hub you retrieve them by
var param1 = this.Context.QueryString["param1"];
var param2 = this.Context.QueryString["param2"];