예제 #1
0
 /// <summary>
 /// 向指定客户端发送消息
 /// </summary>
 /// <param name="ClientID">客户端ID</param>
 /// <param name="msg">消息字符串</param>
 public void SendMessageToClientByID(int ClientID, string msg)
 {
     ClientList.Find(i => i.ClientID == ClientID).Send(new Data(HeadInformation.Message, new Message()
     {
         ClientID = -1, ToClientID = ClientID, Msg = msg
     }));
 }
예제 #2
0
        /// <summary>
        /// 接收客户端消息
        /// </summary>
        /// <param name="client">客户端实例</param>
        void ReceiveMessages(object client)
        {
            try
            {
                Client _client = (Client)client;

                while (true)
                {
                    // 接收客户端消息
                    Data data = _client.Receive();

                    // 触发事件
                    ReceiveMessages_Event?.Invoke(data, _client);

                    // 判断头部信息类型
                    if (data.HeadInfo == HeadInformation.Message) // 消息
                    {
                        // 判断是否为私聊消息
                        if (data.Data_Message.ToClientID != 0) // 私聊
                        {
                            // 发送私聊消息
                            SendMessageToClientByID(data.Data_Message.ToClientID, data);
                        }
                        else // 群聊
                        {
                            // 广播群聊消息
                            BroadcastMessage(data);
                        }
                    }
                    else if (data.HeadInfo == HeadInformation.ClientOffline) // 客户端离线
                    {
                        ClientList.Remove(ClientList.Find(i => i.ClientID == data.Data_Message.ClientID));

                        // 广播客户端离线消息
                        BroadcastMessage(new Data(HeadInformation.ClientOffline, new Message()
                        {
                            ClientID = data.Data_Message.ClientID, ClientName = data.Data_Message.ClientName
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                // 终止当前线程
                Thread.CurrentThread.Abort();
            }
        }
예제 #3
0
 /// <summary>
 /// 向指定客户端发送消息
 /// </summary>
 /// <param name="ClientID">客户端ID</param>
 /// <param name="data">消息data</param>
 void SendMessageToClientByID(int ClientID, Data data)
 {
     ClientList.Find(i => i.ClientID == ClientID).Send(data);
 }