浏览代码

Cleaning out Vivox TODOs. The main thing is shifting the Vivox login and join to do some extra error handling. Waiting to hear on one more issue regarding IChannelSession.Disconnect being called before EndConnect completes, which might be allowing players to stay in the voice channel after leaving a lobby if they mash through the buttons quickly.

/main/staging/vivox_polish
nathaniel.buck@unity3d.com 3 年前
当前提交
fc7d77ff
共有 3 个文件被更改,包括 69 次插入38 次删除
  1. 63
      Assets/Scripts/Game/GameManager.cs
  2. 9
      Assets/Scripts/UI/InLobbyUserUI.cs
  3. 35
      Assets/Scripts/Vivox/VivoxSetup.cs

63
Assets/Scripts/Game/GameManager.cs


using LobbyRelaySample.relay;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Debug.Log("Signed in.");
m_localUser.ID = Locator.Get.Identity.GetSubIdentity(Auth.IIdentityType.Auth).GetContent("id");
m_localUser.DisplayName = NameGenerator.GetName(m_localUser.ID);
m_vivoxSetup.Initialize(m_vivoxUserHandlers); // Should be before AddPlayer, since that will attempt to set the Vivox ID based on the Auth ID.
StartVivoxLogin();
}
private void BeginObservers()

m_lobbyContentHeartbeat.BeginTracking(m_localLobby, m_localUser);
SetUserLobbyState();
StartRelayConnection();
m_vivoxSetup.JoinLobbyChannel(m_localLobby.LobbyID, null); // TODO: Retry on failure?
StartVivoxJoin();
}
private void OnLeftLobby()

SetGameState(GameState.JoinMenu);
}
private void StartVivoxLogin()
{
m_vivoxSetup.Initialize(m_vivoxUserHandlers, OnVivoxLoginComplete);
void OnVivoxLoginComplete(bool didSucceed)
{
if (!didSucceed)
{ Debug.LogError("Vivox login failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartVivoxLogin, m_localLobby.LobbyID));
return;
}
}
}
private void StartVivoxJoin()
{
m_vivoxSetup.JoinLobbyChannel(m_localLobby.LobbyID, OnVivoxJoinComplete);
void OnVivoxJoinComplete(bool didSucceed)
{
if (!didSucceed)
{ Debug.LogError("Vivox connection failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartVivoxJoin, m_localLobby.LobbyID));
return;
}
}
}
private void StartRelayConnection()
{
if (m_localUser.IsHost)

OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Connecting);
m_relaySetup.BeginRelayJoin(m_localLobby, m_localUser, OnRelayConnected);
}
private void OnRelayConnected(bool didSucceed, RelayUtpClient client)
{
Component.Destroy(m_relaySetup);
m_relaySetup = null;
void OnRelayConnected(bool didSucceed, RelayUtpClient client)
{
Component.Destroy(m_relaySetup);
m_relaySetup = null;
if (!didSucceed)
{ Debug.LogError("Relay connection failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartRelayConnection, m_localLobby.LobbyID));
return;
}
if (!didSucceed)
{
Debug.LogError("Relay connection failed! Retrying in 5s...");
StartCoroutine(RetryRelayConnection());
return;
m_relayClient = client;
OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Lobby);
m_relayClient = client;
OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Lobby);
private IEnumerator RetryRelayConnection()
private IEnumerator RetryConnection(Action doConnection, string lobbyId)
StartRelayConnection();
if (m_localLobby != null && m_localLobby.LobbyID == lobbyId && !string.IsNullOrEmpty(lobbyId)) // Ensure we didn't leave the lobby during this waiting period.
doConnection?.Invoke();
}
private void BeginCountDown()

9
Assets/Scripts/UI/InLobbyUserUI.cs


m_StatusText.SetText(SetStatusFancy(observed.UserStatus));
m_EmoteText.SetText(observed.Emote.GetString());
m_HostIcon.enabled = observed.IsHost;
SetAudioState(true);
}
/// <summary>
/// Disable or show the Volume Icons in sync with the package.
/// </summary>
void SetAudioState(bool hasVoice)
{
// TODO: Disable if vivox is not available, per-user.
}
string SetStatusFancy(UserStatus status)

35
Assets/Scripts/Vivox/VivoxSetup.cs


/// <summary>
/// Initialize the Vivox service, before actually joining any audio channels.
/// </summary>
public void Initialize(List<VivoxUserHandler> userHandlers)
/// <param name="onComplete">Called whether the login succeeds or not.</param>
public void Initialize(List<VivoxUserHandler> userHandlers, Action<bool> onComplete)
{
if (m_isMidInitialize)
return;

{
m_loginSession.EndLogin(result);
m_hasInitialized = true;
onComplete?.Invoke(true);
}
catch (Exception ex)
{ UnityEngine.Debug.LogWarning("Vivox failed to login: " + ex.Message);
onComplete?.Invoke(false);
}
finally
{

m_channelSession.BeginConnect(true, false, true, token, result =>
{
m_channelSession.EndConnect(result); // TODO: Error handling?
onComplete?.Invoke(true);
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelJoined(m_channelSession);
try
{ m_channelSession.EndConnect(result);
onComplete?.Invoke(true);
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelJoined(m_channelSession);
}
catch (Exception ex)
{ UnityEngine.Debug.LogWarning("Vivox failed to connect: " + ex.Message);
onComplete?.Invoke(false);
}
// TODO: Reset slider and mute UI on lobby enter
ChannelId id = m_channelSession.Channel;
m_channelSession?.Disconnect(
(result) => { m_loginSession.DeleteChannelSession(id); }); // TODO: What about if this is called while also trying to connect?
if (m_channelSession != null)
{
ChannelId id = m_channelSession.Channel;
m_channelSession?.Disconnect(
(result) => { m_loginSession.DeleteChannelSession(id); m_channelSession = null; }); // TODO: What about if this is called while also trying to connect?
}
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelLeft();
}

/// </summary>
public void Uninitialize()
{
// TODO: Also call LeaveLobbyChannel?
if (!m_hasInitialized)
return;
m_loginSession.Logout();

正在加载...
取消
保存