Browse Source

Replaced callback function with events for chat connection. Also added events for disconnecting and user joining/leaving.

master
Nico de Poel 5 years ago
parent
commit
4b526227d6
  1. 34
      Assets/Scripts/EOSVoiceChat.cs
  2. 14
      Assets/Scripts/MagnificentVoiceChat.cs

34
Assets/Scripts/EOSVoiceChat.cs

@ -33,6 +33,12 @@ public class EOSVoiceChat: IDisposable
private ulong? onAudioDevicesChangedCallbackId; private ulong? onAudioDevicesChangedCallbackId;
private ulong? onConnectionChangedCallbackId, onParticipantStatusChangedCallbackId, onParticipantUpdatedCallbackId; private ulong? onConnectionChangedCallbackId, onParticipantStatusChangedCallbackId, onParticipantUpdatedCallbackId;
public event Action OnChatConnected;
public event Action OnChatConnectionFailed;
public event Action OnChatDisconnected;
public event Action<ProductUserId> OnChatUserJoined;
public event Action<ProductUserId> OnChatUserLeft;
/// <summary> /// <summary>
/// Provide the required interfaces for voice chat. Product User ID is provided through a callback, so that the /// Provide the required interfaces for voice chat. Product User ID is provided through a callback, so that the
/// same instance of this class can remain in use even if the logged in user changes. /// same instance of this class can remain in use even if the logged in user changes.
@ -59,11 +65,11 @@ public class EOSVoiceChat: IDisposable
/// Join an existing chat lobby or create a new one. /// Join an existing chat lobby or create a new one.
/// The completion callback is invoked on both success and failure, and can be used to set up the chat lobby's initial parameters. /// The completion callback is invoked on both success and failure, and can be used to set up the chat lobby's initial parameters.
/// </summary> /// </summary>
public void ConnectToChat(string chatLobbyName, Action<bool> onCompleted = null, uint maxChatPlayers = DefaultMaxChatPlayers)
public void ConnectToChat(string chatLobbyName, uint maxChatPlayers = DefaultMaxChatPlayers)
{ {
DisconnectChat(); // Leave any currently connected chat lobby DisconnectChat(); // Leave any currently connected chat lobby
var connectArgs = new ChatConnectArgs(chatLobbyName, onCompleted, maxChatPlayers);
var connectArgs = new ChatConnectArgs(chatLobbyName, maxChatPlayers);
lobbyInterface.CreateLobbySearch(new CreateLobbySearchOptions { MaxResults = 1 }, out var searchHandle); lobbyInterface.CreateLobbySearch(new CreateLobbySearchOptions { MaxResults = 1 }, out var searchHandle);
searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName }); searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName });
@ -125,7 +131,8 @@ public class EOSVoiceChat: IDisposable
chatUsers.Clear(); chatUsers.Clear();
SubscribeToRoomNotifications(); SubscribeToRoomNotifications();
connectArgs.onCompleted?.Invoke(true); // NOTE: this callback may come too soon, we probably don't have the list of participants populated yet...
OnChatConnected?.Invoke();
break; break;
case Result.LobbyLobbyAlreadyExists: case Result.LobbyLobbyAlreadyExists:
// This can happen if two clients try to create the same lobby at the same time, a classic race condition. // This can happen if two clients try to create the same lobby at the same time, a classic race condition.
@ -133,13 +140,13 @@ public class EOSVoiceChat: IDisposable
connectedLobbyId = null; connectedLobbyId = null;
rtcRoomName = null; rtcRoomName = null;
Debug.Log("Chat lobby already exists, attempting to join it..."); Debug.Log("Chat lobby already exists, attempting to join it...");
ConnectToChat(connectArgs.chatLobbyName, connectArgs.onCompleted, connectArgs.maxChatPlayers);
ConnectToChat(connectArgs.chatLobbyName, connectArgs.maxChatPlayers);
break; break;
default: default:
Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}");
connectedLobbyId = null; connectedLobbyId = null;
rtcRoomName = null; rtcRoomName = null;
Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}");
connectArgs.onCompleted?.Invoke(false);
OnChatConnectionFailed?.Invoke();
break; break;
} }
} }
@ -156,13 +163,14 @@ public class EOSVoiceChat: IDisposable
chatUsers.Clear(); chatUsers.Clear();
SubscribeToRoomNotifications(); SubscribeToRoomNotifications();
connectArgs.onCompleted?.Invoke(true); // NOTE: this callback may come too soon, we probably don't have the list of participants populated yet...
OnChatConnected?.Invoke();
break; break;
default: default:
Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}");
connectedLobbyId = null; connectedLobbyId = null;
rtcRoomName = null; rtcRoomName = null;
Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}");
connectArgs.onCompleted?.Invoke(false);
OnChatConnectionFailed?.Invoke();
break; break;
} }
} }
@ -206,6 +214,8 @@ public class EOSVoiceChat: IDisposable
connectedLobbyId = null; connectedLobbyId = null;
rtcRoomName = null; rtcRoomName = null;
chatUsers.Clear(); chatUsers.Clear();
OnChatDisconnected?.Invoke();
} }
/// <summary> /// <summary>
@ -420,10 +430,12 @@ public class EOSVoiceChat: IDisposable
{ {
var chatUser = new ChatUser(data.ParticipantId); var chatUser = new ChatUser(data.ParticipantId);
chatUsers.Add(data.ParticipantId, chatUser); chatUsers.Add(data.ParticipantId, chatUser);
OnChatUserJoined?.Invoke(data.ParticipantId);
} }
break; break;
case RTCParticipantStatus.Left: case RTCParticipantStatus.Left:
chatUsers.Remove(data.ParticipantId); chatUsers.Remove(data.ParticipantId);
OnChatUserLeft?.Invoke(data.ParticipantId);
break; break;
} }
} }
@ -440,13 +452,11 @@ public class EOSVoiceChat: IDisposable
private class ChatConnectArgs private class ChatConnectArgs
{ {
public readonly string chatLobbyName; public readonly string chatLobbyName;
public readonly Action<bool> onCompleted;
public readonly uint maxChatPlayers; public readonly uint maxChatPlayers;
public ChatConnectArgs(string chatLobbyName, Action<bool> onCompleted, uint maxChatPlayers)
public ChatConnectArgs(string chatLobbyName, uint maxChatPlayers)
{ {
this.chatLobbyName = chatLobbyName; this.chatLobbyName = chatLobbyName;
this.onCompleted = onCompleted;
this.maxChatPlayers = maxChatPlayers; this.maxChatPlayers = maxChatPlayers;
} }
} }

14
Assets/Scripts/MagnificentVoiceChat.cs

@ -127,6 +127,11 @@ public class MagnificentVoiceChat : MonoBehaviour
}, null, HandleLoginResult); }, null, HandleLoginResult);
voiceChat = new EOSVoiceChat(lobbyInterface, rtcInterface, audioInterface, () => localProductUserId); voiceChat = new EOSVoiceChat(lobbyInterface, rtcInterface, audioInterface, () => localProductUserId);
voiceChat.OnChatConnected += () => status.AppendLine("Chat lobby successfully connected!");
voiceChat.OnChatConnectionFailed += () => status.AppendLine("Chat lobby connection failed...");
voiceChat.OnChatDisconnected += () => status.AppendLine("Chat lobby disconnected");
voiceChat.OnChatUserJoined += userId => status.AppendLine($"Chat user {userId} joined");
voiceChat.OnChatUserLeft += userId => status.AppendLine($"Chat user {userId} left");
} }
private Credentials GetEpicCredentials() // This is platform-specific private Credentials GetEpicCredentials() // This is platform-specific
@ -215,14 +220,7 @@ public class MagnificentVoiceChat : MonoBehaviour
private void CreateOrJoinVoiceLobby() private void CreateOrJoinVoiceLobby()
{ {
voiceChat.ConnectToChat(DebugLobbyId, success =>
{
Debug.Log($"Chat lobby connect result = {success}");
if (success)
status.AppendLine("Chat lobby successfully connected!");
else
status.AppendLine("Chat lobby connect failure...");
});
voiceChat.ConnectToChat(DebugLobbyId);
} }
void Update() void Update()

Loading…
Cancel
Save