归档!
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
using System.Collections;
|
||||
using Google.Protobuf;
|
||||
using NativeWebSocket;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
[System.Serializable]
|
||||
public class LoginResponse
|
||||
{
|
||||
public int code;
|
||||
public string msg;
|
||||
public Data data;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Data
|
||||
{
|
||||
public long usn;
|
||||
public string name;
|
||||
public string accessToken;
|
||||
public string refreshToken;
|
||||
}
|
||||
|
||||
public class SocketManager : MonoBehaviour
|
||||
{
|
||||
@@ -21,17 +40,12 @@ public class SocketManager : MonoBehaviour
|
||||
|
||||
public async void Connect()
|
||||
{
|
||||
// _ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
||||
_ws = new WebSocket($"ws://127.0.0.1:8501/?token={Random.Range(1, 1000)}");
|
||||
var token = await GetTokenAsync();
|
||||
|
||||
_ws.OnOpen += () =>
|
||||
{
|
||||
Debug.Log("Connection open!");
|
||||
SendMessage(MessageID.EnterInstance, new C2S_EnterInstance
|
||||
{
|
||||
InstanceID = 1
|
||||
});
|
||||
};
|
||||
// _ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
||||
_ws = new WebSocket($"ws://127.0.0.1:8501/?token={token}");
|
||||
|
||||
_ws.OnOpen += () => { Debug.Log("Connection open!"); };
|
||||
|
||||
_ws.OnError += (e) => { Debug.Log("Error! " + e); };
|
||||
|
||||
@@ -47,6 +61,28 @@ public class SocketManager : MonoBehaviour
|
||||
await _ws.Connect();
|
||||
}
|
||||
|
||||
private async System.Threading.Tasks.Task<string> GetTokenAsync()
|
||||
{
|
||||
string token = "";
|
||||
var tcs = new System.Threading.Tasks.TaskCompletionSource<string>();
|
||||
|
||||
StartCoroutine(HttpPost("http://127.0.0.1:8503/gw/login", "{\"phone\": \"1234\", \"code\": \"1234\"}", (resp) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = JsonUtility.FromJson<LoginResponse>(resp);
|
||||
token = r.data.accessToken;
|
||||
tcs.SetResult(token);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
tcs.SetException(e);
|
||||
}
|
||||
}));
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
|
||||
public void SendMessage(MessageID id, IMessage msg)
|
||||
{
|
||||
var m = new Message
|
||||
@@ -57,6 +93,28 @@ public class SocketManager : MonoBehaviour
|
||||
_ws.Send(m.ToByteArray());
|
||||
}
|
||||
|
||||
public IEnumerator HttpPost(string url, string jsonData, System.Action<string> callback)
|
||||
{
|
||||
var request = new UnityWebRequest(url, "POST");
|
||||
var bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
callback?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("HTTP POST Error: " + request.error);
|
||||
}
|
||||
|
||||
request.Dispose();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
#if !UNITY_WEBGL || UNITY_EDITOR
|
||||
|
||||
Reference in New Issue
Block a user