From a29fffa93dc66ff45589f8fd1f8584f7bcb9fdad Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期四, 05 二月 2026 18:41:17 +0800
Subject: [PATCH] h5 tcp switch to websocket
---
Main/Core/NetworkPackage/GameNetSystem.cs | 162 +++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 136 insertions(+), 26 deletions(-)
diff --git a/Main/Core/NetworkPackage/GameNetSystem.cs b/Main/Core/NetworkPackage/GameNetSystem.cs
index a0661a3..7bbd6e3 100644
--- a/Main/Core/NetworkPackage/GameNetSystem.cs
+++ b/Main/Core/NetworkPackage/GameNetSystem.cs
@@ -2,14 +2,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using Cysharp.Threading.Tasks;
+public enum NetworkType
+{
+ TCP, // 浣跨敤鍘熸湁鐨� TCP Socket
+ WebSocket // 浣跨敤 WebSocket锛堝皬娓告垙骞冲彴锛�
+}
public class GameNetSystem : Singleton<GameNetSystem>
{
//闄愬埗瀹㈡埛绔殑涓嬩竴涓寘鏄櫥褰曞寘C0101_tagCPlayerLogin锛屽鏋滀笉鏄櫥褰曞寘涓嶅厑璁稿彂閫�
bool waitLogin = false; //绛夊緟鍙戦�佺櫥褰曞寘锛屽鏋滄湁鍏朵粬鍖呯洿鎺ュ睆钄斤紝閬垮厤鏂嚎閲嶈繛鐨勬儏鍐靛彂浜嗘敾鍑诲寘涔嬬被鐨�
//绛夊緟鏈嶅姟绔�0403鐨勫寘鍚庢墠鑳藉彂鍏朵粬鐨勫姛鑳藉寘锛屽彧鏈塁0123_tagCClientPackVersion 鍜� C0101_tagCPlayerLogin 鍙互鍙戦��
- bool waitLoginMap = false;
+ bool waitLoginMap = false;
+
+ // 缃戠粶鏈嶅姟鎺ュ彛锛堢粺涓�鏀寔 TCP 鍜� WebSocket锛�
+ private INetworkService networkService;
+
NetUpdateBehaviour m_NetUpdateBehaviour;
NeverConnectState neverConnectState;
AccountLoginState accountLoginState;
@@ -74,12 +84,26 @@
}
}
- private ClientSocket mainSocket;
- public bool mainSocketConnected { get { return mainSocket == null ? false : mainSocket.connected; } }
+ private ClientSocket mainSocket; // 淇濈暀鍏煎锛屼絾涓嶅啀鐩存帴浣跨敤
+
+ public bool mainSocketConnected
+ {
+ get { return networkService?.IsConnected ?? false; }
+ }
public float timeSinceMainSocketLastProtocol
{
- get { return mainSocket == null ? Time.time : (float)(DateTime.Now - mainSocket.lastPackageTime).TotalSeconds; }
+ get
+ {
+ if (networkService == null)
+ return Time.time;
+
+ var lastTime = networkService.LastPackageTime;
+ if (lastTime == DateTime.MinValue)
+ return Time.time; // 浠庢湭鏀跺寘
+
+ return (float)(DateTime.Now - lastTime).TotalSeconds;
+ }
}
@@ -107,9 +131,9 @@
{
try
{
- if (mainSocketConnected)
+ if (networkService != null && networkService.IsConnected)
{
- mainSocket.CloseConnect();
+ networkService.Disconnect();
}
}
catch (System.Exception ex)
@@ -117,16 +141,98 @@
Debug.Log(ex);
}
- mainSocket = new ClientSocket(ServerType.Main);
mainProtocolQueue.Clear();
- mainSocket.Connect(ip, port, (bool ok) =>
+ // 鏍规嵁 Main.CurrentNetworkType 閫夋嫨缃戠粶绫诲瀷
+ Debug.Log($"[GameNetSystem] 杩炴帴鏈嶅姟鍣� {ip}:{port}锛屼娇鐢▄(Main.CurrentNetworkType == NetworkType.WebSocket ? "WebSocket" : "TCP")}");
+
+ if (Main.CurrentNetworkType == NetworkType.WebSocket)
{
+ // 浣跨敤 WebSocket
+ ConnectWithWebSocket(ip, port, onConnected);
+ }
+ else
+ {
+ // 浣跨敤 TCP Socket锛堥�氳繃閫傞厤鍣級
+ ConnectWithTCP(ip, port, onConnected);
+ }
+ }
+
+ private async void ConnectWithTCP(string ip, int port, Action<bool> onConnected)
+ {
+ try
+ {
+ // 鍒涘缓 TCP Socket 閫傞厤鍣紙涓嶄慨鏀� ClientSocket锛�
+ networkService = new ClientSocketAdapter(ServerType.Main);
+
+ // 璁㈤槄鐘舵�佷簨浠�
+ networkService.OnStateChanged += OnNetworkStateChanged;
+
+ // 杩炴帴
+ string url = $"{ip}:{port}";
+ Debug.Log($"[GameNetSystem] TCP 寮�濮嬭繛鎺�: {url}");
+ bool success = await networkService.ConnectAsync(url);
+
+ Debug.Log($"[GameNetSystem] TCP 杩炴帴{(success ? "鎴愬姛" : "澶辫触")}: {url}");
+
+ // 鍏煎锛氫繚鐣� mainSocket 寮曠敤锛堜絾閫氳繃 networkService 璁块棶锛�
+ mainSocket = (networkService as ClientSocketAdapter)?.GetClientSocket();
+
if (onConnected != null)
{
- onConnected(ok);
+ onConnected(success);
}
- });
+ }
+ catch (System.Exception ex)
+ {
+ Debug.LogError($"[GameNetSystem] TCP 杩炴帴寮傚父: {ex.Message}");
+ if (onConnected != null)
+ {
+ onConnected(false);
+ }
+ }
+ }
+
+ private async void ConnectWithWebSocket(string ip, int port, Action<bool> onConnected)
+ {
+ try
+ {
+ // 鍒涘缓 WebSocket 鏈嶅姟锛堜紶鍏� ServerType锛屼笌 ClientSocket 涓�鑷达級
+ networkService = new WebSocketNetworkService(ServerType.Main);
+
+ // 璁㈤槄鐘舵�佷簨浠�
+ networkService.OnStateChanged += OnNetworkStateChanged;
+
+ string url = $"ws://{ip}:{port}";
+ Debug.Log($"[GameNetSystem] WebSocket 寮�濮嬭繛鎺�: {url}");
+ bool success = await networkService.ConnectAsync(url);
+
+ Debug.Log($"[GameNetSystem] WebSocket 杩炴帴{(success ? "鎴愬姛" : "澶辫触")}: {url}");
+
+ if (onConnected != null)
+ {
+ onConnected(success);
+ }
+ }
+ catch (System.Exception ex)
+ {
+ Debug.LogError($"[GameNetSystem] WebSocket 杩炴帴寮傚父: {ex.Message}");
+ if (onConnected != null)
+ {
+ onConnected(false);
+ }
+ }
+ }
+
+ private void OnNetworkStateChanged(NetworkState state)
+ {
+ Debug.Log($"[GameNetSystem] 缃戠粶鐘舵�佸彉鍖�: {state}");
+
+ // 鏂紑杩炴帴鏃惰Е鍙戞柇绾垮鐞�
+ if (state == NetworkState.Disconnected || state == NetworkState.Closed)
+ {
+ netState = NetState.DisConnected;
+ }
}
//闄愬埗瀹㈡埛绔殑涓嬩竴涓寘鏄櫥褰曞寘C0101_tagCPlayerLogin锛屽鏋滀笉鏄櫥褰曞寘涓嶅厑璁稿彂閫�
@@ -152,18 +258,17 @@
public void SendCachePackage()
{
int cnt = sendQueue.Count;
- if (mainSocket != null)
+ while (sendQueue.Count > 0)
{
- while (sendQueue.Count > 0)
- {
- SendInfo(sendQueue.Dequeue());
- }
+ SendInfo(sendQueue.Dequeue());
}
- Debug.LogError($"閲嶇偣鎻愰啋锛�0403鐧诲綍鍚� 鍙戦�佺紦瀛樺寘鏁伴噺 {cnt} 涓�");
+ Debug.Log($"閲嶇偣鎻愰啋锛�0403鐧诲綍鍚� 鍙戦�佺紦瀛樺寘鏁伴噺 {cnt} 涓�");
}
public void SendInfo(GameNetPackBasic protocol)
{
+ Debug.LogError("protocol to send: " + protocol.ToString());
+
if (waitLogin)
{
if (protocol is not C0101_tagCPlayerLogin)
@@ -184,18 +289,18 @@
}
}
- if (mainSocket != null)
+ if (networkService != null)
{
- mainSocket.SendInfo(protocol);
+ networkService.SendInfo(protocol);
DebugPkgCache.Push(protocol);
}
}
public void SendInfo(byte[] vBytes)
{
- if (mainSocket != null)
+ if (networkService != null)
{
- mainSocket.SendInfo(vBytes);
+ networkService.SendInfo(vBytes);
}
}
@@ -226,9 +331,9 @@
{
try
{
- if (mainSocket != null)
+ if (networkService != null)
{
- mainSocket.CloseConnect();
+ networkService.Disconnect();
}
mainProtocolQueue.Clear();
@@ -250,9 +355,9 @@
{
try
{
- if (mainSocket != null)
+ if (networkService != null)
{
- mainSocket.CloseConnect();
+ networkService.Disconnect();
}
mainProtocolQueue.Clear();
@@ -275,9 +380,9 @@
{
SDKUtils.Instance.RoleLoginOut();
- if (mainSocket != null)
+ if (networkService != null)
{
- mainSocket.CloseConnect();
+ networkService.Disconnect();
}
mainProtocolQueue.Clear();
@@ -309,6 +414,11 @@
void OnUpdate()
{
+ if (networkService != null)
+ {
+ if (networkService is WebSocketNetworkService wsService) { wsService.Update(); }
+ }
+
lock (this)
{
while (mainProtocolQueue.Count > 0)
--
Gitblit v1.8.0