- Code: Select all
using PlayerIOClient;
using UnityEngine;
using System.Collections;
public class MultiplayerManager : MonoBehaviour {
public MultiplayerGUI gui;
private Client client;
private RoomInfo[] rooms;
private Connection connection;
public bool joinedRoom;
// Use this for initialization
void Start() {
PlayerIO.UnityInit(this);
// create a random userid
// TODO enable quickconnect kong / facebook accounts
string userid = "Guest" + Random.Range(0, 10000);
PlayerIO.Connect(
"surrender-xbaoyl1lkqvzfwma7rzdq", // 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.
userid, // 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
OnConnectSuccess,
OnError
);
}
private void OnError(PlayerIOError error) {
gui.ShowError(error.ToString());
}
private void OnConnectSuccess(Client client) {
this.client = client;
client.Multiplayer.DevelopmentServer = new ServerEndpoint("192.168.0.9", 8184);
StartCoroutine(PollRoomList());
gui.Init(MultiplayerGUI.Mode.InLobby);
}
IEnumerator PollRoomList() {
while (!joinedRoom) {
client.Multiplayer.ListRooms("GameRoom", null, 0, 0, OnRoomList);
yield return new WaitForSeconds(3f);
}
}
void OnRoomList(RoomInfo[] rooms) {
this.rooms = rooms;
gui.UpdateRoomList(rooms);
}
public void JoinRoom(string roomId) {
client.Multiplayer.JoinRoom(roomId, null, OnJoinedRoom, OnError);
}
public void CreateNewRoom(string roomName, string roomPassword) {
//Create or join the room
client.Multiplayer.CreateJoinRoom(
roomName, //Room id. If set to null a random roomid is used
"GameRoom", //The room type started on the server
true, //Should the room be visible in the lobby?
null,
null,
OnJoinedRoom,
OnError);
}
private void OnJoinedRoom(Connection newConnection) {
connection = newConnection;
Debug.Log("Joined room!");
//connection.OnMessage += handlemessage;
gui.Init(MultiplayerGUI.Mode.Joined);
connection.OnMessage += HandleMessage;
joinedRoom = true;
}
void HandleMessage(object sender, PlayerIOClient.Message m) {
Debug.Log(m);
}
}
Now the problem is in the OnJoinedRoom callback. None of those lines actually do anything they are supposed to. The debug.log does print the message Joined room to the console but it has no stack trace whatsoever which is at the very least bizarre... it doesn't set joinedRoom to true and my gui.init call doesn't get called. Strange. The OnConnectSuccess callback works fine and is formatted the same way. Is there something going on behind the scenes I'm not aware of?
Many thanks