Forum C# CreatJoinRoom callback, please help

CreatJoinRoom callback, please help

Postby atizoa » July 14th, 2012, 9:53 am

Hello player.IO community, I am completely new to PLAYER.IO. I do know basic c# , though the most experience I've had with c# was a 1 week course, and skimming through a book about it. I have been reading and taking in slowly how to work with PLAYER.IO for the past two days. I've come across "Building Flash Multiplayer Games" and have been mainly learning from there. Along the way of reading that article I have been reading the net.client reference to make sure I understand, at a decent level, each of the PLAYER.IO functions.
Here's one of my problems though, I'm not using flash - at all, though the way things are coded look fairly similar to C#. I plan on making the client(coding) through c#, and not knowing where to look is bringing me down. I have been messing with code from the player.io client example, trying to add too it but honestly I'm getting no where.
I noticed that in the flash tutorial
Code: Select all
PlayerIO.connect(
    stage,          //Room id. If set to null a random roomid is used
    gameID,         //The game type started on the server
    "public",       //Should the room be hidden from the lobby?
    "GuestUser",    //Room data.
    "",             //User join data
    >>handleConnect<<,  //Function executed on successful joining of the room
    handleError     //Function executed if we got a join error 
);
   function handleConnect(client:Client){

that the variable "handleConnect" was placed in, then managed inside a function that would perform if true. I can't even get this to work in the c# example - I've spent 2 hours trying to figure out how to get "Callback (playerioclient.connection)" to work . I have also researched how delegates work and ways of using them, since that's what the callback was.

I have tried searching examples, articles, and even just bits and pieces for c# player.io client or server, but no luck. I am more than willing to work hard to learn how to use player.io c#. Honestly I have no idea where to start or look. If you know where I can find a good resource on c# clients/servers or both, I'd greatly appreciate it.

Sorry if my post broke any rules, or is in the wrong area.
atizoa
 
Posts: 11
Joined: July 10th, 2012, 9:11 pm

Re: CreatJoinRoom callback, please help

Postby Henrik » July 15th, 2012, 11:12 am

http://playerio.com/documentation/refer ... teJoinRoom

If you check the manual, you can see that there are three versions of CreateJoinRoom, one that takes no callbacks, and two that does. The first method is synchronous, which means that it will wait until it has joined a room, and then return a Connection to it. Any errors are handled by the method itself throwing exceptions if something goes wrong.

The other two methods are asynchronous, which means that they won't wait for the room to be joined, they'll return immediately. But when the room is joined, the success callback will be called and executed in another thread. If there was an error, the error callback will be called.

Asynchronous programming can be pretty tricky, are there any specific reasons you want to use that model? If not, I'd suggest you use the synchronous methods in the .Net client instead, they're much easier to work with and to understand the program flow. The synchronous version of CreateJoinRoom is used like this:

Code: Select all
var conn = client.MultiPlayer.CreateJoinRoom ("roomid", "roomtype", true, null, null);
conn.Send("hello", 1, 2, 3);


The Callback<Connection> you see for the asynchronous methods means that the successCallback parameter has to be a method that takes a single parameter, a Connection, since that is what is returned when you join the room. Likewise, the error handling is through the Callback<PlayerIOError> errorCallback. There are many ways to do that call, the best way is to use anonymous delegates, like this:

Code: Select all
client.MultiPlayer.CreateJoinRoom("roomid", "roomtype", true, null, null,
    delegate(Connection conn) {
        conn.Send("hello", 1, 2, 4);
    },
    delegate(PlayerIOError err) {
        Console.WriteLine(err);
    }
);


But you could use an already declared named method, like this:

Code: Select all
private void myLittleMethod(Connection conn) {
    conn.Send("hullo", 1, 2, 3);
}

//And in some other method:
client.MultiPlayer.CreateJoinRoom("roomid", "roomtype", true, null, null, myLittleMethod);


And finally, you can assign a delegate to a variable and use that, like this:

Code: Select all
var myLittlePony = delegate(Connection conn) {
    conn.Send("hello", 1, 4, 9);
};

client.MultiPlayer.CreateJoinRoom("roomid", "roomtype", true, null, null, myLittlePony);
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: CreatJoinRoom callback, please help

Postby atizoa » July 16th, 2012, 7:19 am

Thank you very much Henrik. Yes, there is a reason why I need to use this. The reason I "think" I need to use this is I'm making the client "multi-player" coding as a .dll, and I need the .dll to send back to the program running it certain things(true/false,etc).

If I made the client coding as a dll (if possible), would I need to include anything from the build besides the main dll?
atizoa
 
Posts: 11
Joined: July 10th, 2012, 9:11 pm

Re: CreatJoinRoom callback, please help

Postby Henrik » July 16th, 2012, 11:18 am

atizoa wrote:Thank you very much Henrik. Yes, there is a reason why I need to use this. The reason I "think" I need to use this is I'm making the client "multi-player" coding as a .dll, and I need the .dll to send back to the program running it certain things(true/false,etc).

Ok, but if you are making a class library, you will have some sort of public class with public methods that internally uses the Player.IO methods. If you use the synchronous methods, your class library will be synchronous. And if you use the asynchronous methods, your library will be asynchronous. Or you can do both, and let the one using your class library decide. That choice has nothing to do with you making it a class library or standalone executable or whatever.

atizoa wrote:If I made the client coding as a dll (if possible), would I need to include anything from the build besides the main dll?

Your dll will reference the PlayerIOClient.dll, so that needs to be included, but whoever is using your dll only needs to reference yours.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm


Return to C#



cron