1
This commit is contained in:
@@ -24,7 +24,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
private readonly Dictionary<SocketType, string> socketPaths = new();
|
||||
|
||||
//private const string baseUrl = "ws://game5241.wzaxr.top";
|
||||
#if UNITY_EDITOR
|
||||
private const string baseUrl = "ws://8.137.163.153";
|
||||
#else
|
||||
private const string baseUrl = "ws://8.137.8.46";
|
||||
#endif
|
||||
public bool isGame=false;
|
||||
protected override void Awake()
|
||||
{
|
||||
@@ -47,7 +51,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
{
|
||||
if (exist.IsOpen)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Debug.Log($"[WebSocket] {type} 已连接,直接返回");
|
||||
|
||||
|
||||
#endif
|
||||
switch (type)
|
||||
{
|
||||
case SocketType.login:
|
||||
@@ -76,7 +84,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
// 事件绑定
|
||||
ws.OnOpen += (web) =>
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Debug.Log($"[WebSocket] {type} 已连接: {url}");
|
||||
|
||||
|
||||
#endif
|
||||
web.Send("{\"protocol\":\"json\",\"version\":1}\u001e ");
|
||||
switch (type)
|
||||
{
|
||||
@@ -102,7 +114,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
|
||||
ws.OnClosed += (web, code, reason) =>
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Debug.Log($"[WebSocket] {type} 连接关闭 Code:{code} Reason:{reason}");
|
||||
|
||||
|
||||
#endif
|
||||
// 自动重连
|
||||
switch (type)
|
||||
{
|
||||
@@ -140,7 +156,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
|
||||
ws.OnClosed += (web, code, message) =>
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
Debug.LogError($"[WebSocket] {type} 错误{code}: {message}");
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
ws.OnMessage += (web, msg) =>
|
||||
@@ -165,11 +185,19 @@ namespace Assets.MyGame.Scripts.Net
|
||||
if (sockets.TryGetValue(type, out var ws) && ws.IsOpen)
|
||||
{
|
||||
ws.Send(msg + "\u001e");
|
||||
#if UNITY_EDITOR
|
||||
|
||||
Debug.LogWarning($"[WebSocket] {type}Send :" + msg);
|
||||
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
Debug.LogWarning($"[WebSocket] {type} 未连接,无法发送消息");
|
||||
|
||||
#endif
|
||||
UIManager.Instance.ShowBoardError($"[WebSocket] {type} 未连接,无法发送消息");
|
||||
}
|
||||
}
|
||||
@@ -192,7 +220,11 @@ namespace Assets.MyGame.Scripts.Net
|
||||
/// </summary>
|
||||
private System.Collections.IEnumerator Reconnect(SocketType type,Action action=null)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
Debug.Log($"[WebSocket] {type} 3 秒后尝试重连...");
|
||||
|
||||
#endif
|
||||
yield return new WaitForSeconds(3f);
|
||||
Connect(type,action);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user