コード例 #1
0
 public void ReadThread()
 {
     while (true)
     {
         var readBytesCount = ClientStreamContext.Read(_buffer, 0, _buffer.Length);
         if (readBytesCount > 0)
         {
             ChatStore.HandleNewMessage(_buffer, this, readBytesCount);
             Array.Clear(_buffer, 0, _buffer.Length);
         }
     }
 }
コード例 #2
0
ファイル: ChatStore.cs プロジェクト: vellykket/TcpChat
        public static void HandleNewMessage(byte[] message, SocketClient client, int readBytes)
        {
            var messageToText = Encoding.ASCII.GetString(message, 0, readBytes);

            if (client.ClientName == "")
            {
                ChatStore.HandleName(messageToText, client);
                client.ClientName = client.ClientName.Split().First();
            }
            else
            {
                if (!ConnectedClients.ContainsKey(client.SocketId))
                {
                    ChatStore.HandleNewClient(client, messageToText);
                }
                else
                {
                    var targetChat = ChatsClientMapping[client.SocketId];

                    if (messageToText.StartsWith("/"))
                    {
                        Commands(messageToText, client, targetChat);
                    }
                    else
                    {
                        foreach (var clientInChat in Chats[targetChat])
                        {
                            if (client.SocketId == clientInChat.SocketId)
                            {
                                clientInChat.SendPacketIntoSocket($"You: {messageToText}" + "\r\n");
                                continue;
                            }
                            clientInChat.SendPacketIntoSocket($"{client.ClientName}: {messageToText}" + "\r\n");
                        }
                    }
                }
            }
        }