226 lines
6.3 KiB
C#
226 lines
6.3 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using WebSocketSharp;
|
|
|
|
namespace Game
|
|
{
|
|
public interface ISocketManager
|
|
{
|
|
void SetHttpToken(string token);
|
|
void SetHttpKey(string requestKey);
|
|
void Connect();
|
|
UniTask ConnectAsync();
|
|
void SendMessage(string message);
|
|
UniTask<bool> SendMessageAsync(string message);
|
|
void CloseConnect();
|
|
}
|
|
|
|
public class SocketManager : ManagerBase, ISocketManager
|
|
{
|
|
private WebSocket _webSocket;
|
|
private string token;
|
|
private string requestKey;
|
|
private bool isOpen;
|
|
|
|
public SocketManager()
|
|
{
|
|
token = default;
|
|
requestKey = default;
|
|
}
|
|
|
|
public void SetHttpToken(string token)
|
|
{
|
|
this.token = token;
|
|
}
|
|
|
|
public void SetHttpKey(string requestKey)
|
|
{
|
|
this.requestKey = requestKey;
|
|
}
|
|
|
|
async UniTask DisconnectAndReconnect()
|
|
{
|
|
// int time = 5;
|
|
// int i = 0;
|
|
// while (this.isOpen)
|
|
// {
|
|
// if (!this._webSocket.IsAlive)
|
|
// {
|
|
// i++;
|
|
// await this.ConnectAsync();
|
|
// Debug.Log($"断线了,重连了{i}次");
|
|
// await UniTask.Yield();
|
|
// }
|
|
//
|
|
// await UniTask.Yield();
|
|
// }
|
|
await UniTask.Yield();
|
|
}
|
|
|
|
async UniTask Ping()
|
|
{
|
|
// float time = 0;
|
|
// int i = 0;
|
|
// while (this.isOpen)
|
|
// {
|
|
// time += Time.deltaTime;
|
|
// if (time >= 5)
|
|
// {
|
|
// time = 0;
|
|
// if (this._webSocket.IsAlive)
|
|
// {
|
|
// this._webSocket.Send("ping");
|
|
// Debug.Log("ping");
|
|
// }
|
|
// }
|
|
//
|
|
// await UniTask.Yield();
|
|
// }
|
|
await UniTask.Yield();
|
|
}
|
|
|
|
private float timePing = 0;
|
|
private float timeDis = 0;
|
|
private int countDis = 0;
|
|
|
|
protected override void OnUpdate(float dateTime)
|
|
{
|
|
base.OnUpdate(dateTime);
|
|
//
|
|
if (this.isOpen && this._webSocket != null && !this._webSocket.IsAlive)
|
|
{
|
|
timeDis += Time.deltaTime;
|
|
if (this.timeDis >= 5)
|
|
{
|
|
countDis++;
|
|
this.Connect();
|
|
// Debug.Log($"断线了,重连了{this.countDis}次");
|
|
timeDis = 0;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (this.isOpen && this._webSocket != null && this._webSocket.IsAlive)
|
|
{
|
|
this.timePing += Time.deltaTime;
|
|
if (this.timePing >= 20)
|
|
{
|
|
this.timePing = 0;
|
|
if (this._webSocket.IsAlive)
|
|
{
|
|
this._webSocket.Send("ping");
|
|
// Debug.Log("ping");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Connect()
|
|
{
|
|
if (this._webSocket == null)
|
|
{
|
|
using (this._webSocket = new WebSocket(NetworkConst.socketUrl))
|
|
{
|
|
this.isOpen = true;
|
|
Conn();
|
|
this._webSocket.OnClose += OnClose;
|
|
this._webSocket.OnOpen += OnOpen;
|
|
this._webSocket.OnError += OnError;
|
|
this._webSocket.OnMessage += OnMessage;
|
|
DisconnectAndReconnect().Forget();
|
|
this.Ping().Forget();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Conn();
|
|
}
|
|
}
|
|
|
|
public async UniTask ConnectAsync()
|
|
{
|
|
if (this._webSocket == null)
|
|
{
|
|
using (this._webSocket = new WebSocket(NetworkConst.socketUrl))
|
|
{
|
|
this.isOpen = true;
|
|
Conn();
|
|
await UniTask.Yield();
|
|
this._webSocket.OnClose += OnClose;
|
|
this._webSocket.OnOpen += OnOpen;
|
|
this._webSocket.OnError += OnError;
|
|
this._webSocket.OnMessage += OnMessage;
|
|
DisconnectAndReconnect().Forget();
|
|
this.Ping().Forget();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Conn();
|
|
await UniTask.Yield();
|
|
}
|
|
}
|
|
|
|
void Conn()
|
|
{
|
|
this._webSocket.Connect();
|
|
}
|
|
|
|
public void SendMessage(string message)
|
|
{
|
|
this._webSocket.Send(message);
|
|
}
|
|
|
|
public async UniTask<bool> SendMessageAsync(string message)
|
|
{
|
|
int index = -1;
|
|
this._webSocket.SendAsync(message, b => { index = b ? 0 : 1; });
|
|
while (true)
|
|
{
|
|
if (index != -1)
|
|
return index == 0 ? true : false;
|
|
|
|
await UniTask.Yield();
|
|
}
|
|
}
|
|
|
|
public void CloseConnect()
|
|
{
|
|
this.isOpen = false;
|
|
this._webSocket.Close();
|
|
this._webSocket.OnClose -= OnClose;
|
|
this._webSocket.OnOpen -= OnOpen;
|
|
this._webSocket.OnError -= OnError;
|
|
this._webSocket.OnMessage -= OnMessage;
|
|
this._webSocket = null;
|
|
token = default;
|
|
requestKey = default;
|
|
}
|
|
|
|
private void OnMessage(object sender, MessageEventArgs e)
|
|
{
|
|
UnityEngine.Debug.Log($"OnMessage : {e.Data}");
|
|
}
|
|
|
|
private void OnError(object sender, ErrorEventArgs e)
|
|
{
|
|
UnityEngine.Debug.Log($"OnError : {e.Message}");
|
|
}
|
|
|
|
private void OnOpen(object sender, EventArgs e)
|
|
{
|
|
Debug.Log("token is " + this.token);
|
|
this._webSocket.Send($"{{\n\t\"requestKey\": \"{this.requestKey}\",\n\t\"token\": \"{this.token}\"\n}}");
|
|
UnityEngine.Debug.Log($"OnOpen : {e}");
|
|
}
|
|
|
|
private void OnClose(object sender, CloseEventArgs e)
|
|
{
|
|
UnityEngine.Debug.Log($"OnClose : {e.Code}");
|
|
}
|
|
}
|
|
} |