Browse Source

Use clientData to pass connection arguments to callback functions; cleans things up nicely at the expense of a bit more boilerplate code

master
Nico de Poel 5 years ago
parent
commit
4b9599b73b
  1. 40
      Assets/Scripts/EOSVoiceChat.cs

40
Assets/Scripts/EOSVoiceChat.cs

@ -45,6 +45,8 @@ public class EOSVoiceChat
public void ConnectToChat(string chatLobbyName, Action<bool> onCompleted = null, uint maxChatPlayers = DefaultMaxChatPlayers)
{
DisconnectChat(); // Leave any currently connected chat lobby
var connectArgs = new ChatConnectArgs(chatLobbyName, onCompleted, maxChatPlayers);
lobbyInterface.CreateLobbySearch(new CreateLobbySearchOptions { MaxResults = 1 }, out var searchHandle);
searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName });
@ -66,8 +68,8 @@ public class EOSVoiceChat
LobbyDetailsHandle = lobbyDetails,
PresenceEnabled = false,
},
null,
data => HandleLobbyJoined(data, onCompleted)
connectArgs,
HandleLobbyJoined
);
break;
default:
@ -86,51 +88,53 @@ public class EOSVoiceChat
BucketId = Application.productName, // TODO: do we need anything more specific than this?
EnableRTCRoom = true,
},
chatLobbyName,
data => HandleLobbyCreated(data, onCompleted)
connectArgs,
HandleLobbyCreated
);
break;
}
});
}
private void HandleLobbyCreated(CreateLobbyCallbackInfo data, Action<bool> onCompleted)
private void HandleLobbyCreated(CreateLobbyCallbackInfo data)
{
var connectArgs = (ChatConnectArgs)data.ClientData;
switch (data.ResultCode)
{
case Result.Success:
connectedLobbyId = data.LobbyId;
Debug.Log($"Chat lobby creation successful, lobby ID = {connectedLobbyId}");
onCompleted?.Invoke(true);
connectArgs.onCompleted?.Invoke(true);
break;
case Result.LobbyLobbyAlreadyExists:
// This can happen if two clients try to create the same lobby at the same time, a classic race condition.
// Try to join the other client's newly created chat lobby instead.
connectedLobbyId = null;
Debug.Log("Chat lobby already exists, attempting to join it...");
ConnectToChat((string)data.ClientData, onCompleted);
ConnectToChat(connectArgs.chatLobbyName, connectArgs.onCompleted, connectArgs.maxChatPlayers);
break;
default:
connectedLobbyId = null;
Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}");
onCompleted?.Invoke(false);
connectArgs.onCompleted?.Invoke(false);
break;
}
}
private void HandleLobbyJoined(JoinLobbyCallbackInfo data, Action<bool> onCompleted)
private void HandleLobbyJoined(JoinLobbyCallbackInfo data)
{
var connectArgs = (ChatConnectArgs)data.ClientData;
switch (data.ResultCode)
{
case Result.Success:
connectedLobbyId = data.LobbyId;
Debug.Log($"Chat lobby joined successfully, lobby ID = {connectedLobbyId}");
onCompleted?.Invoke(true);
connectArgs.onCompleted?.Invoke(true);
break;
default:
connectedLobbyId = null;
Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}");
onCompleted?.Invoke(false);
connectArgs.onCompleted?.Invoke(false);
break;
}
}
@ -184,4 +188,18 @@ public class EOSVoiceChat
if (!IsConnected)
return;
}
private class ChatConnectArgs
{
public readonly string chatLobbyName;
public readonly Action<bool> onCompleted;
public readonly uint maxChatPlayers;
public ChatConnectArgs(string chatLobbyName, Action<bool> onCompleted, uint maxChatPlayers)
{
this.chatLobbyName = chatLobbyName;
this.onCompleted = onCompleted;
this.maxChatPlayers = maxChatPlayers;
}
}
}
Loading…
Cancel
Save