Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: How to send data to IConnected.Connect()

Tags:

signalr

I implement the Connect() method on IConnected interface to add new connections to the correct groups. This works well except for one thing: in order to add the user to the correct group, I need to send a value to be read in this method. I tried adding property to the client hub:

var uIHub = $.connection.uIHub;
uIHub.SessionType = "Edit";

But it's not accessible from the Connect method:

if (string.IsNullOrEmpty(Caller.SessionType) || Caller.SessionType == "Edit") {
     sessionId = WAFContext.EditSession.SessionId.ToString();                
} else {
     sessionId = WAFContext.ViewSession.SessionId.ToString();
}
Groups.Add(Context.ConnectionId, sessionId);

Caller.SessionType is always null.

Any suggestions on how to solve this?

like image 490
Vidar Langberget Avatar asked Sep 18 '12 08:09

Vidar Langberget


People also ask

How do I send a message to a specific user in SignalR?

SignalR allows messages to be sent to a particular client connection, all connections associated with a specific user, as well as to named groups of connections. => await Clients. User(userId).

How do I send a message to a group in SignalR?

When user click on send button, the message to be posted to server side using signalR connection hub. Thus whenever you post any message after clicking the join group button, the message will appear to all the clients who has joined the group.

How many connections SignalR can handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

How does SignalR WebSocket work?

SignalR is an abstraction over some of the transports that are required to do real-time work between client and server. SignalR first attempts to establish a WebSocket connection if possible. WebSocket is the optimal transport for SignalR because it has: The most efficient use of server memory.


1 Answers

I solved this by adding my information to the querystring, which is available on the IConnected.Connect() method.

On the .NET client you pass the querystring into your HubConnection:

var connection = new HubConnection("http://localhost:8080/", "myInfo=12345"); 

On the JS client, you set the qs property before starting the connection:

$.connection.hub.qs = "myInfo=12345"; 

You can then access this information on the server in the Connect() method:

var myInfo = Context.QueryString["myInfo"]; Groups.Add(Context.ConnectionId, myInfo); 
like image 170
Alexander Köplinger Avatar answered Sep 25 '22 11:09

Alexander Köplinger