Example #1
0
        private void clientProc(object client)
        {
            //獲取網路資訊
            TcpClient      tcpClient    = (TcpClient)client;
            NetworkStream  clientStream = tcpClient.GetStream();
            Talkconnection connection   = new Talkconnection(tcpClient);

            //建立使用者資訊
            byte[] nickname = new byte[4096];
            clientStream.Read(nickname, 0, nickname.Length);
            connection.ClientUser.Username = encoder.GetString(nickname);

            //新增到訊息處理器
            talkmessagehandler.addconnection(connection);
            connection.messageComing(new TalkmessageEventArgs("", connection.ClientUser.Username, "USER", connection));

            updateUI("[", "Monitor");
            updateUI(connection.ClientUser.Username, "Monitor");
            updateUI(" is online.]\n", "Monitor");
            updateUI(connection.ClientUser.Username, "List");



            //從client不斷接收訊息
            while (true)
            {
                byte[] message   = new byte[4096];
                int    bytesRead = 0;

                try
                {
                    //從client讀入訊息
                    bytesRead = clientStream.Read(message, 0, message.Length);

                    //觸發messageEvent (通知talkmessagehandler 廣播訊息)
                    TalkmessageEventArgs e = new TalkmessageEventArgs(Encoding.Unicode.GetString(message), connection.ClientUser.Username, "TEXT", connection);
                    connection.messageComing(e);
                    //更新ServerUI
                    updateUI(e.Message, "Monitor");
                }
                catch
                {
                    //在Server monitor上顯示錯誤訊息
                }
                if (bytesRead == 0)
                {
                    break;
                }
            }
            talkmessagehandler.removeconnection(connection);
            connection.messageComing(new TalkmessageEventArgs("", connection.ClientUser.Username, "USER", connection));

            updateUI(connection.ClientUser.Username, "List-r");
            updateUI("[", "Monitor");
            updateUI(connection.ClientUser.Username, "Monitor");
            updateUI(" is offline.]\n", "Monitor");

            tcpClient.Close();
        }
Example #2
0
 //功能 : 刪除一個Talkconnection , 並且取消訂閱其事件 (在server的clientproc中當client離線時執行)
 //       刪除一個User
 public void removeconnection(Talkconnection talkconnection)
 {
     connectionlist.Remove(talkconnection);
     userlist.Remove(talkconnection.ClientUser);
     talkconnection.messageEvent -= messageBroadcast;
 }
Example #3
0
 //功能 : 添加一個Talkconnection 到connectionlist中 , 並且訂閱其事件
 //       添加一個User 到 userlist中
 public void addconnection(Talkconnection talkconnection)
 {
     connectionlist.Add(talkconnection);
     userlist.Add(talkconnection.ClientUser);
     talkconnection.messageEvent += new messageEventhandler(messageBroadcast);
 }