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.