Forum C# C# Client question..

C# Client question..

Postby woodman231 » May 17th, 2011, 7:41 pm

Hello,

I am just trying to get a proper message handler implemented in a WPF C# client.

So far my code works well enough to connect to/ create a multiplayer join room. It can also recieve messages and update a textbox.

The problem I am having is making the objects more universal to use the class functions and objects in other controls.

For example I am working mostly off of the Program.cs example given in the .net framwork client example.

So here is my code. Basically at this point I am not sure how to get my send button to actually do anything. If I run multiple clients they all connect and they all see the join messages but I currently have no way of entering in data to a chat and sending it:

Code: Select all
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 System.Threading;
using PlayerIOClient;


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string recieved;
       
        public MainWindow()
        {
            InitializeComponent();
            var client = PlayerIO.Connect(
                "my game id",   // Game id (Get your own at playerio.com. 1: Create user, 2:Goto admin pannel, 3:Create game, 4: Copy game id inside the "")
                "public",                  // The id of the connection, as given in the settings section of the admin panel. By default, a connection with id='public' is created on all games.
                "woodman231",                  // The id of the user connecting. This can be any string you like. For instance, it might be "fb10239" if you´re building a Facebook app and the user connecting has id 10239
                null,                     // If the connection identified by the connection id only accepts authenticated requests, the auth value generated based on UserId is added here
                null                // The partnerid to tag the user with, if using PartnerPay
            );
           
            var connection = client.Multiplayer.CreateJoinRoom("my-room-id", "bounce", true, null, null);

            textBox1.AppendText("Joined Room\r\n");

            connection.Send("Hello World");

            connection.OnMessage += delegate(object sender, PlayerIOClient.Message m)
            {
                recieved = m.ToString();
                new Thread(Work).Start();
            };
        }

        void Work()
        {
            UpdateMessage();
        }

        void UpdateMessage ()
        {
            Action action = () => textBox1.AppendText(recieved + "\r\n");
            Dispatcher.Invoke(action);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           //Unable to access the client or connection variable made in the MainWindow() function so how am I going to send  message to the currently connected client?
        }

    }
}



This form just has a textbox to show the messages recieved, then another text box to be used as an input, and a submit button to send the input to all clients.

Any help would be appreciated.

Thanks,
Sean W.
woodman231
 
Posts: 3
Joined: May 17th, 2011, 7:08 pm

Re: C# Client question..

Postby Henrik » May 18th, 2011, 12:28 pm

I haven't worked with WPF windowed apps so I could be wrong, but I'm guessing that a single instance of your MainWindow class is created, so you should be able to put some fields on it that can be accessed from the other methods. In your constructor you do var client = ..., which initializes a client, but only within the scope of the constructor, so when you exit that method, it's gone. Try something like this instead:

Code: Select all
public partial class MainWindow : Window {
    public string recieved;
    private PlayerIOClient.Client client;
    private PlayerIOClient.Connection connection;
       
    public MainWindow() {
        InitializeComponent();
        client = PlayerIO.Connect(...);
        //...
        connection = client.Multiplayer.CreateJoinRoom(...);
        //...
    }

    private void button1_Click(object sender, RoutedEventArgs e) {
        connection.Send(...);
    }
}
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: C# Client question..

Postby woodman231 » May 18th, 2011, 3:12 pm

Yep that is pretty much exactly what I ended up doing.

So fully it turned out to be like this:

Code: Select all
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 System.Threading;
using PlayerIOClient;


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string recieved;
        public Client a;
        public Connection b;
        public MainWindow()
        {
            InitializeComponent();
            a = PlayerIO.Connect(
                "mystring",   // Game id (Get your own at playerio.com. 1: Create user, 2:Goto admin pannel, 3:Create game, 4: Copy game id inside the "")
                "public",                  // The id of the connection, as given in the settings section of the admin panel. By default, a connection with id='public' is created on all games.
                "woodman231",                  // The id of the user connecting. This can be any string you like. For instance, it might be "fb10239" if you´re building a Facebook app and the user connecting has id 10239
                null,                     // If the connection identified by the connection id only accepts authenticated requests, the auth value generated based on UserId is added here
                null                // The partnerid to tag the user with, if using PartnerPay
            );
           
            b = a.Multiplayer.CreateJoinRoom("my-room-id", "bounce", true, null, null);

            textBox1.AppendText("Joined Room\r\n");

            b.OnMessage += delegate(object sender, PlayerIOClient.Message m)
            {
                recieved = sender.ToString() + " " + m.ToString();               
                new Thread(Work).Start();         
            };
        }

        void Work()
        {
            UpdateMessage();
        }

        void UpdateMessage ()
        {
            Action action = () => textBox1.AppendText(recieved + "\r\n");
            Dispatcher.Invoke(action);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            b.Send("ChatMessage", textBox2.ToString());           
        }

    }
}



They key part for me was declaring it as Client and Connection instead of just "var" :).

Thanks,
Sean W.
woodman231
 
Posts: 3
Joined: May 17th, 2011, 7:08 pm


Return to C#



cron