Commit 6c4cd520 authored by nk161690's avatar nk161690

log

parent 136fc4fa
...@@ -7,12 +7,14 @@ using Photon.Realtime; ...@@ -7,12 +7,14 @@ using Photon.Realtime;
using UnityEditor.VersionControl; using UnityEditor.VersionControl;
using TMPro; using TMPro;
using Unity.VisualScripting.Antlr3.Runtime; using Unity.VisualScripting.Antlr3.Runtime;
using static Log;
using System;
public class FacebookLogin : MonoBehaviour public class FacebookLogin : MonoBehaviour
{ {
[SerializeField] private TextMeshProUGUI message;
private readonly string[] facebookPermissions = { "public_profile", "email" }; private readonly string[] facebookPermissions = { "public_profile", "email" };
private LoginSession session = new LoginSession();
private Log log = new Log();
private void Awake() private void Awake()
{ {
...@@ -27,6 +29,12 @@ public class FacebookLogin : MonoBehaviour ...@@ -27,6 +29,12 @@ public class FacebookLogin : MonoBehaviour
} }
} }
private void OnApplicationQuit()
{
session.LogoutTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
log.LogLoginSession(session);
}
private void OnInitComplete() private void OnInitComplete()
{ {
if (FB.IsInitialized) if (FB.IsInitialized)
...@@ -66,6 +74,10 @@ public class FacebookLogin : MonoBehaviour ...@@ -66,6 +74,10 @@ public class FacebookLogin : MonoBehaviour
// Use the accessToken to authenticate with Photon as a custom authentication type // Use the accessToken to authenticate with Photon as a custom authentication type
OnFacebookLoggedIn(); OnFacebookLoggedIn();
// Log
session.Username = AccessToken.CurrentAccessToken.UserId;
session.LoginTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
} }
else else
{ {
......
using Newtonsoft.Json;
using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using UnityEngine;
using static System.Collections.Specialized.BitVector32;
public class Log
{
public class LoginSession
{
public string Username { get; set; }
public string LoginTime { get; set; }
public string LogoutTime { get; set; }
}
public async void LogLoginSession(LoginSession session)
{
// 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);
}
}
}
}
fileFormatVersion: 2
guid: 132acc5db1794b342ab34765ca9c17d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -4,9 +4,8 @@ using UnityEngine; ...@@ -4,9 +4,8 @@ using UnityEngine;
using Newtonsoft.Json; using Newtonsoft.Json;
using Photon.Realtime; using Photon.Realtime;
using Photon.Pun; using Photon.Pun;
using System.Text;
using System; using System;
using System.Net.Http.Headers; using static Log;
public class Login : MonoBehaviourPunCallbacks public class Login : MonoBehaviourPunCallbacks
{ {
...@@ -16,7 +15,8 @@ public class Login : MonoBehaviourPunCallbacks ...@@ -16,7 +15,8 @@ public class Login : MonoBehaviourPunCallbacks
private readonly string baseURL = "http://localhost:5172/User/"; private readonly string baseURL = "http://localhost:5172/User/";
private string token = ""; private string token = "";
public LoginSession session = new LoginSession(); private LoginSession session = new LoginSession();
private Log log = new Log();
public class RespondMessage public class RespondMessage
{ {
...@@ -25,19 +25,6 @@ public class Login : MonoBehaviourPunCallbacks ...@@ -25,19 +25,6 @@ public class Login : MonoBehaviourPunCallbacks
public string Data { get; set; } public string Data { get; set; }
} }
public class LoginSession
{
public string Username { get; set; }
public DateTime LoginTime { get; set; }
public DateTime? LogoutTime { get; set; }
}
private void OnApplicationQuit()
{
session.LogoutTime = DateTime.Now;
LogLoginSession(token);
}
public async void CallLoginAPI() public async void CallLoginAPI()
{ {
string endpoint = "Login"; string endpoint = "Login";
...@@ -63,7 +50,7 @@ public class Login : MonoBehaviourPunCallbacks ...@@ -63,7 +50,7 @@ public class Login : MonoBehaviourPunCallbacks
token = respondMessage.Data; token = respondMessage.Data;
AuthenticateWithPhoton(token); AuthenticateWithPhoton(token);
session.Username = username.text; session.Username = username.text;
session.LoginTime = DateTime.Now; session.LoginTime = DateTime.Now.ToString("dd/MM/yyyy");
} }
else else
{ {
...@@ -107,45 +94,5 @@ public class Login : MonoBehaviourPunCallbacks ...@@ -107,45 +94,5 @@ public class Login : MonoBehaviourPunCallbacks
} }
} }
} }
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Connected to Photon!");
message.text = "Login successful! Connected to Photon.";
}
public override void OnDisconnected(DisconnectCause cause)
{
base.OnDisconnected(cause);
Debug.Log("Disconnected from Photon. Reason: " + cause.ToString());
}
private async void LogLoginSession(string token)
{
// 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);
}
}
}
} }
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine;
public class PhotonServer : MonoBehaviourPunCallbacks
{
[SerializeField] private TextMeshProUGUI message;
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Connected to Photon!");
message.text = "Login successful! Connected to Photon.";
}
public override void OnDisconnected(DisconnectCause cause)
{
base.OnDisconnected(cause);
Debug.Log("Disconnected from Photon. Reason: " + cause.ToString());
}
}
fileFormatVersion: 2
guid: abbec8d2166306344b83dc07dadc742e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -858,7 +858,6 @@ MonoBehaviour: ...@@ -858,7 +858,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ab1db3a1da2c2b541bc729810838e50f, type: 3} m_Script: {fileID: 11500000, guid: ab1db3a1da2c2b541bc729810838e50f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
message: {fileID: 849773379}
--- !u!1 &366359934 --- !u!1 &366359934
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -1140,6 +1139,7 @@ GameObject: ...@@ -1140,6 +1139,7 @@ GameObject:
- component: {fileID: 519420032} - component: {fileID: 519420032}
- component: {fileID: 519420031} - component: {fileID: 519420031}
- component: {fileID: 519420029} - component: {fileID: 519420029}
- component: {fileID: 519420033}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
...@@ -1213,6 +1213,19 @@ Transform: ...@@ -1213,6 +1213,19 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &519420033
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: abbec8d2166306344b83dc07dadc742e, type: 3}
m_Name:
m_EditorClassIdentifier:
message: {fileID: 849773379}
--- !u!1 &569175305 --- !u!1 &569175305
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
......
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