Chat Tutorial Client Code Listing

<Previous Chat Tutorial Server Code Listing><Next Chat Tutorial Client Code Listing>

PixiChatClient

MainWindow.xaml
<Window x:Class="PixiChatClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <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>
</Window>
MainWIndow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using PixiChatClient.Network;
using Pixi.Client.Network;
using Pixi.Logging;
using Pixi.PC.Client;
using Pixi.Network;
using PixiChat.Common;

namespace PixiChatClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GameNetwork _gameNetwork;
        private DispatcherTimer _timer;

        public MainWindow()
        {
            InitializeComponent();
            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);
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(10);
            _timer.Tick += new EventHandler(MessageLoop);
            _timer.Start();

        }

        private void MessageLoop(object sender, EventArgs e)
        {
            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();
            }
        }

        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);
        }
    }
}

PixiChatClient.Network

GameNetwork.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pixi.PC.Client;
using Pixi.Client.Network;
using Pixi.Logging;
using PixiChat.Common;

namespace PixiChatClient.Network
{
    public class GameNetwork : ClientNetwork
    {
        public GameNetwork(IClientTransceiverFactory clientTransceiverFactory, ILogger logger)
            : base(clientTransceiverFactory, logger)
        {
        }

        public void SendChatMessage(string chatMessage)
        {
            base.SendUDP((byte)GameMessageClass.ChatMessage, 
                (short)GameMessageType.ChatSend, chatMessage, 
                NetTransportType.Reliable);
        }

    }
}

Last edited Feb 3, 2012 at 4:12 AM by pixiserver, version 3

Comments

No comments yet.