Esempio n. 1
0
        /// <summary>
        /// Sends a heartbeat to the WakaTime API.
        /// </summary>
        /// <param name="fromSave">Was this triggered from a save?</param>
        static void PostHeartbeat(bool fromSave = false)
        {
            if (!ApiKeyValidated)
            {
                return;
            }

            // Create our heartbeat
            // If the current scene is empty it's an unsaved scene; so don't
            // try to determine exact file position in that instance.
            var currentScene = EditorSceneManager.GetActiveScene().path;
            var heartbeat    = new HeartbeatSchema(
                currentScene != string.Empty ? Path.Combine(
                    Application.dataPath,
                    currentScene.Substring("Assets/".Length)
                    ) : string.Empty,
                fromSave
                );

            // If it hasn't been longer than the last heartbeat buffer, ignore if
            // the heartbeat isn't triggered by a save or the scene changing.
            if ((heartbeat.time - s_LastHeartbeat.time < HeartbeatBuffer) && !fromSave &&
                (heartbeat.entity == s_LastHeartbeat.entity))
            {
                return;
            }

            var heartbeatJson = JsonUtility.ToJson(heartbeat);
            var www           = UnityWebRequest.Post(
                FormatApiUrl("users/current/heartbeats"),
                string.Empty
                );

            // Manually add an upload handler so the data isn't corrupted
            www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(heartbeatJson));
            // Set the content type to json since it defaults to www form data
            www.SetRequestHeader("Content-Type", "application/json");
            // Send the request
            AsyncHelper.Add(new RequestEnumerator(www.Send(), () =>
            {
                var result = JsonUtility.FromJson <ResponseSchema <HeartbeatResponseSchema> >(www.downloadHandler.text);

                if (result.error != null)
                {
                    UnityEngine.Debug.LogError(
                        "<WakaTime> Failed to send heartbeat to WakaTime. If this " +
                        "continues, please disable the plugin and submit an issue " +
                        "on Github.\n" + result.error
                        );
                }
                else
                {
                    // UnityEngine.Debug.Log("Sent heartbeat to WakaTime");
                    s_LastHeartbeat = result.data;
                }
            }));
        }
Esempio n. 2
0
        /// <summary>
        /// Sends a heartbeat to the Wakapi API.
        /// </summary>
        /// <param name="fromSave">Was this triggered from a save?</param>
        static void PostHeartbeat(bool fromSave = false)
        {
            // Create our heartbeat
            // If the current scene is empty it's an unsaved scene; so don't
            // try to determine exact file position in that instance.
            var currentScene = EditorSceneManager.GetActiveScene().path;
            var heartbeat    = new HeartbeatSchema(
                currentScene != string.Empty
                    ? Path.Combine(
                    Application.dataPath,
                    currentScene.Substring("Assets/".Length)
                    )
                    : string.Empty,
                fromSave
                );

            // If it hasn't been longer than the last heartbeat buffer, ignore if
            // the heartbeat isn't triggered by a save or the scene changing.
            if ((heartbeat.time - s_LastHeartbeat.time < HeartbeatBuffer) && !fromSave &&
                (heartbeat.entity == s_LastHeartbeat.entity))
            {
                return;
            }

            var heartbeatJson = $"[{JsonUtility.ToJson(heartbeat)}]";
            var www           = UnityWebRequest.Post(HeartbeatURL, string.Empty);

            // Manually add an upload handler so the data isn't corrupted
            www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(heartbeatJson));
            // Set the content type to json since it defaults to www form data
            www.SetRequestHeader("Content-Type", "application/json");
            www.SetRequestHeader("Authorization", $"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ApiKey))}");
            www.SetRequestHeader("X-Machine-Name", heartbeat.machine);
            www.SetRequestHeader("User-Agent",
                                 $"wakatime/1.0.0 ({heartbeat.operating_system}-idk) {heartbeat.editor}/1.0.0 {heartbeat.editor}-wakatime/1.0.0");

            // Send the request
            AsyncHelper.Add(new RequestEnumerator(www.Send(), () => {
                var result = JsonUtility.FromJson <ResponseSchema <HeartbeatResponseSchema> >(www.downloadHandler.text);

                if (result.error != null)
                {
                    UnityEngine.Debug.LogError(
                        "<Wakapi> Failed to send heartbeat to Wakapi. If this " +
                        "continues there is something wrong with your API key, URL or server is offline.\n" + result.error
                        );
                }
                else
                {
                    // UnityEngine.Debug.Log("Sent heartbeat to Wakapi");
                    s_LastHeartbeat = result.data;
                }
                www.Dispose();
            }));
        }