stickleback14
stickleback14

Reputation: 13

SignalR 1.0 - send data to a specific client

I want to be able to send messages to a specific client, I have working code but I cannot find a way to identify users, as on each page load/refresh the client id will change, so I cannot rely on that. I have tried to append a querystring to the connection, but I have only been able to find the querystring of the same context.

This is my hub, and within the send method i want to be able to match the id that is sent in to a particular connection id at the point of sending the message:

public class Chat : Hub
{
    public string addMsg()
    {
        return "";
    }

    public void Send(string message, string id)
    {

        Clients.Client(Context.ConnectionId).receiveMessage(message);
    }
}

Here is my client code, i want to pass the id of the person to send a message to to the server send method, and use the querystring value of the other connected user to match it to the id i am sending.

var chat = $.connection.chat;

chat.client.receiveMessage = function (message) {
  alert("Received from server: " + message);
};

chat.addMsg = function (message) {

};

$("#sendMessage").click(function () {
  chat.server.send($('#newMessage').val(), 6);
});

$.connection.hub.qs = "id=1";

$.connection.hub.start().done(function () {
  var myClientId = $.connection.hub.id;
  var qs = $.connection.hub.qs;
});

I hope my question makes sense, I have been trying to crack this for a while now, below are some links to some of the articles i have used to get to where i am now, I am just missing the last piece of the puzzle, please go easy on me :)

http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx

SignalR- send data to a specific client

https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

Upvotes: 1

Views: 3361

Answers (2)

Nestor
Nestor

Reputation: 13990

How about using the Clients.Caller object int he hub, and overriding the OnConnected method:

public override Task OnConnected()
{
    Clients.Caller.sendInitMessage(...);
    return base.OnConnected();
}

Upvotes: 0

Ragesh
Ragesh

Reputation: 3073

I don't think this is going to work the way you want it to. The connection ID is just that -- an identifier for a particular connection. SignalR itself doesn't know anything about authenticating users. It is, however, built on top of ASP.NET and all of your familiar authentication methods (Windows, Forms, etc.) work as you would expect.

Once ASP.NET has authenticated the user, you have access to this in your hubs as Context.User. It's now up to you to maintain a mapping between this user and one or more connection IDs. Besides browser refreshes, you might need to deal with a user accessing your service from multiple browsers or machines. Sending a message to this user means sending it to all of those browsers and machines.

Jabbr does all this and more. You really should take a look at that code for a good way to implement this.

Upvotes: 3

Related Questions