Beispiel #1
0
        }//获取房间信息

        public void Leave()
        {
            if (this.id == null)
            {
                return;
            }

            lock (syncObj)
            {
                chatters.Remove(this.id);
            }
            ChatEvent -= myEventHandler;
            ChatEventArgs e = new ChatEventArgs();

            e.msgType = MessageType.UserLeave;
            e.name    = this.id;
            this.id   = null;
        }
Beispiel #2
0
        }//私聊

        public void fapply(string user)
        {
            ChatEventArgs e = new ChatEventArgs();

            e.msgType = MessageType.friendapply;
            e.name    = this.id;
            try
            {
                ChatEventHandler chatterTo;//创建一个临时委托实例
                lock (syncObj)
                {
                    chatterTo = chatters[user];                                    //查找成员字典中,找到要接收者的委托调用
                }
                chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); //异步方式调用接收者的委托调用
            }
            catch (KeyNotFoundException)
            {
            }
        }//好友申请
Beispiel #3
0
        public void sendmessage(string user, string msg)
        {
            ChatEventArgs e = new ChatEventArgs();

            e.msgType = MessageType.ReceiveWhisper;
            e.name    = this.id;
            e.message = msg;
            try
            {
                ChatEventHandler chatterTo;//创建一个临时委托实例
                lock (syncObj)
                {
                    chatterTo = chatters[user];                                    //查找成员字典中,找到要接收者的委托调用
                }
                chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); //异步方式调用接收者的委托调用
            }
            catch (KeyNotFoundException)
            {
            }
        }//私聊
Beispiel #4
0
        }//同意好友申请

        public LoginTag login(string id, string pwd)//登陆
        {
            if (!db.Enter(id, pwd))
            {
                return(null);
            }
            myEventHandler = new ChatEventHandler(MyEventHandler);//将MyEventHandler方法作为参数传递给委托
            if (!chatters.ContainsKey(id) && id != "" && id != null)
            {
                this.id = id;
                chatters.Add(id, MyEventHandler);
            }
            LoginTag lt = new LoginTag();

            lt.singe  = Guid.NewGuid().ToString();
            lt.userid = id;
            db.sing(lt);
            cb = OperationContext.Current.GetCallbackChannel <IClientcallback>(); //获取当前操作客户端实例的通道给IChatCallback接口的实例callback,此通道是一个定义为IChatCallback类型的泛类型,通道的类型是事先服务契约协定好的双工机制。
            ChatEventArgs e = new ChatEventArgs();                                //实例化事件消息类ChatEventArgs

            e.msgType = MessageType.UserEnter;
            e.name    = id;
            return(lt);
        }
Beispiel #5
0
        private void MyEventHandler(object sender, ChatEventArgs e)
        {
            try
            {
                switch (e.msgType)
                {
                case MessageType.Receive:
                    cb.Receive(e.name, e.message);
                    break;

                case MessageType.ReceiveWhisper:
                    cb.ReceiveWhisper(e.name, e.message);
                    break;

                case MessageType.UserEnter:
                    cb.UserEnter(e.name);
                    break;

                case MessageType.UserLeave:
                    cb.UserLeave(e.name);
                    break;

                case MessageType.friendapply:
                    cb.cfapply(e.name);
                    break;

                case MessageType.friendaccess:
                    cb.cfacc(e.name);
                    break;
                }
            }
            catch
            {
                Leave();
            }
        }