Ejemplo n.º 1
0
 private void updateOnlineUserList()
 {
     if (userListBox.InvokeRequired)
     {
         UpdateUserBoxList d = new UpdateUserBoxList(updateOnlineUserList);
         userListBox.Invoke(d);
     }
     else
     {
         userListBox.Items.Clear();
         foreach (OnlineUser user in onlineUserList)
         {
             userListBox.Items.Add(user.username);
         }
     }
 }
Ejemplo n.º 2
0
 //接收消息的方法
 private void receiveData()
 {
     while (!isExit)
     {
         byte[] msg = new byte[1024];
         try
         {
             int receiveLength = clientSocket.Receive(msg, 0, 1024, SocketFlags.None);
         }
         catch
         {
             MessageBox.Show("与服务器断开了连接");
             clientSocket.Shutdown(SocketShutdown.Both);
             clientSocket.Close();
             clientSocket = null;
         }
         var receivedJson = JsonConvert.DeserializeObject <dynamic>(Encoding.UTF8.GetString(msg));
         //接收的消息为在线人列表
         if ((int)receivedJson.cmd == 4)
         {
             onlineUserList.Clear();
             var userArrayData = receivedJson.list;
             foreach (var item in userArrayData)
             {
                 OnlineUser user = new OnlineUser(item["uid"].ToString(), item["username"].ToString());
                 onlineUserList.Add(user);
             }
             UpdateUserBoxList d = new UpdateUserBoxList(updateOnlineUserList);
             userListBox.Invoke(d);
         }
         //接收的消息为一般的消息
         if ((int)receivedJson.cmd == 5)
         {
             string message           = (string)receivedJson.message;
             string user              = (string)receivedJson.user;
             string finalMessage      = $"{user}说:{message}";
             AddTalkMessageDelegate d = new AddTalkMessageDelegate(AddTalkMessage);
             userTalkRichBox.Invoke(d, finalMessage);
         }
     }
 }