示例#1
0
        /// <summary>
        /// 向指定的多个客户端id发送消息
        /// </summary>
        /// <param name="senderSessionId">发送者的客户端id</param>
        /// <param name="receiveSessionIds">接收者的客户端id</param>
        /// <param name="message">消息</param>
        /// <param name="receipt">是否回执</param>
        public void SendMessage(Guid senderSessionId, IEnumerable <Guid> receiveSessionIds, string message, bool receipt = false)
        {
            receiveSessionIds = receiveSessionIds.Distinct().ToArray();
            var Notifications = new WebSocketNotifications()
            {
                SenderSessionId   = senderSessionId,
                ReceiveSessionIds = receiveSessionIds.ToList(),
                Message           = message,
                Receipt           = receipt,
            };

            this.SendMessage(Notifications);
        }
示例#2
0
 /// <summary>
 /// 发送链接
 /// </summary>
 /// <param name="notification"></param>
 /// <returns></returns>
 public Task Send(INotification notification)
 {
     return(Task.Factory.StartNew(delegate
     {
         if (notification is WebSocketNotifications)
         {
             WebSocketNotifications webSocket = (WebSocketNotifications)notification;
             if (webSocket.session != null)
             {
                 webSocket.session.Send(notification.Message);
             }
             else
             {
             }
         }
     }));
 }
示例#3
0
        public void MessageJob(CancellationToken token)
        {
            Trace.WriteLine(DateTime.Now.ToString() + " test1 start");
            string appId = ApplicationConfigUtil.GetAppSeting("WebSocketServer", "WsPath");

            WebSocketProxyAgent.Initialization(ApplicationConfigUtil.GetAppSeting("WebSocketServer", "CSRedisClient"),
                                               appId);

            int           Status    = (int)MessageStatusEumns.Ready;
            List <string> AppIdList = new List <string>()
            {
                appId
            };
            long Count = DataHandleManager.Instance().RtMessageHandle.CountRtMessage(Status, DefaultTimeOutSecond, AppIdList);

            if (Count > 0)
            {
                List <RtMessage> MessageList = DataHandleManager.Instance().RtMessageHandle.GetRtMessageList(Status, DefaultTimeOutSecond, AppIdList);
                if (!CollectionUtils.IsEmpty(MessageList))
                {
                    foreach (var message in MessageList)
                    {
                        string      clientRedisKey = RedisUtil.GetUserIdRedisKey(message.AppId, message.UserId);
                        string      SessionId      = RedisUtil.GetWebSocketSessionID(clientRedisKey);
                        List <Guid> receiveList    = new List <Guid>()
                        {
                            new Guid(SessionId)
                        };
                        WebSocketNotifications notifications = new WebSocketNotifications()
                        {
                            SenderSessionId   = Guid.Empty,
                            ReceiveSessionIds = receiveList,
                            Message           = message.Message,
                            Receipt           = true,
                            NotificationTag   = message.id
                        };
                        WebSocketProxyAgent.SendMessage(notifications);
                    }
                }
            }
            Trace.WriteLine(DateTime.Now.ToString() + " test1 end");
        }
示例#4
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="notifications"></param>
        public void SendMessage(WebSocketNotifications notifications)
        {
            Dictionary <string, NotificationsEventArgs> redata = new Dictionary <string, NotificationsEventArgs>();

            foreach (var uid in notifications.ReceiveSessionIds)
            {
                string server = SelectServer(uid);
                if (redata.ContainsKey(server) == false)
                {
                    redata.Add(server, new NotificationsEventArgs(server, notifications));
                }
                redata[server].AddReceiveClientId(uid);
            }
            foreach (var sendArgs in redata.Values)
            {
                OnSend?.Invoke(this, sendArgs);
                var ServerKey = RedisKeyFormatUtil.GetServerKey(_appId, sendArgs.Server);
                _redis.Publish(ServerKey, JsonConvert.SerializeObject(notifications));
            }
        }
示例#5
0
 private void PushBroker_OnNewConnection(WebSocketNotifications notification)
 {
     notification.session.Close();
 }
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <param name="notifications"></param>
 public static void SendMessage(WebSocketNotifications notifications) =>
 SingleInstance.SendMessage(notifications);
 internal NotificationsEventArgs(string server, WebSocketNotifications Notifications)
 {
     this.Server        = server;
     this.Notifications = Notifications;
 }
示例#8
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}");
            }
        }