Ejemplo n.º 1
0
        /// <summary>
        /// Relay event to all clients
        /// </summary>
        /// <param name="e"></param>
        private void RelayMessage(ChatEventArgs e)
        {
            // Event
            ChatEventHandler temp = ChatEvent;

            if (temp != null)
            {
                // Send current evnets
                foreach (ChatEventHandler handler in temp.GetInvocationList())
                {
                    if (handler != myEventHandler)
                    {
                        handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //public void Whisper(string to, string msg)
        public void Whisper(int imageNumber, bool isMoving)
        {
            ChatEventArgs e = new ChatEventArgs();
            e.msgType = MessageType.ReceiveWhisper;
            e.name = this.name;

            e.imageNumber = imageNumber;
            e.isMoving = isMoving;
            // e.message = msg;
            RelayMessage(e);

            //try
            //{
            //    ChatEventHandler chatterTo;
            //    lock (syncObj)
            //    {
            //        chatterTo = chatters[to];
            //    }
            //    chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
            //}
            //catch (KeyNotFoundException)
            //{
                // When errors occur
            //}
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Raise Event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void MyEventHandler(object sender, ChatEventArgs e)
 {
     try
     {
         switch (e.msgType)
         {
             case MessageType.Receive:
                 // callback.Receive(e.name, e.message);
                 callback.Receive(e.name, e.x, e.y, e.z);
                 Console.WriteLine("callback Receive from " + e.name + "(" + e.x + ", " + e.y + ")");
                 break;
             case MessageType.ReceiveWhisper:
                 //callback.ReceiveWhisper(e.name, e.message);
                 callback.ReceiveWhisper(e.name, e.imageNumber, e.isMoving);
                 Console.WriteLine("callback ReceiveWhisper from " + e.name + "(" + e.imageNumber + ", " + e.isMoving + ")");
                 break;
             case MessageType.ReceiveStart:
                 callback.ReceiveStart(e.name, e.posX, e.posY);
                 Console.WriteLine("callback Start request from " + e.name);
                 break;
             case MessageType.ReceiveReset:
                 callback.ReceiveReset(e.name);
                 Console.WriteLine("callback Reset request from " + e.name);
                 break;
             case MessageType.UserEnter:
                 callback.UserEnter(e.name);
                 Console.WriteLine("callback UserEnter " + e.name);
                 break;
             case MessageType.UserLeave:
                 callback.UserLeave(e.name);
                 Console.WriteLine("callback UserLeave " + e.name);
                 break;
         }
     }
     catch
     {
         Leave();
     }
 }
Ejemplo n.º 4
0
        public void Start(string name)
        {
            ChatEventArgs e = new ChatEventArgs();
            e.msgType = MessageType.ReceiveStart;
            e.name = this.name;

            // Generate Random Position
            Random random = new Random();

            for (int i = 0; i < e.posX.Length; i++)
            {
                int _tempX = random.Next(0, 900);
                int _tempY = random.Next(100, 600);

                // Avoid overlapping between points
                for (int j = 0; j <= i; j++)
                {
                    // Ignore check logic for the first location
                    if (i > 0 && e.posX[j] > 0)
                    {
                        while ((Math.Abs(e.posX[j] - _tempX) < 200) && (Math.Abs(e.posY[j] - _tempY) < 200))
                        {
                            _tempX = random.Next(0, 900);
                            _tempY = random.Next(100, 600);

                            j = 0;
                        }
                    }
                }

                e.posX[i] = _tempX;
                e.posY[i] = _tempY;
            }

            // Broadcast to all clients
            BroadcastMessage(e);
        }
Ejemplo n.º 5
0
        //public void Say(string msg)
        public void Say(int x, int y, string z)
        {
            ChatEventArgs e = new ChatEventArgs();
            e.msgType = MessageType.Receive;
            e.name = this.name;
            // e.message = msg;

            //Console.WriteLine("server: " + " I got what you are saying");

            e.x = x;
            e.y = y;
            e.z = z;

            RelayMessage(e);
        }
Ejemplo n.º 6
0
        public void Reset(string name)
        {
            ChatEventArgs e = new ChatEventArgs();
            e.msgType = MessageType.ReceiveReset;
            e.name = this.name;

            // Broadcast to all clients
            BroadcastMessage(e);
        }
Ejemplo n.º 7
0
        public void Leave()
        {
            if (this.name == null) return;

            lock (syncObj)
            {
                chatters.Remove(this.name);
            }
            ChatEvent -= myEventHandler;

            // Raise New Event
            ChatEventArgs e = new ChatEventArgs();
            e.msgType = MessageType.UserLeave;
            e.name = this.name;
            RelayMessage(e);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// * JOIN 
        /// When the user join the chat room first
        /// </summary>
        /// <param name="name">User Name</param>
        /// <returns>Return user list if there are no the same name in the list</returns>
        public string[] Join(string name)
        {
            Console.WriteLine("User Name: " + name);

            myEventHandler = new ChatEventHandler(MyEventHandler);

            lock (syncObj)
            {
                if (!chatters.ContainsKey(name))// Check there are the same name in the list
                {
                    // Add name and event
                    this.name = name;
                    chatters.Add(name, MyEventHandler);

                    // Set the channel for user
                    callback = OperationContext.Current.GetCallbackChannel<IChatCallback>();

                    // Send "UserEnter" event
                    ChatEventArgs e = new ChatEventArgs();
                    e.msgType = MessageType.UserEnter;
                    e.name = name;
                    RelayMessage(e);

                    // Add Deligater
                    ChatEvent += myEventHandler;

                    // Send the user list
                    string[] list = new string[chatters.Count];

                    lock (syncObj)
                    {
                        chatters.Keys.CopyTo(list, 0);
                    }
                    return list;
                }
                else // If the there are the same name in the list
                {
                    return null;
                }
            }
        }