Example #1
0
        private async Task SendAgent(DeviceNotification deviceNotification)
        {
            var onlineClients = _onlineManager.GetAllByDeviceId(deviceNotification.DeviceId);

            foreach (var onlineClient in onlineClients)
            {
                if (_signalrClient == null)
                {
                    _logger.LogDebug("不能获取DeviceId " + deviceNotification.DeviceId + " 在 connectionId " + onlineClient.ConnectionId + " 从  SignalR AgentHub!");
                    continue;
                }

                await _signalrClient.Push(onlineClient.SysName, new Core.SignalBus.Model.SignalrMessage
                {
                    Type     = typeof(DeviceNotification).FullName,
                    SmgType  = SignalrMessageTypeEnum.Push,
                    JsonData = JsonHelper.ToJson(deviceNotification)
                });
            }
        }
Example #2
0
        public async Task SendDeviceNotificationsAsync(DeviceNotification deviceNotification)
        {
            //Logger.LogWarning("1--signalRClient.SendAsync-getNotification:" + JsonHelper.ToJson(deviceNotification));
            try
            {
                int send          = 0;
                var onlineClients = _onlineManager.GetAllByDeviceId(deviceNotification.DeviceId);

                if (1 == 2)
                {
                    var onlineClient = onlineClients.OrderByDescending(p => p.ConnectTime).FirstOrDefault();
                    if (onlineClient != null)
                    {
                        var signalRClient = _deviceHubContext.Clients.Client(onlineClient.ConnectionId);
                        if (signalRClient == null)
                        {
                            Logger.LogDebug("不能获取DeviceId " + deviceNotification.DeviceId + " 在 connectionId " + onlineClient.ConnectionId + " 从  SignalR WeiChatHub!");
                        }

                        await signalRClient.SendAsync("getNotification", JsonHelper.ToJson(deviceNotification.Notification));
                    }
                }
                else
                {
                    foreach (var onlineClient in onlineClients)
                    {
                        var signalRClient = _deviceHubContext.Clients.Client(onlineClient.ConnectionId);
                        if (signalRClient == null)
                        {
                            Logger.LogDebug("不能获取DeviceId " + deviceNotification.DeviceId + " 在 connectionId " + onlineClient.ConnectionId + " 从  SignalR WeiChatHub!");
                            continue;
                        }
                        //Logger.LogWarning("2--signalRClient.SendAsync-getNotification:" + JsonHelper.ToJson(deviceNotification));
                        await signalRClient.SendAsync("getNotification", JsonHelper.ToJson(deviceNotification.Notification));

                        send++;

                        //推送记日志
                        WriteHttpLog(
                            RequestTypeEnum.Signalr,
                            CommonHelper.NewSequentialGuid().ToString(),
                            JsonConvert.SerializeObject(deviceNotification.Notification),
                            "Signalr#SendDeviceNotifications", deviceNotification.DeviceId);
                    }

                    //if( send == 0 ) {
                    //    Logger.LogError( "不能发送通知给用户: " + deviceNotification.DeviceId );
                    //}
                    //else if( send == 1 ) {
                    //    Logger.LogError( "连接没有重复情况,推送发送成功: " + deviceNotification.DeviceId );
                    //}
                    //else if( send > 1 ) {
                    //    Logger.LogError( "连接有重复情况: " + deviceNotification.DeviceId );
                    //}
                }
            }
            catch (Exception ex) {
                Logger.LogWarning("不能发送通知给用户: " + deviceNotification.DeviceId);
                Logger.LogWarning(ex.ToString(), ex);
            }

            await Task.FromResult(0);
        }