Files
5.24BuYu/Assets/MyGame/Scripts/Net/WebSocketManager.cs
T

213 lines
6.8 KiB
C#
Raw Normal View History

2026-02-03 11:35:19 +08:00
using Assets.MyGame.Scripts.Net.WebResponses;
using Best.WebSockets;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
2026-03-09 09:25:01 +08:00
using Unity.VisualScripting;
2026-02-03 11:35:19 +08:00
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Assets.MyGame.Scripts.Net
{
public enum SocketType
{
login,
room,
game,
2026-04-15 18:30:37 +08:00
chess,
customer,
2026-02-03 11:35:19 +08:00
}
public partial class WebSocketManager : MonoSingleton<WebSocketManager>
{
private readonly Dictionary<SocketType, WebSocket> sockets = new();
private readonly Dictionary<SocketType, string> socketPaths = new();
2026-02-13 19:23:01 +08:00
//private const string baseUrl = "ws://game5241.wzaxr.top";
2026-03-27 15:18:35 +08:00
private const string baseUrl = "ws://8.137.163.153";
2026-03-18 17:26:14 +08:00
public bool isGame=false;
2026-02-03 11:35:19 +08:00
protected override void Awake()
{
base.Awake();
2026-02-09 10:44:38 +08:00
socketPaths[SocketType.game] = "/gamehub";
socketPaths[SocketType.room] = "/fishingroomhub";
socketPaths[SocketType.login] = "/loginhub";
2026-02-13 19:23:01 +08:00
socketPaths[SocketType.chess] = "/chessgamehub";
2026-04-15 18:30:37 +08:00
socketPaths[SocketType.customer] = "/customerHub";
2026-02-03 11:35:19 +08:00
}
/// <summary>
/// 创建或返回已存在的 WebSocket
/// </summary>
2026-02-13 19:23:01 +08:00
public WebSocket Connect(SocketType type,Action action=null)
2026-02-03 11:35:19 +08:00
{
// 已存在则直接返回
if (sockets.TryGetValue(type, out var exist))
{
if (exist.IsOpen)
{
Debug.Log($"[WebSocket] {type} 已连接,直接返回");
2026-02-13 19:23:01 +08:00
switch (type)
{
case SocketType.login:
break;
case SocketType.room:
//WebSocketSendList.JoinRoom("11109", GameManager.Instance.user.id, 1);
break;
case SocketType.game:
break;
case SocketType.chess:
WebSocketSendList.GetRoomListChineseChess(1);
break;
2026-04-15 18:30:37 +08:00
case SocketType.customer:
break;
2026-02-13 19:23:01 +08:00
default:
break;
}
2026-02-03 11:35:19 +08:00
return exist;
}
}
string url = baseUrl + socketPaths[type];
var ws = new WebSocket(new Uri(url));
sockets[type] = ws;
// 事件绑定
ws.OnOpen += (web) =>
{
Debug.Log($"[WebSocket] {type} 已连接: {url}");
web.Send("{\"protocol\":\"json\",\"version\":1}\u001e ");
switch (type)
{
case SocketType.login:
WebSocketSendList.Bind(GameManager.Instance.user.id);
break;
case SocketType.room:
2026-03-09 09:25:01 +08:00
2026-02-03 11:35:19 +08:00
break;
case SocketType.game:
break;
case SocketType.chess:
break;
2026-04-15 18:30:37 +08:00
case SocketType.customer:
break;
2026-02-03 11:35:19 +08:00
default:
break;
}
2026-02-13 19:23:01 +08:00
if (action != null)
action?.Invoke();
2026-02-03 11:35:19 +08:00
};
ws.OnClosed += (web, code, reason) =>
{
Debug.Log($"[WebSocket] {type} 连接关闭 Code:{code} Reason:{reason}");
// 自动重连
2026-03-18 17:26:14 +08:00
switch (type)
2026-03-09 09:25:01 +08:00
{
2026-03-18 17:26:14 +08:00
case SocketType.login:
break;
case SocketType.room:
break;
case SocketType.game:
if (isGame)
{
int sitpos = 0;
foreach (var item in GameManager.Instance.gameData.currRoomData.players)
{
if (item.userId==GameManager.Instance.user.id)
{
sitpos = item.position; break;
}
}
StartCoroutine(Reconnect(type, () =>
{
WebSocketSendList.JoinRoom(GameManager.Instance.gameData.currRoomData.roomId, GameManager.Instance.user.id, sitpos);
}));
}
break;
case SocketType.chess:
break;
2026-04-15 18:30:37 +08:00
case SocketType.customer:
break;
2026-03-18 17:26:14 +08:00
default:
break;
2026-03-09 09:25:01 +08:00
}
2026-03-18 17:26:14 +08:00
2026-03-09 09:25:01 +08:00
2026-02-03 11:35:19 +08:00
};
ws.OnClosed += (web, code, message) =>
{
Debug.LogError($"[WebSocket] {type} 错误{code}: {message}");
};
ws.OnMessage += (web, msg) =>
{
OnMessage(web, msg, type);
};
// 保存
sockets[type] = ws;
// 发起连接
ws.Open();
return ws;
}
/// <summary>
/// 发送消息
/// </summary>
public void Send(SocketType type, string msg)
{
if (sockets.TryGetValue(type, out var ws) && ws.IsOpen)
{
ws.Send(msg + "\u001e");
Debug.LogWarning($"[WebSocket] {type}Send :" + msg);
}
else
{
Debug.LogWarning($"[WebSocket] {type} 未连接,无法发送消息");
2026-03-18 17:26:14 +08:00
UIManager.Instance.ShowBoardError($"[WebSocket] {type} 未连接,无法发送消息");
2026-02-03 11:35:19 +08:00
}
}
/// <summary>
/// 关闭某个 socket
/// </summary>
public void Close(SocketType type)
{
if (sockets.TryGetValue(type, out var ws))
{
2026-03-09 09:25:01 +08:00
ws.Close(WebSocketStatusCodes.NormalClosure, "正常关闭"); // 或者 CloseStatusCode.Normal
//ws.Close();
2026-02-03 11:35:19 +08:00
sockets.Remove(type);
}
}
/// <summary>
/// 自动重连
/// </summary>
2026-03-18 17:26:14 +08:00
private System.Collections.IEnumerator Reconnect(SocketType type,Action action=null)
2026-02-03 11:35:19 +08:00
{
Debug.Log($"[WebSocket] {type} 3 秒后尝试重连...");
yield return new WaitForSeconds(3f);
2026-03-18 17:26:14 +08:00
Connect(type,action);
2026-02-03 11:35:19 +08:00
}
/// <summary>
/// 关闭所有 socket
/// </summary>
private void OnDestroy()
{
foreach (var ws in sockets.Values)
{
ws.Close();
}
sockets.Clear();
}
}
}