From 18e418ca0b5354d464f242be08958b90ee8d779a Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期五, 06 二月 2026 14:59:24 +0800
Subject: [PATCH] TCP/WEBSOCKET SUPPORT
---
Main/Core/NetworkPackage/GameNetSystem.cs | 179 +++++++++++++----------------------------------------------
1 files changed, 39 insertions(+), 140 deletions(-)
diff --git a/Main/Core/NetworkPackage/GameNetSystem.cs b/Main/Core/NetworkPackage/GameNetSystem.cs
index 7bbd6e3..94d9e5e 100644
--- a/Main/Core/NetworkPackage/GameNetSystem.cs
+++ b/Main/Core/NetworkPackage/GameNetSystem.cs
@@ -2,24 +2,14 @@
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;
-
- // 缃戠粶鏈嶅姟鎺ュ彛锛堢粺涓�鏀寔 TCP 鍜� WebSocket锛�
- private INetworkService networkService;
-
+ bool waitLoginMap = false;
NetUpdateBehaviour m_NetUpdateBehaviour;
NeverConnectState neverConnectState;
AccountLoginState accountLoginState;
@@ -84,26 +74,12 @@
}
}
- private ClientSocket mainSocket; // 淇濈暀鍏煎锛屼絾涓嶅啀鐩存帴浣跨敤
-
- public bool mainSocketConnected
- {
- get { return networkService?.IsConnected ?? false; }
- }
+ private ClientSocket mainSocket;
+ public bool mainSocketConnected { get { return mainSocket == null ? false : mainSocket.connected; } }
public float timeSinceMainSocketLastProtocol
{
- get
- {
- if (networkService == null)
- return Time.time;
-
- var lastTime = networkService.LastPackageTime;
- if (lastTime == DateTime.MinValue)
- return Time.time; // 浠庢湭鏀跺寘
-
- return (float)(DateTime.Now - lastTime).TotalSeconds;
- }
+ get { return mainSocket == null ? Time.time : (float)(DateTime.Now - mainSocket.lastPackageTime).TotalSeconds; }
}
@@ -131,9 +107,9 @@
{
try
{
- if (networkService != null && networkService.IsConnected)
+ if (mainSocketConnected)
{
- networkService.Disconnect();
+ mainSocket.CloseConnect();
}
}
catch (System.Exception ex)
@@ -141,98 +117,22 @@
Debug.Log(ex);
}
- mainProtocolQueue.Clear();
-
- // 鏍规嵁 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(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)
+ mainSocket = new ClientSocket(ServerType.Main);
+ // websocket鐨勬柇寮�閾炬帴闇�瑕佸鐞嗕竴涓�
+ mainSocket.OnDisconnected = () =>
{
netState = NetState.DisConnected;
- }
+ LoginManager.Instance.busy = false;
+ };
+ mainProtocolQueue.Clear();
+
+ mainSocket.Connect(ip, port, (bool ok) =>
+ {
+ if (onConnected != null)
+ {
+ onConnected(ok);
+ }
+ });
}
//闄愬埗瀹㈡埛绔殑涓嬩竴涓寘鏄櫥褰曞寘C0101_tagCPlayerLogin锛屽鏋滀笉鏄櫥褰曞寘涓嶅厑璁稿彂閫�
@@ -258,17 +158,18 @@
public void SendCachePackage()
{
int cnt = sendQueue.Count;
- while (sendQueue.Count > 0)
+ if (mainSocket != null)
{
- SendInfo(sendQueue.Dequeue());
+ while (sendQueue.Count > 0)
+ {
+ SendInfo(sendQueue.Dequeue());
+ }
}
- Debug.Log($"閲嶇偣鎻愰啋锛�0403鐧诲綍鍚� 鍙戦�佺紦瀛樺寘鏁伴噺 {cnt} 涓�");
+ Debug.LogError($"閲嶇偣鎻愰啋锛�0403鐧诲綍鍚� 鍙戦�佺紦瀛樺寘鏁伴噺 {cnt} 涓�");
}
public void SendInfo(GameNetPackBasic protocol)
{
- Debug.LogError("protocol to send: " + protocol.ToString());
-
if (waitLogin)
{
if (protocol is not C0101_tagCPlayerLogin)
@@ -289,21 +190,22 @@
}
}
- if (networkService != null)
+ if (mainSocket != null)
{
- networkService.SendInfo(protocol);
+ mainSocket.SendInfo(protocol);
DebugPkgCache.Push(protocol);
}
}
+#if UNITY_EDITOR
public void SendInfo(byte[] vBytes)
{
- if (networkService != null)
+ if (mainSocket != null)
{
- networkService.SendInfo(vBytes);
+ mainSocket.SendInfo(vBytes);
}
}
-
+#endif
public void PushPackage(GameNetPackBasic protocol, ServerType type)
{
@@ -331,9 +233,9 @@
{
try
{
- if (networkService != null)
+ if (mainSocket != null)
{
- networkService.Disconnect();
+ mainSocket.CloseConnect();
}
mainProtocolQueue.Clear();
@@ -355,9 +257,9 @@
{
try
{
- if (networkService != null)
+ if (mainSocket != null)
{
- networkService.Disconnect();
+ mainSocket.CloseConnect();
}
mainProtocolQueue.Clear();
@@ -380,9 +282,9 @@
{
SDKUtils.Instance.RoleLoginOut();
- if (networkService != null)
+ if (mainSocket != null)
{
- networkService.Disconnect();
+ mainSocket.CloseConnect();
}
mainProtocolQueue.Clear();
@@ -414,10 +316,7 @@
void OnUpdate()
{
- if (networkService != null)
- {
- if (networkService is WebSocketNetworkService wsService) { wsService.Update(); }
- }
+ mainSocket?.DispatchMessageQueue();
lock (this)
{
--
Gitblit v1.8.0