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

 ̄綄美尐妖づ 提交于 2019-12-12 07:09:41

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/12473030/signalr-how-to-send-data-to-iconnected-connect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!