Exemplo n.º 1
0
        private void BroadcastMessage(ChatEventArgs e)
        {
            ChatEventHandler t = broadcastList;

            if (t != null)
            {
                foreach (ChatEventHandler c in t.GetInvocationList())
                {
                    c.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
                }
            }
        }
Exemplo n.º 2
0
        public void Say(string message)
        {
            try
            {
                Trace.WriteLine("User " + username + " SAY: " + message + " : start");
                if (message == string.Empty)
                {
                    return;
                }

                ChatEventArgs e = new ChatEventArgs();
                e.name    = username;
                e.msgType = MessageType.Receive;
                e.message = message;
                BroadcastMessage(e);
                Trace.WriteLine("User " + username + " SAY: " + message + " : end");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie SAY method exception: " + ex.Message);
            }
        }
Exemplo n.º 3
0
        public string[] Join(string name)
        {
            string[] list = new string[chatChannelList.Count];
            try
            {
                Trace.WriteLine("User " + name + " join the chat room: start");
                username = name;
                callback = OperationContext.Current.GetCallbackChannel <IChatCallBack>();
                ChatEventArgs e = new ChatEventArgs();
                e.name    = name;
                e.msgType = MessageType.UserEnter;
                BroadcastMessage(e);
                broadcastList += MessageHandling;

                lock (obj)
                {
                    chatChannelList.Keys.CopyTo(list, 0);
                }

                myHandler = new ChatEventHandler(MessageHandling);
                lock (obj)
                {
                    if (!chatChannelList.Keys.Contains(name))
                    {
                        chatChannelList.Add(name, myHandler);
                    }
                }
                Trace.WriteLine("User " + username + " join the chat room: end");

                Console.WriteLine("Callback instance number: " + chatChannelList.Count);
                Console.WriteLine("broadcastList Delegete number: " + broadcastList.GetInvocationList().Count());
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie join method exception: " + ex.Message);
            }

            return(list);
        }
Exemplo n.º 4
0
        public string[] Join(string name)
        {
            string[] list = new string[chatChannelList.Count];
            try
            {
                Trace.WriteLine("User " + name + " join the chat room: start");
                username = name;
                callback = OperationContext.Current.GetCallbackChannel<IChatCallBack>();
                ChatEventArgs e = new ChatEventArgs();
                e.name = name;
                e.msgType = MessageType.UserEnter;
                BroadcastMessage(e);
                broadcastList += MessageHandling;

                lock (obj)
                {
                    chatChannelList.Keys.CopyTo(list, 0);
                }

                myHandler = new ChatEventHandler(MessageHandling);
                lock (obj)
                {
                    if (!chatChannelList.Keys.Contains(name))
                    {
                        chatChannelList.Add(name, myHandler);
                    }
                }
                Trace.WriteLine("User " + username + " join the chat room: end");

                Console.WriteLine("Callback instance number: " + chatChannelList.Count);
                Console.WriteLine("broadcastList Delegete number: " + broadcastList.GetInvocationList().Count());
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie join method exception: " + ex.Message);
            }

            return list;
        }
Exemplo n.º 5
0
        public void Whisper(string name, string message)
        {
            try
            {
                Trace.WriteLine("User " + username + " WHISPER: " + message + " : start");
                if (name == string.Empty || message == string.Empty)
                {
                    return;
                }

                ChatEventArgs e = new ChatEventArgs();
                e.name    = name;
                e.msgType = MessageType.ReceiveWhisper;
                e.message = message;
                chatChannelList[name](username, e);
                Trace.WriteLine("User " + username + " WHISPER: " + message + " : end");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie Whisper method exception: " + ex.Message);
            }
        }
Exemplo n.º 6
0
        public void Leave()
        {
            try
            {
                Trace.WriteLine("User " + username + " leave the chat room: start");
                lock (obj)
                {
                    if (chatChannelList.Keys.Contains(username))
                    {
                        chatChannelList.Remove(username);
                    }
                }

                broadcastList -= myHandler;
                ChatEventArgs e = new ChatEventArgs();
                e.name = username;
                e.msgType = MessageType.UserLeave;
                BroadcastMessage(e);
                //System.Threading.Thread.Sleep(3000);

                Trace.WriteLine("User " + username + " leave the chat room: end");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 7
0
 private void MessageHandling(object sender, ChatEventArgs e)
 {
     try
     {
         switch (e.msgType)
         {
             case MessageType.UserEnter:
                 callback.UserEnter(e.name);
                 break;
             case MessageType.UserLeave:
                 callback.UserLeave(e.name);
                 break;
             case MessageType.Receive:
                 callback.Receive(e.name, e.message);
                 break;
             case MessageType.ReceiveWhisper:
                 callback.ReceiveWhisper((string)sender, e.message);
                 break;
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine("Servcie MessageHandling method exception: " + ex.Message);
         Leave();
     }
 }
Exemplo n.º 8
0
        private void BroadcastMessage(ChatEventArgs e)
        {
            ChatEventHandler t = broadcastList;

            if (t != null)
            {
                foreach (ChatEventHandler c in t.GetInvocationList())
                {
                    c.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
                }
            }
        }
Exemplo n.º 9
0
        public void Whisper(string name, string message)
        {
            try
            {
                Trace.WriteLine("User " + username + " WHISPER: " + message + " : start");
                if (name == string.Empty || message == string.Empty)
                    return;

                ChatEventArgs e = new ChatEventArgs();
                e.name = name;
                e.msgType = MessageType.ReceiveWhisper;
                e.message = message;
                chatChannelList[name](username, e);
                Trace.WriteLine("User " + username + " WHISPER: " + message + " : end");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie Whisper method exception: " + ex.Message);
            }
        }
Exemplo n.º 10
0
        public void Say(string message)
        {
            try
            {
                Trace.WriteLine("User " + username + " SAY: " + message + " : start");
                if (message == string.Empty)
                    return;

                ChatEventArgs e = new ChatEventArgs();
                e.name = username;
                e.msgType = MessageType.Receive;
                e.message = message;
                BroadcastMessage(e);
                Trace.WriteLine("User " + username + " SAY: " + message + " : end");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie SAY method exception: " + ex.Message);
            }
        }