Пример #1
0
        public void StartReceive()
        {
            while (true)
            {
                var stream = _tcpClient.GetStream();
                stream.Read(_messageLenght, 0, _messageLenght.Length);

                var data = new byte[BitConverter.ToInt32(_messageLenght, 0)];
                stream.Read(data, 0, data.Length);

                var message = new Message();
                message.ReadBytes(data);

                if (MessageReceived != null)
                {
                    var thread = new Thread(() => MessageReceived(this, message));
                    thread.Start();
                }

        
               

             


            }
            

        }
Пример #2
0
        async public Task StartReceiveAsync()
        {
            while (true)
            {
                var stream = _tcpClient.GetStream();

                var mesLen = stream.ReadAsync(_messageLenght, 0, _messageLenght.Length);
                await mesLen;

                //mesLen.Result()'
                var data = new byte[BitConverter.ToInt32(_messageLenght, 0)];
                await stream.ReadAsync(data, 0, data.Length);

                var message = new Message();
                message.ReadBytes(data);

                if (MessageReceived != null)
                {
                    //var thread = new Thread(() => MessageReceived(this, message));
                    //thread.Start();

                    //await MessageReceivedAsync(this, message);

                    await Task.Run(() => MessageReceived(this, message));
                }

        
               

             


            }
            

        }
Пример #3
0
        public void SendMessage(Message message)
        {
          

            this._messageQueue.Enqueue(message);
        }
Пример #4
0
        void OnMessageReceived(Client client, Message message)
        {
            if (!client.NicknameReceived)
            {
                client.Nickname = message.MessageText;
                client.NicknameReceived = true;
                Broadcast(new Message
                {
                    ClientId = 0,
                    MessageText = $"ACTION={ServerActions.NewClientConnected};NEWCLIENTID={client.ClientId};NICKNAME={client.Nickname}"
                });

                
                return;

            }

            Broadcast(message);

          
            
        }
Пример #5
0
 void Broadcast(Message message)
 {
     foreach (var client in _clients)
     {
         client.SendMessage(message);
     }
 }
Пример #6
0
        void OnMessageReceived(Client client, Message message)
        {
            if (!client.NicknameReceived)
            {
                client.Nickname = message.MessageText;
                client.NicknameReceived = true;
                Broadcast(new Message
                {
                    ClientId = 0,
                    MessageText = $"NEWCLIENTID={client.ClientId};NICKNAME={client.Nickname}"
                });

                
                return;

            }

            Broadcast(message);

          
            
        }
Пример #7
0
 async private Task MessageReceivedAsync(Client client, Message message)
 {
   
     await Task.Run(() => MessageReceived(client, message));
    
 }