Commit 99e18c1e authored by nk161690's avatar nk161690

elastic

parent 06080c39
......@@ -2,14 +2,11 @@ using System.Net.Http;
using TMPro;
using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;
using Photon.Realtime;
using Photon.Pun;
using UnityEngine.Networking;
using ExitGames.Client.Photon;
using System.Text;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEditor.PackageManager;
using System;
using System.Net.Http.Headers;
public class Login : MonoBehaviourPunCallbacks
{
......@@ -19,7 +16,6 @@ public class Login : MonoBehaviourPunCallbacks
private readonly string baseURL = "http://localhost:5172/User/";
[System.Serializable]
public class RespondMessage
{
public bool Success { get; set; }
......@@ -27,6 +23,13 @@ public class Login : MonoBehaviourPunCallbacks
public string Data { get; set; }
}
public class LoginSession
{
public string Username { get; set; }
public DateTime LoginTime { get; set; }
public DateTime? LogoutTime { get; set; }
}
public async void CallLoginAPI()
{
string endpoint = "Login";
......@@ -50,6 +53,7 @@ public class Login : MonoBehaviourPunCallbacks
//ThjMGQtYjU4ZTAyN2UyMmE4IiwibmJmIjoxNjkwNDUzMTc3LCJleHAiOjE2OTA0NTQ5NzcsImlhdCI6MTY5MDQ1MzE3N30
//.W-0PXJ9GAmwOOXlKi8cEr3lBkwx9rGbpLKCCUYJkyCA");
AuthenticateWithPhoton(respondMessage.Data);
LogLoginSession(respondMessage.Data);
}
else
{
......@@ -106,4 +110,40 @@ public class Login : MonoBehaviourPunCallbacks
base.OnDisconnected(cause);
Debug.Log("Disconnected from Photon. Reason: " + cause.ToString());
}
private async void LogLoginSession(string token)
{
// Prepare the login session data
LoginSession session = new LoginSession
{
Username = CleanInput(username.text.Trim()),
LoginTime = DateTime.UtcNow,
LogoutTime = null
};
// Serialize the data as JSON
string jsonData = JsonConvert.SerializeObject(session);
// Send the data to Elasticsearch using HTTP POST request
string elasticUrl = "https://893485fbcebc4a4d9102091b2bad74a4.us-central1.gcp.cloud.es.io:443/login_sessions/_doc" +
"?pipeline=ent-search-generic-ingestion";
using (HttpClient client = new HttpClient())
{
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ApiKey", "MUdvM3NJa0I3bFF0X01wcFZWY2s6MzU4WllreVJRSldfZEhWSTVPbHZ1UQ==");
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(elasticUrl, content);
if (response.IsSuccessStatusCode)
{
Debug.Log("Login session logged successfully!");
}
else
{
Debug.LogError("Failed to log login session to Elasticsearch. Error: " + response.StatusCode);
}
}
}
}
......@@ -28,7 +28,7 @@ MonoBehaviour:
AuthMode: 0
EnableLobbyStatistics: 0
NetworkLogging: 1
DevRegion:
DevRegion: asia
PunLogging: 0
EnableSupportLogger: 0
RunInBackground: 1
......@@ -41,4 +41,4 @@ MonoBehaviour:
- RespawnSpaceship
DisableAutoOpenWizard: 1
ShowSettings: 0
DevRegionSetOnce: 0
DevRegionSetOnce: 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment