Question by 
               natepell14 · Dec 31, 2020 at 03:12 AM · 
                networkingeventsstreamingsockets  
              
 
              How do I connect to streamlabs socket with a ClientWebSocket object?
I have been having an issue where I can't seem to connect to the streamlabs web socket for some reason. I keep getting "Unable to connect to the remote server" errors. I assume I am probably just not including some option I need or malforming something, but I have tried several things short of creating a separate node.js application to pipe the socket messages from and had no luck. I have reached out to streamlabs support, but have not yet received a response. Could it be that I need to port forward or something for it to work? Code is below ~
 public class TestDatabaseConnection : MonoBehaviour {
     DatabaseConnection connection = new DatabaseConnection();
     public TextMeshProUGUI text;
     private bool socketEnabled = false;
     private string hostURL = "https://streamlabs.com/api/v1.0/socket/token?access_token=" ;
     private SocketTokenResponse response;
  
     IEnumerator GetSocketToken(string accessToken) {
         string URL = hostURL + accessToken;
  
         using ( UnityWebRequest restAPI = UnityWebRequest.Get(URL) ) {
  
             restAPI.SetRequestHeader("content-type", "application/json");
             restAPI.SetRequestHeader("Accept", "application/json");
  
             yield return restAPI.SendWebRequest();
  
             if ( restAPI.isNetworkError || restAPI.isHttpError )
             {
                 Debug.Log(restAPI.error);
             } else {
                 Debug.Log("Form upload complete!");
                 if ( restAPI.isDone ) {
                     response = JsonUtility.FromJson<SocketTokenResponse>(System.Text.Encoding.UTF8.GetString(restAPI.downloadHandler.data));
                     Debug.Log(response.socket_token);
                     StartCoroutine(StartListening());
                 }
             }
         }
     }
  
     void Start() {
         string sql =
         @"SELECT UserId
            ,AccessToken
            ,RefreshToken
            ,Expiration
            ,TimeCreated
            ,SocketToken
            ,CurrentBoss
            ,MaxHealth
            ,Health
        FROM tblBitBoss";
  
         DataSet result = connection.DataSet(sql);
  
         //the below text output is for testing purposes
         /*text.SetText(
             result.Tables[0].Rows[0]["AccessToken"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["RefreshToken"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["Expiration"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["TimeCreated"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["SocketToken"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["CurrentBoss"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["MaxHealth"].ToString() + Environment.NewLine +
             result.Tables[0].Rows[0]["Health"].ToString()
         );*/
  
         StartCoroutine(GetSocketToken(result.Tables[0].Rows[0]["AccessToken"].ToString()));
     }
  
     private IEnumerator StartListening() {
         Debug.Log("Beginning coroutine.");
         ClientWebSocket socket;
         Task task;
         Uri uri;
      
         try {
             socket = new ClientWebSocket();
             task = socket.ConnectAsync(new Uri("wss://sockets.streamlabs.com?token=\"" + response.socket_token + "\""), CancellationToken.None);
         }
         catch ( Exception ) {
             throw;
         }
  
         socketEnabled = true;
  
         while (socketEnabled) {
             if ( socket.State == WebSocketState.Open ) {
                 Debug.Log("Socket open.");
             } else if ( socket.State == WebSocketState.Connecting ) {
                 Debug.Log("Socket still connecting.");
             } else if ( socket.State == WebSocketState.Aborted ) {
                 Debug.LogError("Socket connection aborted.");
             } else if ( task.Exception != null ) {
                 Debug.LogError(task.Exception.Message + Environment.NewLine + task.Exception.InnerException.Message);
             }
             yield return new WaitForSeconds(.5f);
         }
  
         socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure.", CancellationToken.None);
     }
  
     [Serializable]
     public class SocketTokenResponse
     {
         public string socket_token;
  
         public SocketTokenResponse() {
         }
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                