Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Error when executing GetConnectToken()

Discussion in 'Vivox (Voice & Text Chat)' started by unitsume, Nov 7, 2022.

  1. unitsume

    unitsume

    Joined:
    Oct 7, 2022
    Posts:
    31
    Hello.

    I made a simple program for voice conversation.
    An error occurred when executing GetConnectToken().
    I'm stuck because I can't clear it.
    What am I doing wrong?
    Thank you.

    Error message
    tester.cs
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Unity.Services.Authentication;
    6. using Unity.Services.Core;
    7. using Unity.Services.Vivox;
    8. using VivoxUnity;
    9.  
    10. public class tester : MonoBehaviour
    11. {
    12.     private Client _client = null;
    13.  
    14.     private ILoginSession _loginSession = null;
    15.  
    16.     private AccountId _accountId = null;
    17.  
    18.     [SerializeField]
    19.     private string _domain = "mt1s.vivox.com";
    20.  
    21.     [SerializeField]
    22.     private string _issuer = "**********-****-***";
    23.  
    24.     [SerializeField]
    25.     private string _server = "https://mt1s.www.vivox.com/api2";
    26.  
    27.     [SerializeField]
    28.     private string _tokenKey = "*******";
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.  
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (Input.GetKey("right shift"))
    40.         {
    41.             JoinChannel("positional-channel-test", ChannelType.Positional);
    42.         }
    43.     }
    44.  
    45.     private void Awake()
    46.     {
    47.         _client = new Client();
    48.         _client.Initialize();
    49.         CreateAccount("a1", "Avatar1");
    50.         Login();
    51.     }
    52.  
    53.     private void OnApplicationQuit()
    54.     {
    55.         if (_client != null)
    56.         {
    57.             _client.Uninitialize();
    58.             _client = null;
    59.         }
    60.     }
    61.  
    62.     public void CreateAccount(string uniqueId, string displayName)
    63.     {
    64.         _accountId = new AccountId(_issuer, uniqueId, _domain, displayName);
    65.     }
    66.  
    67.  
    68.     public void Login()
    69.     {
    70.         _loginSession = _client.GetLoginSession(_accountId);
    71.  
    72.         Uri serverUri = new Uri(_server);
    73.         string token = _loginSession.GetLoginToken(_tokenKey, TimeSpan.FromSeconds(90d));
    74.         _loginSession.BeginLogin(serverUri, token, asyncResult =>
    75.         {
    76.             try
    77.             {
    78.                 _loginSession.EndLogin(asyncResult);
    79.             }
    80.             catch (Exception e)
    81.             {
    82.                 Debug.Log("Login failed");
    83.                 return;
    84.             }
    85.  
    86.             Debug.LogWarning("Successfully logged");
    87.         });
    88.     }
    89.  
    90.     public void Logout()
    91.     {
    92.         if (_loginSession == null || _loginSession.State == LoginState.LoggingOut || _loginSession.State == LoginState.LoggedOut)
    93.         {
    94.             Debug.LogWarning("Logout failed");
    95.             return;
    96.         }
    97.  
    98.         _loginSession.Logout();
    99.     }
    100.  
    101.     public void JoinChannel(string channelName, ChannelType channelType)
    102.     {
    103.         if (_loginSession.State == LoginState.LoggedIn)
    104.         {
    105.             ChannelId channelId = new ChannelId(_issuer, channelName, _domain, channelType);
    106.             IChannelSession channelSession = _loginSession.GetChannelSession(channelId);
    107.  
    108.             string token = channelSession.GetConnectToken(_tokenKey, TimeSpan.FromSeconds(90d));
    109.  
    110.             Debug.Log("channelSession: " + channelSession);
    111.             Debug.Log("token: " + token);
    112.  
    113.             channelSession.BeginConnect(true, false, true, token, asyncResult =>
    114.             {
    115.                 try
    116.                 {
    117.                     channelSession.EndConnect(asyncResult);
    118.                 }
    119.                 catch (Exception e)
    120.                 {
    121.                     channelSession.Parent.DeleteChannelSession(channelSession.Channel);
    122.                     Debug.Log("Join channel failed");
    123.                     return;
    124.                 }
    125.                 Debug.Log("Successfully Join channel");
    126.             });
    127.         }
    128.         else
    129.         {
    130.             Debug.LogWarning("You cannot join the channel because you are not logged in");
    131.         }
    132.     }
    133.  
    134. }
     
  2. MurphyMurph_21

    MurphyMurph_21

    Joined:
    Jul 3, 2020
    Posts:
    73
    Hey @unitsume ,

    I would add an Unitialize before you initialize in the awake method

    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         var _client = new Client();
    4.         _client.Uninitialize();
    5.         _client.Initialize();
    6.         CreateAccount("a1", "Avatar1");
    7.         Login();
    8.     }
    I also wouldn't call JoinChannel in the Update() because even tho you may press right shift button fast and only once, update is called every frame so Join Channel is probably getting called at least 2-3 times probably more. I just tried joining the same channel twice and I get the same error.

    Just create a wrapper around your JoinChannel method and call it from a button in the UI
    Code (CSharp):
    1.  
    2.     public void JoinChannel()
    3.     {
    4.         JoinChannel("positional-channel-test", ChannelType.Positional);
    5.     }
    if you dont want to use the UI to join channel then subcribe to Login Events and then join channel after user is logged in
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         _loginSession.PropertyChanged += OnLoginPropertyChanged;
    4.     }
    5.     private void OnApplicationQuit()
    6.     {
    7.         _loginSession.PropertyChanged -= OnLoginPropertyChanged;
    8.     }
    9.  
    10.     public async void OnLoginPropertyChanged(object sender, PropertyChangedEventArgs propArgs)
    11.     {
    12.         var senderLoginSession = (ILoginSession)sender;
    13.  
    14.         if (propArgs.PropertyName == "State")
    15.         {
    16.             switch (senderLoginSession.State)
    17.             {
    18.                 case LoginState.LoggingIn:
    19.                     Debug.Log(senderLoginSession.State);
    20.                     break;
    21.                 case LoginState.LoggedIn:
    22.                     Debug.Log(senderLoginSession.State);
    23.                     JoinChannel("positional-channel-test", ChannelType.Positional);
    24.                     break;
    25.                 case LoginState.LoggingOut:
    26.                     Debug.Log(senderLoginSession.State);
    27.                     break;
    28.                 case LoginState.LoggedOut:
    29.                     Debug.Log(senderLoginSession.State);
    30.                     break;
    31.  
    32.                 default:
    33.                     Debug.Log($"Event Error - Logging In/Out events failed");
    34.                     break;
    35.             }
    36.         }
    37.     }
     
  3. unitsume

    unitsume

    Joined:
    Oct 7, 2022
    Posts:
    31
    Sorry for my late reply. I was sick.

    Thank you very much. It solved.

    It was helpful. I hope you don't get sick too.
     
    MurphyMurph_21 likes this.