コード例 #1
0
ファイル: Server.cs プロジェクト: Soarc/Intership
        void HandleClient(Client newClient)
        {
            

            _clients.Add(newClient);

       

            var sendThread = new Thread(newClient.StartSend);
            sendThread.Start();

            var receiveThread = new Thread(newClient.StartReceive);
            receiveThread.Start();
           
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: Soarc/Intership
        async public void Start()
        { 
            _listener = new TcpListener(IPAddress.Any, Config.Port);
            _listener.Start();

            while (true)
            {
                var tcpClient = _listener.AcceptTcpClient();
                var newClient = new Client(tcpClient);
                await HandleClientAsync(newClient);
            }
        

            

        }
コード例 #3
0
ファイル: Server.cs プロジェクト: Soarc/Intership
        async Task HandleClientAsync(Client newClient)
        {
            

            _clients.Add(newClient);



            newClient.ClientId = ++_currentClientId;

            //var sendThread = new Thread(newClient.StartSend);
            //sendThread.Start();


            await newClient.StartSendAsync();

            newClient.SendMessage(new Message
            {
                ClientId = 0,
                MessageText = $"ACTION={ServerActions.ClientId};CLIENTID={newClient.ClientId}",
            });

            foreach (var client in _clients)
            {
                if (client == newClient)
                    continue;

                newClient.SendMessage(new Message
                {
                    ClientId = 0,
                    MessageText =$"ACTION={ServerActions.NewClientConnected};NEWCLIENTID={client.ClientId};NICKNAME={client.Nickname}",
                });

            }


            newClient.MessageReceived += OnMessageReceived;

            //var receiveThread = new Thread(newClient.StartReceive);
            //receiveThread.Start();


            await newClient.StartReceiveAsync();
          
          
           
        }
コード例 #4
0
ファイル: Server.cs プロジェクト: Eva113/Intership
        void HandleClient(Client newClient)
        {
            

            _clients.Add(newClient);



            newClient.ClientId = ++_currentClientId;

            var sendThread = new Thread(newClient.StartSend);
            sendThread.Start();

           


            newClient.SendMessage(new Message
            {
                ClientId = 0,
                MessageText = $"CLIENTID={newClient.ClientId}",
            });

            foreach (var client in _clients)
            {
                if (client == newClient)
                    continue;

                newClient.SendMessage(new Message
                {
                    ClientId = 0,
                    MessageText =$"CLIENTID={client.ClientId};NICKNAME={client.Nickname}",
                });

            }


            newClient.MessageReceived += OnMessageReceived;

            var receiveThread = new Thread(newClient.StartReceive);
            receiveThread.Start();

          

          
          
           
        }
コード例 #5
0
ファイル: Server.cs プロジェクト: Soarc/Intership
        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);

          
            
        }
コード例 #6
0
ファイル: Server.cs プロジェクト: Eva113/Intership
        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
ファイル: Client.cs プロジェクト: Soarc/Intership
 async private Task MessageReceivedAsync(Client client, Message message)
 {
   
     await Task.Run(() => MessageReceived(client, message));
    
 }