/// <summary> /// Establishes socket connected with the server /// </summary> /// <returns>Whether the socket can attempt to connect. Usually returns true when the service has not been authenticated</returns> public bool Connect() { if (Status == BingStatus.Idle || !Auth.IsValid()) { TryLogError("Could not connect. Not authorized"); return(false); } m_Socket.CustomHeaders = new Dictionary <string, string> { { "X-ConnectionId", BingUtils.GetNewRequestID() }, { "Authorization", "Bearer " + Auth.Token } }; TryLog("Connecting..."); Status = BingStatus.Connecting; m_Socket.ConnectAsync( () => { TryLog("Connected"); Status = BingStatus.Connected; }, exception => { TryLogError("Could not connect: " + exception); Status = BingStatus.Authenticated; } ); return(true); }
/// <summary> /// Request authentication so that the client can communicate with the server /// </summary> /// <param name="key">The Bing Speech to Text API key</param> /// <param name="callback">Bool callback for whether the authentication was successful</param> public void Authenticate(string key, Action <string> onSuccess, Action <Exception> onFailure) { TryLog("Authenticating"); Auth = new BingAuthorization(key); Status = BingStatus.Authenticating; Auth.FetchToken( token => { TryLog("Authorized: " + token); Status = BingStatus.Authenticated; m_RequestId = BingUtils.GetNewRequestID(); m_Header = BingUtils.GetHeader(m_RequestId); onSuccess.TryInvoke(token); }, exception => { TryLogError(exception); Status = BingStatus.Idle; onFailure.TryInvoke(exception); } ); }