Example #1
0
 public void SendToBrowser(int sender,string text)
 {
     if (NotifyClient == null || string.IsNullOrEmpty(text))
         return;
     ClientNotifEventArgs arg = new ClientNotifEventArgs(sender,new int[]{Context.ClientID},new HttpRawMessage(text));
     NotifyClient(this, arg);
 }
        public virtual CommonResult BroadCastToClient(object sender, ClientNotifEventArgs e)
        {
            CommonResult result = CommonResult.InvalidParams;
            ISocketMessage msg = e.Command;
            int senderUID = e.Sender;
            int[] keys = null;

            try
            {
                if (this._Clients.Keys.Count > 0)
                {
                    //  如果指定了广播受众
                    if (e.Recvs != null && e.Recvs.Length > 0)
                        keys = e.Recvs;
                    else
                    {
                        //  否则群发
                        lock (this._Clients.SyncRoot)
                        {
                            if (this._Clients.Keys.Count < 1)
                                return CommonResult.Success;
                            keys = new int[this._Clients.Keys.Count];
                            this._Clients.Keys.CopyTo(keys, 0);
                        }
                    }

                    if (keys != null && keys.Length > 0)
                    {
                        ClientManager clmngr = null;
                        foreach (int uid in keys)
                        {
                            //  如果发送人是普通用户,则检查其是否在某个用户的黑名单里
                            if (this._Clients.ContainsKey(uid))
                            {
                                clmngr = this._Clients[uid] as ClientManager;
                                AppendToOutMessageQueue(new QueuedOutMessage(clmngr, msg));
                            }
                        }
                    }
                }
                result = CommonResult.Success;

            }
            catch (Exception ex)
            {
                DebugMessage(string.Format("BroadCastToClient Error:{0};Source:{1}", ex.Message, ex.Source));
                result = CommonResult.SystemError;
            }
            finally { }

            return result;
        }
        /// <summary>
        /// 向客户端下发消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public virtual CommonResult NotifyClient(object sender, ClientNotifEventArgs e)
        {
            int[] sessionIDs = e.Recvs;
            ISocketMessage msg = e.Command;
            ClientManager clmngr = null;
            CommonResult result = CommonResult.OtherError;

            try
            {
                if (sessionIDs == null || sessionIDs.Length < 1 || msg == null)
                    return CommonResult.NoData;
                foreach (int sessionID in sessionIDs)
                {
                    if (!_Clients.ContainsKey(sessionID))
                        continue;
                    clmngr = this._Clients[sessionID] as ClientManager;
                    AppendToOutMessageQueue(new QueuedOutMessage(clmngr, msg));
                }

                result = CommonResult.Success;
            }
            catch (Exception ex)
            {
                DebugMessage(string.Format("NotifiClient Error:{0};Source:{1}", ex.Message, ex.Source));
                result = CommonResult.SystemError;
            }
            finally { }

            return result;
        }