//public async override Task OnConnected() //{ // await base.OnConnected(); // Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId); //} //public async override Task OnDisconnected(bool stopCalled) //{ // await base.OnDisconnected(stopCalled); // Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId); //} public override async Task OnConnected() { await base.OnConnected(); //可将授权信息写入Cookie中,再在Hub中解析,建立用户与ConnectId之间的关联关系; foreach (var item in Context.RequestCookies.Values) { Logger.Debug("Cookie " + item.Name + " : " + item.Value); } //$.connection.hub.qs = {"qs_token":"qs_token_value"}; //在JS中使用以上代码设置即可; foreach (var item in Context.QueryString) { Logger.Debug("QueryString " + item.Key + " : " + item.Value); } //see https://stackoverflow.com/questions/15528221/passing-token-through-http-headers-signalr //从文章看,WebSocket是不支持自定义Http Headder //但.net 版本的signalr是支持Header的 foreach (var item in Context.Headers) { Logger.Debug("Heanders " + item.Key + " : " + item.Value); } var client = CreateClientForCurrentConnection(); Logger.Debug("A client is connected: " + client); _onlineClientManager.Add(client); }
public override async Task OnConnected() { await base.OnConnected(); var client = CreateClientForCurrentConnection(); Logger.Debug("A client is connected: " + client); _onlineClientManager.Add(client); }
/// <summary> /// /// </summary> /// <param name="userId"></param> /// <returns></returns> public async Task InitUser(long userId) { var oneClient = _onlineClientManager.GetByConnectionIdOrNull(Context.ConnectionId); if (oneClient != null) { _onlineClientManager.Remove(Context.ConnectionId); var newclient = CreateClientForCurrentConnection(userId); _onlineClientManager.Add(newclient); } else { } //await Clients.All.InvokeAsync("NoticeOnline", $"用户组数据更新完成,新增id为:{Context.ConnectionId}userId:{userId}"); }
public void Test_All() { int tenantId = 1; Dictionary <string, int> connections = new Dictionary <string, int>(); for (int i = 0; i < 100; i++) { connections.Add(MakeNewConnectionId(), i + 1); } foreach (var pair in connections) { _clientManager.Add(new OnlineClient(pair.Key, "127.0.0.1", tenantId, pair.Value)); } var testId = connections.Keys.ToList()[5]; _clientManager.GetAllClients().Count.ShouldBe(connections.Count); _clientManager.GetAllByUserId(new UserIdentifier(tenantId, connections[testId])).Count.ShouldBe(1); _clientManager.GetByConnectionIdOrNull(testId).ShouldNotBeNull(); _clientManager.Remove(testId).ShouldBeTrue(); _clientManager.GetAllClients().Count.ShouldBe(connections.Count - 1); _clientManager.GetByConnectionIdOrNull(testId).ShouldBeNull(); _clientManager.GetAllByUserId(new UserIdentifier(tenantId, connections[testId])).Count.ShouldBe(0); }
public override async Task OnConnectedAsync() { await base.OnConnectedAsync(); var client = mOnlineClientManager.GetByConnectionId(Context.ConnectionId); if (client == null) { client = CreateClientForCurrentConnection(Context.GetHttpContext().Request.Query["accessToken"].ToString()); mOnlineClientManager.Add(client); } }
public async override Task OnConnected() { await base.OnConnected(); var client = new OnlineClient( Context.ConnectionId, GetIpAddressOfClient(), AbpSession.TenantId, AbpSession.UserId ); Logger.Debug("A client is connected: " + client); _onlineClientManager.Add(client); }