diff --git a/Assets/Scripts/EOSVoiceChat.cs b/Assets/Scripts/EOSVoiceChat.cs index baefdec..2f08284 100644 --- a/Assets/Scripts/EOSVoiceChat.cs +++ b/Assets/Scripts/EOSVoiceChat.cs @@ -33,6 +33,12 @@ public class EOSVoiceChat: IDisposable private ulong? onAudioDevicesChangedCallbackId; private ulong? onConnectionChangedCallbackId, onParticipantStatusChangedCallbackId, onParticipantUpdatedCallbackId; + public event Action OnChatConnected; + public event Action OnChatConnectionFailed; + public event Action OnChatDisconnected; + public event Action OnChatUserJoined; + public event Action OnChatUserLeft; + /// /// 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. @@ -59,11 +65,11 @@ public class EOSVoiceChat: IDisposable /// 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. /// - public void ConnectToChat(string chatLobbyName, Action onCompleted = null, uint maxChatPlayers = DefaultMaxChatPlayers) + public void ConnectToChat(string chatLobbyName, uint maxChatPlayers = DefaultMaxChatPlayers) { 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); searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName }); @@ -125,7 +131,8 @@ public class EOSVoiceChat: IDisposable chatUsers.Clear(); 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; case Result.LobbyLobbyAlreadyExists: // 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; rtcRoomName = null; Debug.Log("Chat lobby already exists, attempting to join it..."); - ConnectToChat(connectArgs.chatLobbyName, connectArgs.onCompleted, connectArgs.maxChatPlayers); + ConnectToChat(connectArgs.chatLobbyName, connectArgs.maxChatPlayers); break; default: + Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}"); connectedLobbyId = null; rtcRoomName = null; - Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}"); - connectArgs.onCompleted?.Invoke(false); + OnChatConnectionFailed?.Invoke(); break; } } @@ -156,13 +163,14 @@ public class EOSVoiceChat: IDisposable chatUsers.Clear(); 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; default: + Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}"); connectedLobbyId = null; rtcRoomName = null; - Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}"); - connectArgs.onCompleted?.Invoke(false); + OnChatConnectionFailed?.Invoke(); break; } } @@ -206,6 +214,8 @@ public class EOSVoiceChat: IDisposable connectedLobbyId = null; rtcRoomName = null; chatUsers.Clear(); + + OnChatDisconnected?.Invoke(); } /// @@ -420,10 +430,12 @@ public class EOSVoiceChat: IDisposable { var chatUser = new ChatUser(data.ParticipantId); chatUsers.Add(data.ParticipantId, chatUser); + OnChatUserJoined?.Invoke(data.ParticipantId); } break; case RTCParticipantStatus.Left: chatUsers.Remove(data.ParticipantId); + OnChatUserLeft?.Invoke(data.ParticipantId); break; } } @@ -440,13 +452,11 @@ public class EOSVoiceChat: IDisposable private class ChatConnectArgs { public readonly string chatLobbyName; - public readonly Action onCompleted; public readonly uint maxChatPlayers; - public ChatConnectArgs(string chatLobbyName, Action onCompleted, uint maxChatPlayers) + public ChatConnectArgs(string chatLobbyName, uint maxChatPlayers) { this.chatLobbyName = chatLobbyName; - this.onCompleted = onCompleted; this.maxChatPlayers = maxChatPlayers; } } diff --git a/Assets/Scripts/MagnificentVoiceChat.cs b/Assets/Scripts/MagnificentVoiceChat.cs index 6e4d240..ea8275d 100644 --- a/Assets/Scripts/MagnificentVoiceChat.cs +++ b/Assets/Scripts/MagnificentVoiceChat.cs @@ -127,6 +127,11 @@ public class MagnificentVoiceChat : MonoBehaviour }, null, HandleLoginResult); 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 @@ -215,14 +220,7 @@ public class MagnificentVoiceChat : MonoBehaviour 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()