-
Notifications
You must be signed in to change notification settings - Fork 3
5) Lobby Quickstart
Nicholas Ventimiglia edited this page Dec 27, 2015
·
2 revisions
The lobby example is a wraps the ortc client and adds common game server functionality. Here is a logical breakdown
- Uses may join a connect with a userId and userName
- Users may join a global 'lobby' room.
- While in the lobby :
- Users will receive presence notifications of the other users
- Users will receive notifications of public rooms to join
- Users may send chat and RPC messages to one another
- Users may create or join game rooms.
- While in a room :
- Users will receive presence notifications of the other users
- Users may send chat and RPC messages to one another
- An 'Authority' (client) is selected based on join time.
// Construct and configure an ortc client
_ortc = new UnityOrtcClient();
_ortc.ClusterUrl = URL;
// pass this client to create a new lobby service
_lobby = LobbyService.Init(_ortc, ApplicationKey, PrivateKey, URL, true);
There are a number of events that you can subscribe to.
/// <summary>
/// Connection state. eg : connected, reconnecting
/// </summary>
public event Action<ConnectionState> OnState = delegate { };
/// <summary>
/// Raised when a new room is available. This is in response to the FindRoom request
/// </summary>
public event Action<RoomFindResponse> OnRoomFound = delegate { };
/// <summary>
/// when a peer joins to the lobby
/// </summary>
public event Action<UserDetails> OnLobbyUserAdd = delegate { };
/// <summary>
/// when a peer leaves the lobby
/// </summary>
public event Action<UserDetails> OnLobbyUserRemove = delegate { };
/// <summary>
/// when a peer is added to the room
/// </summary>
public event Action<UserDetails> OnRoomUserAdd = delegate { };
/// <summary>
/// when a peer is removed from the room
/// </summary>
public event Action<UserDetails> OnRoomUserRemove = delegate { };
/// <summary>
/// when a the room authority changes
/// </summary>
public event Action<UserDetails> OnRoomAuthority= delegate { };
/// <summary>
/// Raised when self joins the lobby
/// </summary>
public event Action OnLobbyEnter = delegate { };
/// <summary>
/// Raised when self leaves the lobby
/// </summary>
public event Action OnLobbyExit = delegate { };
/// <summary>
/// Raised when self leaves a room
/// </summary>
public event Action OnRoomExit = delegate { };
/// <summary>
/// Raised when self joins a room
/// </summary>
public event Action<RoomDetails> OnRoomEnter = delegate { };
/// <summary>
/// Raised when the room is updated. eg, authority changes
/// </summary>
public event Action<RoomDetails> OnRoomUpdate = delegate { };
var user = new UserDetails
{
UserId = Guid.NewGuid().ToString(),
UserName = Application.platform + " " + UnityEngine.Random.Range(0, 1000),
};
_lobby.Connect("AuthKey", user, state =>
{
// Continue after connection or failure
});
The lobby is a special room designed for global chat and room discovery.
// Join the Lobby
_lobby.JoinLobby(result =>
{
Debug.Log(result ? "In Lobby !" : "Error");
});
// Leave the lobby. This will hide your room and your user from public view.
_lobby.LeaveLobby();
// Find peers
var users = _lobby.LobbyUser;
// Lobby Chat
_lobby.SendLobbyChat("Hello From " + _lobby.User.UserName);
// Find other rooms.
_lobby.FindRooms(room =>
{
Debug.Log("Found Room : "+ room.Room.RoomName);
});
A room is a glorified channel. Consider it a logical game zone. Each room as an 'authority'. The authority is the first user who joined. If the first user leaves, authority automatically changes to the next user in order of when they joined.
// Create a room
_lobby.CreateRoom(_lobby.User.UserName, result =>
{
Debug.Log(result ? "Room Created !" : "Error");
});
// Join a room
_lobby.JoinRoom(_lastRoom, result =>
{
Debug.Log(result ? "In Room !" : "Error");
});
// Leave the Room.
_lobby.LeaveRoom();
// Find peers
var users = _lobby.RoomUsers;
// Get the Authority
var authority = _lobby.RoomAuthority;
// Room Chat
_lobby.SendRoomChat("Hello From " + _lobby.User.UserName);
// Personal Chat
_lobby.SendUserChat(friend.UserId, "Hello From " + _lobby.User.UserName);
Key to the lobby system is the ability to define, send, and receive poco rpcs.
// Define a rpc. It is a clr object inherited from LobbyBehaviour
[Serializable]
public class CustomRPC : LobbyMessage
{
public string Message;
}
// Register a handler.
LobbyMessenger<CustomRPC>(OnCustomRPC);
void OnCustomRPC(string channel, CustomRPC model)
{
Debug.Log("Got Custom RPC " + model.Message);
}
// Send a message
_lobby.SendUserRPC(friend.UserId, new CustomRPC { Message = _lobby.User.UserName });
_lobby.SendRoomRPC(new CustomRPC { Message = _lobby.User.UserName });
_lobby.SendLobbyRPC(new CustomRPC { Message = _lobby.User.UserName });