Пример #1
0
        private void AddConnectedUser(string username)
        {
            connectedUsers.Add(username);
            connectedUsers.Sort();

            ConnectedUserBlock newUserBlock = new ConnectedUserBlock
            {
                Name     = username,
                Location = new Point(0, 5)
            };

            newUserBlock.User_Username.Text = username;
            Panel_ConnectedUsersList.Controls.Add(newUserBlock);

            foreach (Control control in Panel_ConnectedUsersList.Controls)
            {
                control.Location = new Point(0, 5 + (connectedUsers.IndexOf(control.Name) * 25));
            }
            Update();
        }
Пример #2
0
        private void HandleReceiver()
        {
            //set delegates
            AddRichMsg   addRichMsg = RichMessageReceived;
            AddUserBlock addUser    = AddConnectedUser;
            DelUserBlock delUser    = RemoveConnectedUser;

            //send to server join message
            PacketStruct packetStruct = new PacketStruct
            {
                Type = "join",
                User = username
            };

            _sWriter.WriteLine(JsonConvert.SerializeObject(packetStruct));
            _sWriter.Flush();

            //while connected, recieve messages and process them
            PacketStruct packet = new PacketStruct();

            while (isConnected)
            {
                //Deserialize packet from StreamReader
                try   { packet = JsonConvert.DeserializeObject <PacketStruct>(_sReader.ReadLine()); }
                catch { packet = new PacketStruct(); }

                switch (packet.Type)
                {
                case "rcu":
                    Invoke(delUser, packet.User);
                    break;

                case "acu":
                    Invoke(addUser, packet.User);
                    break;

                case "ucu":
                    if (packet.User == username)
                    {
                        List <string> usersList = JsonConvert.DeserializeObject <List <string> >(packet.Message);

                        foreach (string user in usersList)
                        {
                            Invoke(addUser, user);
                        }
                    }
                    break;

                case "statchange":
                    ConnectedUserBlock selectedUser = (ConnectedUserBlock)Panel_ConnectedUsersList.Controls.Find(packet.User, false)[0];

                    switch (packet.Message)
                    {
                    case "0":
                        selectedUser.User_OnlineStatus.BackColor = Color.FromArgb(147, 196, 125);
                        break;

                    case "1":
                        selectedUser.User_OnlineStatus.BackColor = Color.FromArgb(255, 217, 102);
                        break;

                    case "2":
                        selectedUser.User_OnlineStatus.BackColor = Color.FromArgb(224, 102, 102);
                        break;

                    case "3":
                        selectedUser.User_OnlineStatus.BackColor = Color.FromArgb(153, 153, 153);
                        break;
                    }
                    break;

                default:
                    try { Invoke(addRichMsg, new Tuple <string, string>(packet.User, packet.Message)); }
                    catch { break; }
                    break;
                }
            }
        }