Пример #1
0
        //功能 : 廣播訊息給所有使用者 (如果連線列表有更新的話優先傳送)
        public void messageBroadcast(object sender, TalkmessageEventArgs e)
        {
            BinaryFormatter bin = new BinaryFormatter();

            foreach (Talkconnection talkconnection in connectionlist)
            {
                if (talkconnection.Client.Connected)
                {
                    NetworkStream clientStream = talkconnection.Client.GetStream();
                    byte[]        messageType  = encoder.GetBytes(e.MessageType);
                    byte[]        message      = new byte[4096];

                    clientStream.Write(messageType, 0, messageType.Length);
                    if (e.MessageType.CompareTo("TEXT") == 0)
                    {
                        clientStream.Write(encoder.GetBytes(e.Message), 0, e.Message.Length);
                    }
                    if (e.MessageType.CompareTo("USER") == 0)
                    {
                        bin.Serialize(clientStream, userlist);
                    }
                }
                else
                {
                    continue;
                }
            }
        }
Пример #2
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();
        }