Пример #1
0
    public void OnDataReceived(DataPackage dp)
    {
        ChatMessagePackage chatMessage = dp as ChatMessagePackage;

        if (chatMessage == null)
        {
            return;
        }

        print("Received chat message: " + chatMessage.Body);
    }
Пример #2
0
        /// <summary>
        /// 聊天服务的数据接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Server_DataReceive(object sender, ChatServerDataReceiveEventArgs e)
        {
            switch (e.MessageType)
            {
            case MessageType.Query:
                //查询状态,则发送自己的身份
                this.Client.SendIdentity();
                break;

            case MessageType.Identity:
                //客户端表明身份状态,则加入到客户端列表
                AddClientIP(e.RemoteEP.Address.ToString());
                break;

            case MessageType.Online:
                //有某个客户端上线,则加入到客户端列表
                AddClientIP(e.RemoteEP.Address.ToString());
                AddMessage(string.Format("## 客户机{0}已上线...", e.RemoteEP.Address.ToString()));
                break;

            case MessageType.Offline:
                //某个客户端下线
                RemoveClientIP(e.RemoteEP.Address.ToString());
                AddMessage(string.Format("## 客户机{0}已下线...", e.RemoteEP.Address.ToString()));
                break;

            case MessageType.Chat:
                //消息
                ChatMessagePackage package = ChatMessagePackage.FromPackage(e.Data);
                AddMessage(string.Format("--------------------------------------------------------------\r\n>> {0} 于 {1} 说:\r\n{2}\r\n",
                                         e.RemoteEP.Address.ToString(),
                                         package.Time.ToString("yyyy-MM-dd HH:mm:ss"),
                                         package.Message
                                         ));
                break;
            }
        }