From 01b5728a890315e4dbe9aabf7225a7957171e1a5 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期四, 07 五月 2026 18:00:36 +0800
Subject: [PATCH] webgl2
---
Main/Core/NetworkPackage/Socket/ClientSocket.cs | 87 +++++++++++++++++++++++++++++++++++++++----
1 files changed, 78 insertions(+), 9 deletions(-)
diff --git a/Main/Core/NetworkPackage/Socket/ClientSocket.cs b/Main/Core/NetworkPackage/Socket/ClientSocket.cs
index ae90b8d..9bc71b1 100644
--- a/Main/Core/NetworkPackage/Socket/ClientSocket.cs
+++ b/Main/Core/NetworkPackage/Socket/ClientSocket.cs
@@ -76,11 +76,13 @@
public void Connect(string _ip, int _port, Action<bool> _onConnected)
{
+ Debug.unityLogger.logEnabled = true;
try
{
ip = _ip;
port = _port;
onConnected = _onConnected;
+ Debug.Log($"[ClientSocket][Connect] 灏濊瘯杩炴帴: ip={_ip}, port={_port}");
//鐩墠娴嬭瘯鍒板紓姝ヤ袱涓棶棰�
// 1. BeginGetHostAddresses 涓嶆槑鎯呭喌涓嬩細寰堜箙鎵嶅洖璋冿紝瀵艰嚧瑙﹀彂瓒呮椂
// 2. 瓒呮椂鐨勬儏鍐典笅澶氭灏濊瘯鐧诲綍鍚庯紝浼氳Е鍙戝娆nGetHostAddresses锛屽鑷寸櫥褰曞紓甯�
@@ -96,29 +98,29 @@
ipAddress = ipAddresses[0];
#endif
-
+ Debug.Log($"[ClientSocket][Connect] 瑙f瀽鍒癷pAddress={ipAddress}, family={ipAddress.AddressFamily}");
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
- Debug.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV6");
+ Debug.Log("[ClientSocket][Connect] 褰撳墠浣跨敤鐨勭綉缁�: IPV6");
m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
}
else
{
- Debug.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV4");
+ Debug.Log("[ClientSocket][Connect] 褰撳墠浣跨敤鐨勭綉缁�: IPV4");
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
var ipEndPoint = new IPEndPoint(ipAddress, port);
if (ipEndPoint == null)
{
- Debug.Log("IpEndPoint is null");
+ Debug.LogError("[ClientSocket][Connect] IpEndPoint is null");
}
m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null);
}
catch (Exception e)
{
- Debug.LogError(e.Message);
+ Debug.LogError($"[ClientSocket][Connect] 寮傚父: {e.Message}");
}
@@ -154,12 +156,14 @@
/// <param name="_result"></param>
private void ConnectCallBack(IAsyncResult _result)
{
+ Debug.unityLogger.logEnabled = true;
if (!_result.IsCompleted)
{
- Debug.Log("閾炬帴瓒呮椂锛�");
+ Debug.LogError("[ClientSocket][ConnectCallBack] 閾炬帴瓒呮椂锛�");
CloseConnect();
if (onConnected != null)
{
+ Debug.LogError("[ClientSocket][ConnectCallBack] onConnected(false) 瓒呮椂");
onConnected(false);
onConnected = null;
}
@@ -170,11 +174,12 @@
{
if (m_Socket != null && m_Socket.Connected)
{
- Debug.Log("纭鐨勯摼鎺ュ疄鐜�");
+ Debug.Log("[ClientSocket][ConnectCallBack] 纭鐨勯摼鎺ュ疄鐜�");
OnConnectSuccess();
}
else
{
+ Debug.LogError("[ClientSocket][ConnectCallBack] m_Socket涓簄ull鎴栨湭杩炴帴");
if (m_Socket != null)
{
m_Socket.Disconnect(true);
@@ -183,12 +188,13 @@
}
catch (System.Exception ex)
{
- Debug.Log(ex);
+ Debug.LogError($"[ClientSocket][ConnectCallBack] 寮傚父: {ex}");
}
finally
{
if (onConnected != null)
{
+ Debug.Log($"[ClientSocket][ConnectCallBack] onConnected({{0}})", m_Socket != null && m_Socket.Connected);
onConnected(m_Socket != null && m_Socket.Connected);
onConnected = null;
}
@@ -205,6 +211,8 @@
public void CloseConnect()
{
Debug.Log("==== CloseConnect");
+ Debug.unityLogger.logEnabled = true;
+ Debug.Log("[ClientSocket][CloseConnect] ==== CloseConnect");
try
{
isStopTreading = true;
@@ -242,6 +250,20 @@
{
if (m_packageThread != null)
{
+ m_packageThread.Abort();
+ m_packageThread = null;
+ }
+
+ m_LastPackageTime = DateTime.Now;
+ m_packageThread = new Thread(new ThreadStart(ReceiveInfo)); // 鍚姩绾跨▼鎺ユ敹淇℃伅
+ m_packageThread.IsBackground = true;
+ m_packageThread.Start();
+ isStopTreading = false;
+ Debug.unityLogger.logEnabled = true;
+ Debug.Log("[ClientSocket][OnConnectSuccess] 杩炴帴鎴愬姛锛屽惎鍔ㄦ帴鏀剁嚎绋�");
+ if (m_packageThread != null)
+ {
+ Debug.LogWarning("[ClientSocket][OnConnectSuccess] m_packageThread宸插瓨鍦紝鍏圓bort");
m_packageThread.Abort();
m_packageThread = null;
}
@@ -296,6 +318,51 @@
Debug.Log(e);
}
}
+ Debug.unityLogger.logEnabled = true;
+ Debug.Log("[ClientSocket][ReceiveInfo] 鎺ユ敹绾跨▼鍚姩");
+ while (!isStopTreading)
+ {
+ try
+ {
+ var shutdown = false;
+ if (!m_Socket.Connected)
+ {
+ Debug.LogWarning("[ClientSocket][ReceiveInfo] m_Socket 宸叉柇寮�");
+ shutdown = true;
+ }
+
+ if (!shutdown)
+ {
+ var dataLength = m_Socket.Receive(bufferBytes);
+ Debug.Log($"[ClientSocket][ReceiveInfo] 鏀跺埌鏁版嵁闀垮害: {dataLength}");
+ if (dataLength <= 0)
+ {
+ Debug.LogWarning("[ClientSocket][ReceiveInfo] dataLength <= 0锛屽噯澶囨柇寮�");
+ shutdown = true;
+ }
+ else
+ {
+ getBytesTotal += dataLength;
+ var bytes = new byte[dataLength];
+ Array.Copy(bufferBytes, 0, bytes, 0, dataLength);
+ ReadInfo(bytes);
+ }
+ }
+
+ if (shutdown)
+ {
+ Debug.LogWarning("[ClientSocket][ReceiveInfo] shutdown=true锛屽叧闂璖ocket");
+ isStopTreading = true;
+ m_Socket.Shutdown(SocketShutdown.Both);
+ m_Socket.Close();
+ }
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"[ClientSocket][ReceiveInfo] 寮傚父: {e}");
+ }
+ }
+ Debug.Log("[ClientSocket][ReceiveInfo] 鎺ユ敹绾跨▼閫�鍑�");
}
@@ -312,6 +379,7 @@
if (fragmentBytes != null && fragmentBytes.Length > 0)
{
Array.Resize(ref fixBytes, vBytes.Length + fragmentBytes.Length);
+ Debug.Log($"[ClientSocket][ReadInfo] 瀛樺湪fragmentBytes, 闀垮害: {fragmentBytes.Length}");
Array.Copy(fragmentBytes, 0, fixBytes, 0, fragmentBytes.Length);
Array.Copy(vBytes, 0, fixBytes, fragmentBytes.Length, vBytes.Length);
}
@@ -439,6 +507,7 @@
public async void Connect(string _ip, int _port, Action<bool> _onConnected)
{
+ Debug.unityLogger.logEnabled = true;
ip = _ip;
port = _port;
onConnected = _onConnected;
@@ -597,7 +666,7 @@
public async void CloseConnect()
{
- Debug.Log("[ClientSocket-WebSocket] ==== CloseConnect");
+ Debug.Log("[ClientSocket-WebSocket] ==== CloseConnect\n" + System.Environment.StackTrace);
fragmentBytes = null;
if (webSocket != null)
--
Gitblit v1.8.0