Esempio n. 1
0
        /// <summary>
        /// 传给服务端处理事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnServerHandler(object sender, NotificationsEventArgs e)
        {
            NotificationsVo   notifications     = JsonConvert.DeserializeObject <NotificationsVo>(e.Notifications.Message);
            NotificationsType notificationsType = (NotificationsType)Enum.Parse(typeof(NotificationsType), notifications.type, true);

            switch (notificationsType)
            {
            case NotificationsType.receipt_send:
            case NotificationsType.receipt_offline:
                int status    = (int)MessageStatusEumns.Complete;
                int MessageId = Convert.ToInt32(e.Notifications.NotificationTag);
                DataHandleManager.Instance().RtMessageHandle.UpdateAvailResult(MessageId, status, DateTime.Now, notifications.type);
                break;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 消息订阅处理
        /// </summary>
        /// <param name="e"></param>
        public void RedisSubScribleMessage(CSRedis.CSRedisClient.SubscribeMessageEventArgs e)
        {
            try
            {
                Trace.WriteLine($"收到消息:{e.Body}");
                var data     = JsonConvert.DeserializeObject <WebSocketNotifications>(e.Body);
                var outgoing = new ArraySegment <byte>(Encoding.UTF8.GetBytes(data.Message));
                foreach (var sessionId in data.ReceiveSessionIds)
                {
                    if (ClusterServer.TryGetValue(sessionId, out var wslist) == false)
                    {
                        Trace.WriteLine($"websocket{sessionId}离线了,{data.Message}" + (data.Receipt ? "[消息回调]" : ""));
                        if (data.CheckReceipt(sessionId))
                        {
                            string message = new NotificationsVo(NotificationsType.receipt_offline, data.Message).ToString();
                            var    offlineNotifications = new WebSocketNotifications()
                            {
                                SenderSessionId   = sessionId,
                                ReceiveSessionIds = new List <Guid>()
                                {
                                    data.SenderSessionId
                                },
                                Message         = message,
                                NotificationTag = data.NotificationTag
                            };
                            SendMessage(offlineNotifications);
                        }
                        else if (sessionId == Guid.Empty)
                        {
                            string server = SelectServer(sessionId);
                            OnServerHandler?.Invoke(this, new NotificationsEventArgs(server, data));
                        }
                        continue;
                    }

                    ICollection <WebSocketSession> sockarray = wslist.Values;
                    //如果接收消息人是发送者,并且接收者只有1个以下,则不发送
                    //只有接收者为多端时,才转发消息通知其他端
                    if (sessionId == data.SenderSessionId && sockarray.Count <= 1)
                    {
                        continue;
                    }
                    //发送WebSocket
                    foreach (var sh in sockarray)
                    {
                        sh.SocketClient.SendAsync(outgoing, WebSocketMessageType.Text, true, CancellationToken.None);
                    }
                    if (data.CheckReceipt(sessionId))
                    {
                        string message = new NotificationsVo(NotificationsType.receipt_send, data.Message).ToString();
                        var    receiptSendNotifications = new WebSocketNotifications()
                        {
                            SenderSessionId   = sessionId,
                            ReceiveSessionIds = new List <Guid>()
                            {
                                data.SenderSessionId
                            },
                            Message         = message,
                            NotificationTag = data.NotificationTag
                        };
                        SendMessage(receiptSendNotifications);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"订阅方法出错了:{ex.Message}");
            }
        }