/// <summary> /// GetGuestToken coroutine. (do not block the current thread while waiting for the request response) /// </summary> /// <param name="agentId">The ID to use for the agent.</param> /// <param name="viewportId">The ID to use for the viewport.</param> /// <param name="OnSuccess">The callback in case of request success.</param> /// <param name="OnError">The callback in case of request error.</param> private IEnumerator GetGuestToken_Coroutine(string agentId, string viewportId, Action <string> OnSuccess, Action <string> OnError) { // Build the "get guest token" request URL string requestUrl = string.Format(getGuestToken_requestUrlFormat, httpUrl, agentId, viewportId); DebugLogs.LogVerbose("[GeeoHTTP:GetGuestToken] Request URL (GET): " + requestUrl); // Send the request with the GET method using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(requestUrl)) { yield return(unityWebRequest.Send()); // If the request failed, call the error callback if (unityWebRequest.isError) { GetGuestToken_OnError(unityWebRequest.error, OnError); } else { // If the request failed, call the error callback if (unityWebRequest.responseCode != 200L) { GetGuestToken_OnError(unityWebRequest.downloadHandler.text, OnError); } // If the request succeeded, call the success callback else { DebugLogs.LogVerbose("[GeeoHTTP:GetGuestToken] Request Success: " + unityWebRequest.downloadHandler.text); OnSuccess(unityWebRequest.downloadHandler.text); } } } }
/// <summary> /// Listener to react when a "WebSocket opened" event occurs. /// </summary> private void OnWebSocketOpen() { DebugLogs.LogVerbose("[GeeoWS:OnWebSocketOpen] WebSocket opened"); // Create new Geeo Data instances connectedAgent = new Agent(); connectedViewport = new Viewport(); // Trigger the OnConnected event if any callback registered to it if (OnConnected != null) { OnConnected(); } }
/// <summary> /// Use a JWT WebSocket token previously provided by the Geeo server to open a WebSocket connection. /// If development routes are allowed, you may use the GeeoHTTP.GetGuestToken() method to get a token. /// Once the connection is opened, the OnConnected event will be triggered (so you should register a callback to it). /// </summary> /// <param name="wsToken">The JWT WebSocket token previously provided by the Geeo server.</param> public void Connect(string wsToken) { DebugLogs.LogVerbose("[GeeoWS:Connect] Connecting..."); // Keep the token used to connect the WebSocket lastWsToken = wsToken; // Register listeners for future WebSocket events webSocket.OnOpen += OnWebSocketOpen; webSocket.OnClose += OnWebSocketClose; webSocket.OnError += OnWebSocketError; webSocket.OnMessage += OnWebSocketMessage; // Create a WebSocket and connect to the Geeo server webSocketConnectCoroutine = Geeo.Instance.StartCoroutine(WebSocketConnect(wsToken)); }
/// <summary> /// Connect the WebSocket with a token previously provided by the Geeo server. /// </summary> /// <param name="wsToken">The WebSocket token provided by the Geeo server.</param> private IEnumerator WebSocketConnect(string wsToken) { // Build the "websocket connect" request URL string requestUrl = string.Format(webSocketConnect_requestUrlFormat, wsUrl, wsToken); DebugLogs.LogVerbose("[GeeoWS:WebSocketConnect] Request URL: " + requestUrl); // Wait for the connection to be established yield return(webSocket.Connect(requestUrl)); // Start the network check (ping) if (networkCheckPings) { webSocket.NetworkCheckStart(); } webSocketConnectCoroutine = null; }
/// <summary> /// Remove the last used WebSocket token and registered listeners, then close the current WebSocket connection. /// Once the connection is closed, the OnDisconnected event will be triggered (so you should register a callback to it). /// </summary> public void Disconnect() { DebugLogs.LogVerbose("[GeeoWS:Disconnect] Disconnecting..."); // Unvalidate the last used WebSocket token lastWsToken = null; // Unregister listeners for future WebSocket events webSocket.OnOpen -= OnWebSocketOpen; webSocket.OnError -= OnWebSocketError; webSocket.OnMessage -= OnWebSocketMessage; // Disconnect from the Geeo server WebSocketClose(); // Unregister the OnClose listener only after the close call to get this event triggered webSocket.OnClose -= OnWebSocketClose; }
/// <summary> /// Listener to react when a "WebSocket message" event occurs. /// </summary> /// <param name="message">The received message.</param> private void OnWebSocketMessage(string message) { DebugLogs.LogVerbose("[GeeoWS:OnWebSocketMessage] WebSocket message ›› " + message); // Parse Json data from the received WebSocket message from the Geeo server JsonData messageJson = JsonMapper.ToObject(message); // Check if messageJson is an "error" type object if (messageJson.IsObject) { WebSocketMessage_ErrorReport(JsonMapper.ToObject <ErrorJson>(message)); } // Check if messageJson is an "view update" type array else if (messageJson.IsArray) { // Identify each "update" of the array from the "view update" message foreach (JsonData messageUpdate in messageJson) { // Update type: agent data if (messageUpdate.Keys.Contains("agent_id")) { WebSocketMessage_AgentUpdate(JsonMapper.ToObject <AgentJson>(messageUpdate.ToJson())); } // Update type: point of interest data else if (messageUpdate.Keys.Contains("poi_id")) { WebSocketMessage_PointOfInterestUpdate(JsonMapper.ToObject <PointOfInterestJson>(messageUpdate.ToJson())); } } // Trigger the OnViewUpdated event if any callback registered to it if (OnViewUpdated != null) { OnViewUpdated(); } } }