Tutorial Simple Chat - Client Code

<Previous Chat Server><Next Chat Tutorial Server Code Listing>
Pixi Client setup
The idea is to create a WPF form that uses a GameNetwork object to communicate with the server.
  1. Start by creating a new WPF project and name it PixiChatClient. In the MainWindow.xaml file replace the <Grid></Grid> tags with the following xaml code:
    <StackPanel>
        <StackPanel Orientation="Horizontal" >
            <Label Content="User Name" Margin="3" />
            <TextBox Name="txtUserName" Width="100" Margin="3" />
            <Button Name="btnLogin" Content="Login" Margin="3" Click="btnLogin_Click" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" >
            <Label Content="Message" />
            <TextBox Name="txtMessage" Width="400" />
            <Button Name="btnSend" Content="Send" Click="btnSend_Click" />
        </StackPanel>
        <StackPanel>
            <ListBox Name="lstChat" Height="300">
            </ListBox>
        </StackPanel>
    </StackPanel>
  1. Add references to the Pixi.PC.Client.dll, Pixi.Client.Network.dll, Pixi.Network.dll and Pixi.Logging.dl by right-clicking the References folder in the Solution Explorer and browsing to where the those libraries are located.
  2. Add the following using statements to the top of the MainWindow.xaml.cs class:
using System.Windows.Threading;

using PixiChatClient.Network;
using Pixi.Client.Network;
using Pixi.Logging;
using Pixi.PC.Client;
using Pixi.Network;
  1. Add a module level DispatcherTimer variable:
        private DispatcherTimer _timer;
  1. Add the following code to the MainWindow constructor.
        public MainWindow()
        {
            InitializeComponent();

            //Initialize everything
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(10);
            _timer.Tick += new EventHandler(MessagLoop);
            _timer.Start();
        }
  1. Create the MessageLoop event handler to be used for receiving messages from the ClientNetwork:
        private void MessageLoop(object sender, EventArgs e)
        {
        }
Common Code
Add the existing PixiChat.Common project that was created when we built the Chat Server.
Game Network
To talk to the server, a class must be created that derives from the ClientNetwork class of the Pixi.PC.Client. We'll create a new project with a class named GameNetwork.
  1. Add a new Class Library project to the solution and name it PixiChatClient.Network.
  2. Add a referenct to the PixiChat.Common project.
  3. Add references to the Pixi.PC.Client.dll, Pixi.Client.Network.dll, and Pixi.Logging.dl by right-clicking the References folder in the Solution Explorer and browsing to where the those libraries are located.
  4. Create a new class and name it GameNetwork and add the following using statements:
using Pixi.PC.Client;
using Pixi.Client.Network;
using Pixi.Logging;
using PixiChat.Common;
  1. Derive the class from the ClientNetwork class and add the following constructor:
        public GameNetwork(IClientTransceiverFactory clientTransceiverFactory, ILogger logger)
            : base(clientTransceiverFactory, logger)
        {
        }
  1. Add a method to handle sending Chat messages to the server:
        public void SendChatMessage(string chatMessage)
        {
            base.SendUDP((byte)GameMessageClass.ChatMessage, 
                (short)GameMessageType.ChatSend, chatMessage, 
                NetTransportType.Reliable);
        }
Finish Client
To finish up the client, we just need to add a reference to the PixiChatClient.Network project, add code to the MessageLoop method and create event methods for the two buttons.
  1. Add a reference to the PixiChatClient.Network project and add the following using statement:
using PixiChat.Common;
  1. Add a module level GameNetwork variable:
               private GameNetwork _gameNetwork;
  1. Add the following code to the MainWindow constructor just after the InitializeComponent() line:
            IClientTransceiverFactory factory = new ClientTransceiverFactory();
            ILogger logger = new Logger();

            //Initialize everything
            _gameNetwork = new GameNetwork(factory, logger);
            _gameNetwork.Start("192.168.1.114", 11000, 11001, 0, 100);
  1. In the MessageLoop method, add the following code to handle the LoginSuccess and ChatReply messages from the server. Be sure to call the Update method on the gameNetwork object:
            try
            {
                _gameNetwork.Update();
                ClientNetMessage msg = _gameNetwork.ReadMessage();

                if (msg != null)
                {
                    if (msg.MessageClass == (byte)SystemMessageClass.System)
                    {
                        switch (msg.MessageType)
                        {
                            case (short)SystemMessageType.LoginSuccess:
                                MessageBox.Show("Login Succeeded", "Login Success", MessageBoxButton.OK);
                                btnSend.IsEnabled = true;
                                txtMessage.IsEnabled = true;
                                break;
                        }
                    }
                    if (msg.MessageClass == (byte)GameMessageClass.ChatMessage)
                    {
                        switch (msg.MessageType)
                        {
                            case (short)GameMessageType.ChatReply:
                                string message = msg.Data.ToString();
                                lstChat.Items.Add(message);
                                txtMessage.Text = "";
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                _gameNetwork.Stop();
            }
  1. Finally add the following two methods to handle the button click events:
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtUserName.Text))
            {
                MessageBox.Show("You must enter a user name.");
                return;
            }
            _gameNetwork.Login(txtUserName.Text, "");

            btnLogin.IsEnabled = false;
            txtUserName.IsEnabled = false;
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            _gameNetwork.SendChatMessage(txtMessage.Text);
        }
Final Steps
Start the PixiChatServer. Start the PixiChatClient and start chatting.

Last edited Feb 3, 2012 at 3:38 AM by pixiserver, version 21

Comments

No comments yet.