Exemplo n.º 1
0
        /// <summary>
        /// Listener to react when a "WebSocket closed" event occurs.
        /// </summary>
        private void OnWebSocketClose()
        {
            DebugLogs.LogWarning("[GeeoWS:OnWebSocketClose] WebSocket closed");

            // Reset the Geeo Data instances
            connectedAgent    = null;
            connectedViewport = null;
            agents.Clear();
            pointsOfInterest.Clear();

            // Stop the connect coroutine if still running
            if (webSocketConnectCoroutine != null)
            {
                Geeo.Instance.StopCoroutine(webSocketConnectCoroutine);
                webSocketConnectCoroutine = null;
            }

            // Stop the network check (ping)
            if (networkCheckPings)
            {
                webSocket.NetworkCheckStop();
            }

            // Trigger the OnDisconnected event if any callback registered to it
            if (OnDisconnected != null)
            {
                OnDisconnected();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// If the user leaves the application and this option is enabled, close the WebSocket to avoid useless data transfers.
 /// When the user comes back, try to connect to the Geeo server again with the last used WebSocket token.
 /// </summary>
 /// <param name="paused">If the application lost the focus.</param>
 internal void OnApplicationPause(bool paused)
 {
     // Check if the "disconnect on application pause" option is enabled
     if (disconnectOnApplicationPause)
     {
         // User leaves the application (close the WebSocket connection but keep the WebSocket token to connect again later)
         if (paused && webSocket.isConnected)
         {
             DebugLogs.LogWarning("[GeeoWS:OnApplicationPause] Application paused ›› Closing connection...");
             WebSocketClose();
         }
         // User resumes the application and there is a stored WebSocket token (try to connect the WebSocket again)
         else if (!paused && !string.IsNullOrEmpty(lastWsToken))
         {
             DebugLogs.LogWarning("[GeeoWS:OnApplicationPause] Application resumed ›› Reopening connection...");
             webSocketConnectCoroutine = Geeo.Instance.StartCoroutine(WebSocketConnect(lastWsToken));
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Handle a "point of interest update" type received WebSocket message from the Geeo server to update the points of interest list.
        /// </summary>
        /// <param name="pointOfInterestData">Received message data about a point of interest.</param>
        private void WebSocketMessage_PointOfInterestUpdate(PointOfInterestJson pointOfInterestData)
        {
            PointOfInterest pointOfInterest;

            // If the point of interest just entered the viewport
            if (pointOfInterestData.entered)
            {
                // If the point of interest already exists in the points of interest list, just ignore this update
                if (pointsOfInterest.ContainsKey(pointOfInterestData.poi_id))
                {
                    DebugLogs.LogWarning("[GeeoWS:WebSocketMessage_PointOfInterestUpdate] Point of interest ‘" + pointOfInterestData.poi_id + "’ received 2 ‘entered’ updates without any ‘left’ one");

                    // Get the point of interest corresponding to its identifier from the points of interest list
                    pointOfInterest = pointsOfInterest[pointOfInterestData.poi_id];
                }
                // If the point of interest doesn't exist in the points of interest list, add this point of interest to the list
                else
                {
                    // Create a new PointOfInterest instance and fill its data from the received message
                    pointOfInterest            = new PointOfInterest();
                    pointOfInterest.id         = pointOfInterestData.poi_id;
                    pointOfInterest.latitude   = pointOfInterestData.pos[1];
                    pointOfInterest.longitude  = pointOfInterestData.pos[0];
                    pointOfInterest.publicData = pointOfInterestData.publicData;
                    pointOfInterest.creatorId  = pointOfInterestData.creator;

                    // Add the new point of interest to the points of interest list
                    pointsOfInterest.Add(pointOfInterestData.poi_id, pointOfInterest);
                }

                // Trigger the OnPointOfInterestEntered event if any callback registered to it
                if (OnPointOfInterestEntered != null)
                {
                    OnPointOfInterestEntered(pointOfInterest);
                }
            }
            // If the point of interest just left the viewport
            else if (pointOfInterestData.left)
            {
                // If the point of interest doesn't exist in the points of interest list, just ignore this update
                if (!pointsOfInterest.ContainsKey(pointOfInterestData.poi_id))
                {
                    DebugLogs.LogWarning("[GeeoWS:WebSocketMessage_PointOfInterestUpdate] Point of interest ‘" + pointOfInterestData.poi_id + "’ received a ‘left’ update after another ‘left’ one or no prior ‘entered’ one");

                    // Create a new PointOfInterest instance and fill its data from the received message
                    pointOfInterest    = new PointOfInterest();
                    pointOfInterest.id = pointOfInterestData.poi_id;
                }
                // If the point of interest does exist in the points of interest list, remove this point of interest from the list
                else
                {
                    // Get the point of interest corresponding to its identifier from the points of interest list
                    pointOfInterest = pointsOfInterest[pointOfInterestData.poi_id];

                    // Remove the point of interest from the points of interest list
                    pointsOfInterest.Remove(pointOfInterestData.poi_id);
                }

                // Trigger the OnPointOfInterestLeft event if any callback registered to it
                if (OnPointOfInterestLeft != null)
                {
                    OnPointOfInterestLeft(pointOfInterest);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handle an "agent update" type received WebSocket message from the Geeo server to update the agents list.
        /// </summary>
        /// <param name="agentData">Received message data about an agent.</param>
        private void WebSocketMessage_AgentUpdate(AgentJson agentData)
        {
            Agent agent;

            // If the agent just entered the viewport
            if (agentData.entered)
            {
                // If the agent already exists in the agents list, only update this agent's position
                if (agents.ContainsKey(agentData.agent_id))
                {
                    DebugLogs.LogWarning("[GeeoWS:WebSocketMessage_AgentUpdate] Agent ‘" + agentData.agent_id + "’ received 2 ‘entered’ updates without any ‘left’ one");

                    // Get the agent corresponding to its identifier from the agents list
                    agent = agents[agentData.agent_id];

                    // Update agent's data from the received message
                    agent.latitude  = agentData.pos[1];
                    agent.longitude = agentData.pos[0];
                }
                // If the agent doesn't exist in the agents list, add this agent to the list
                else
                {
                    // Create a new Agent instance and fill its data from the received message
                    agent            = new Agent();
                    agent.id         = agentData.agent_id;
                    agent.latitude   = agentData.pos[1];
                    agent.longitude  = agentData.pos[0];
                    agent.publicData = agentData.publicData;

                    // Add the new agent to the agents list
                    agents.Add(agentData.agent_id, agent);
                }

                // Trigger the OnAgentEntered event if any callback registered to it
                if (OnAgentEntered != null)
                {
                    OnAgentEntered(agent);
                }
            }
            // If the agent just left the viewport
            else if (agentData.left)
            {
                // If the agent doesn't exist in the agents list, just ignore this update
                if (!agents.ContainsKey(agentData.agent_id))
                {
                    DebugLogs.LogWarning("[GeeoWS:WebSocketMessage_AgentUpdate] Agent ‘" + agentData.agent_id + "’ received a ‘left’ update after another ‘left’ one or no prior ‘entered’ one");

                    // Create a new Agent instance and fill its data from the received message
                    agent    = new Agent();
                    agent.id = agentData.agent_id;
                }
                // If the agent does exist in the agents list, remove this agent from the list
                else
                {
                    // Get the agent corresponding to its identifier from the agents list
                    agent = agents[agentData.agent_id];

                    // Remove the agent from the agents list
                    agents.Remove(agentData.agent_id);
                }

                // Trigger the OnAgentLeft event if any callback registered to it
                if (OnAgentLeft != null)
                {
                    OnAgentLeft(agent);
                }
            }
            // If the agent just moved in the viewport
            else
            {
                // If the agent doesn't exist in the agents list, add this agent to the list
                if (!agents.ContainsKey(agentData.agent_id))
                {
                    DebugLogs.LogWarning("[GeeoWS:WebSocketMessage_AgentUpdate] Agent ‘" + agentData.agent_id + "’ received a ‘moved’ update without any prior ‘entered’ one");

                    // Create a new Agent instance and fill its data from the received message
                    agent           = new Agent();
                    agent.id        = agentData.agent_id;
                    agent.latitude  = agentData.pos[1];
                    agent.longitude = agentData.pos[0];

                    // Add the new agent to the agents list
                    agents.Add(agentData.agent_id, agent);
                }
                // If the agent does exist in the agents list, update this agent's position
                else
                {
                    // Get the agent corresponding to its identifier from the agents list
                    agent = agents[agentData.agent_id];

                    // Update agent's data from the received message
                    agent.latitude  = agentData.pos[1];
                    agent.longitude = agentData.pos[0];
                }

                // Trigger the OnAgentMoved event if any callback registered to it
                if (OnAgentMoved != null)
                {
                    OnAgentMoved(agent);
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Close the WebSocket when application is killed.
 /// </summary>
 internal void OnApplicationQuit()
 {
     DebugLogs.LogWarning("[GeeoWS:OnApplicationQuit] Application quit ›› Closing connection...");
     WebSocketClose();
 }