public async Task SendEventByUserId(string userId, string message) { if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(userId)) { return; } var conids = GetConnectionIds(userId); if (conids.Count > 0) { try { IClientProxy proxy = hubClients.Clients(conids); await proxy.Invoke("Exec", message); } catch (Exception e) { e.Log(); } } else { $"Failed to send event to {userId}, no connections".Log(); } }
//========== Background Methods ====================================================================================== private void HeartbeatCheck() { try { // Try catch needed because errors are otherwised silenced, somehow :-( // Remove dead connections (Ping not received timely) foreach (var kvp in LastSeenConnections.ToList()) { if (kvp.Value < DateTime.Now.AddSeconds(-CleanZombieAfter)) { var connectionId = kvp.Key; Log.SignalR(LogTag.CleaningZombieConnection, new { connectionId }); CleanConnection(connectionId); } } // Remove inactive members from "crowded" rooms var chatState = ChatCtrl.GetState(); foreach (var roomKvp in chatState.Rooms) { if (roomKvp.Key.IsMonoLang() && roomKvp.Value.Count > BootAboveUserCount) { var mostInactiveUserKvp = roomKvp.Value.Aggregate((l, r) => l.Value.LastActive < r.Value.LastActive ? l : r); if (mostInactiveUserKvp.Value.LastActive < DateTime.Now.AddMinutes(-BootOutOfRoomAfter)) { Log.SignalR(LogTag.KickSlackerOutOfRoom, new { roomId = roomKvp.Key, userId = mostInactiveUserKvp.Key }); // Get the list of connections that have to leave the room var userConnectionIds = UsersConnections.GetFromKey(mostInactiveUserKvp.Key) .Where(connId => RoomsConnections.HasConnection(roomKvp.Key, connId)) .Select(connId => connId.ToString()).ToList(); Clients.Clients(userConnectionIds).LeaveRoom(roomKvp.Key); } } } // Check for inactive members foreach (var chatUser in chatState.AllUsers.Values) { if (chatUser.LastActivity < DateTime.Now.AddMinutes(-UserIdleAfter)) { ChatCtrl.SetIdleStatus(chatUser.Id); Log.SignalR(LogTag.SetChatUserIdle, new { userId = chatUser.Id }); } } Log.SignalR(LogTag.HubHeartbeatCheckCompleted); } catch (Exception e) { Log.Error(LogTag.HeartbeatCheckError, e); } }
//private SysJob(IHubConnectionContext<dynamic> clients) //{ // Clients = clients; //} /// <summary> /// 执行函数 /// </summary> /// <param name="context"></param> public void Execute(IJobExecutionContext context) { try { JobKey jobkey = context.JobDetail.Key; _clients = (IHubConnectionContext <dynamic>)context.JobDetail.JobDataMap.Get("Clients"); var testDat = "服务器主动推送消息-->" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); List <string> connidList = GetUserConnList(jobkey.Name); //向所有开启服务的客户端推送查询到的数据 _clients.Clients(connidList).BroadcastSysData(testDat); //string connIds = context.JobDetail.JobDataMap.GetString("connIds"); } catch (System.Exception e) { throw new HubException("发送消息发生异常.", new { message = e.Message }); } }
public T Clients(IList <string> connectionIds) { return(TypedClientBuilder <T> .Build(_dynamicContext.Clients(connectionIds))); }
/// <summary> /// 推送数据 /// </summary> /// <param name="cb"></param> public static void PushData(Action <dynamic> cb) { cb(Connections.Clients(SubscribeClientIds)); }
private static void DoSend(List <string> conn_ids, string message) { clients.Clients(conn_ids).Message(message); }
public static dynamic Clients(IList <string> connectionIds) { return(Connector.Clients(connectionIds)); }