Exemplo n.º 1
0
        private void ClientReceived(Client sender, TransferPackage package)
        {
            switch (package.TransferAction)
            {
            case TransferAction.Login:
                if (ClientLogedIn != null)
                {
                    ClientLogedIn(sender);
                }
                break;

            case TransferAction.SendMessage:
                MessageModel messageModel = package.Data as MessageModel;
                Client       targetClient = Clients.FirstOrDefault(x => x.Id == messageModel.ClientId);
                if (targetClient != null)
                {
                    messageModel.ClientName = targetClient.Name;

                    TransferPackage pkg = new TransferPackage(TransferAction.SendMessage, new MessageModel
                    {
                        ClientId   = sender.Id,
                        ClientName = sender.Name,
                        Message    = messageModel.Message
                    });

                    targetClient.Socket.Send(pkg.ToByteArray());

                    if (ClientSendMessage != null)
                    {
                        ClientSendMessage(sender, messageModel);
                    }
                }
                break;
            }
        }
Exemplo n.º 2
0
        private void ConnectedCallback(IAsyncResult ar)
        {
            sock.EndConnect(ar);

            TransferPackage package = new TransferPackage(TransferAction.Login, new LoginModel {
                Name = txtName.Text
            });

            sock.Send(package.ToByteArray());

            Invoke((MethodInvoker)(() => ChangeStatus(true)));

            sock.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, null);
        }
Exemplo n.º 3
0
        private void TimerCallbackAction(object state)
        {
            foreach (var client in server.Clients)
            {
                ListClientModel listClientModel = new ListClientModel
                {
                    Clients = server.Clients.Where(x => x.Id != client.Id).Select(x => new ClientModel {
                        Id = x.Id, Name = x.Name
                    }).ToArray()
                };
                TransferPackage package = new TransferPackage(TransferAction.ListClient, listClientModel);

                client.Socket.Send(package.ToByteArray());
            }
        }
Exemplo n.º 4
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtMessage.Text.Trim().Length == 0)
            {
                return;
            }

            lstMessages.Items.Add("Me: " + txtMessage.Text);

            TransferPackage package = new TransferPackage(TransferAction.SendMessage, new MessageModel
            {
                ClientId = Guid.Parse(lblClientId.Text),
                Message  = txtMessage.Text
            });

            sock.Send(package.ToByteArray());
        }