yyl
2026-05-08 ea4e25ceca21484cbb422b4ce49ec6bc1220441f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
using UnityEngine;
using System;
using System.Collections.Generic;
 
#if !UNITY_WEBGL
using System.Net;
using System.Net.Sockets;
using System.Threading;
#else
using NativeWebSocket;
using Cysharp.Threading.Tasks;
#endif
 
/// <summary>
/// 统一的网络Socket类 - 自动适配TCP Socket和WebSocket
/// TCP Socket: Windows, Mac, iOS, Android等平台
/// WebSocket: WebGL/微信小游戏等Web平台
/// </summary>
public class ClientSocket
{
    GameNetEncode encoder = new GameNetEncode();
    
#if !UNITY_WEBGL
    // TCP Socket 实现(非WebGL平台)
    Socket m_Socket;
    public Socket socket { get { return m_Socket; } }
    private Thread m_packageThread;
    private byte[] bufferBytes = new byte[4096];
    private byte[] fragmentBytes; // TCP分包缓存
    bool isStopTreading = false;
#else
    // WebSocket 实现(WebGL平台)
    WebSocket webSocket;
    public WebSocket socket { get { return webSocket; } }
    private byte[] fragmentBytes; // TCP-to-WS网关按TCP缓冲区拆包,需要跨消息重组
#endif
 
    public Action OnDisconnected;
 
    private long getBytesTotal = 0;
    private long sendBytesTotal = 0;
    
    public bool connected 
    { 
        get 
        {
#if !UNITY_WEBGL
            return m_Socket == null ? false : m_Socket.Connected;
#else
            return webSocket != null && webSocket.State == WebSocketState.Open;
#endif
        } 
    }
 
    ServerType socketType = ServerType.Main;
    DateTime m_LastPackageTime;
    public DateTime lastPackageTime { get { return m_LastPackageTime; } }
 
    string ip;
    int port;
    Action<bool> onConnected = null;
    Queue<byte[]> sendQueue = new Queue<byte[]>();
    static byte[] vCmdBytes = new byte[2];
#if UNITY_WEBGL
    string webSocketUrl;
    bool webSocketOpened;
#endif
 
    public ClientSocket(ServerType type)
    {
        this.socketType = type;
    }
 
#if !UNITY_WEBGL
    // ==================== TCP Socket 实现 ====================
 
    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. 超时的情况下多次尝试登录后,会触发多次OnGetHostAddresses,导致登录异常
            //Dns.BeginGetHostAddresses(_ip, OnGetHostAddresses, null);
 
            IPAddress ipAddress;
#if UNITY_IPHONE
            IPHostEntry ipAddresses = Dns.GetHostEntry(_ip);
            ipAddress = ipAddresses.AddressList[0];
            
#else
            IPAddress[] ipAddresses = Dns.GetHostAddresses(_ip);
            ipAddress = ipAddresses[0];
#endif
 
            Debug.Log($"[ClientSocket][Connect] 解析到ipAddress={ipAddress}, family={ipAddress.AddressFamily}");
            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Debug.Log("[ClientSocket][Connect] 当前使用的网络: IPV6");
                m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                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.LogError("[ClientSocket][Connect] IpEndPoint is null");
            }
 
            m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null);
        }
        catch (Exception e)
        {
            Debug.LogError($"[ClientSocket][Connect] 异常: {e.Message}");
        }
 
 
    }
 
    private void OnGetHostAddresses(IAsyncResult _result)
    {
        var ipAddresses = Dns.EndGetHostAddresses(_result);
 
        if (ipAddresses[0].AddressFamily == AddressFamily.InterNetworkV6)
        {
            Debug.Log("当前使用的网络: IPV6");
            m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
        }
        else
        {
            Debug.Log("当前使用的网络: IPV4");
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
 
        var ipEndPoint = new IPEndPoint(ipAddresses[0], port);
        if (ipEndPoint == null)
        {
            Debug.Log("IpEndPoint is null");
        }
 
        m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null);
    }
 
    /// <summary>
    /// 链接成功时的回调
    /// </summary>
    /// <param name="_result"></param>
    private void ConnectCallBack(IAsyncResult _result)
    {
        Debug.unityLogger.logEnabled = true;
        if (!_result.IsCompleted)
        {
            Debug.LogError("[ClientSocket][ConnectCallBack] 链接超时!");
            CloseConnect();
            if (onConnected != null)
            {
                Debug.LogError("[ClientSocket][ConnectCallBack] onConnected(false) 超时");
                onConnected(false);
                onConnected = null;
            }
        }
        else
        {
            try
            {
                if (m_Socket != null && m_Socket.Connected)
                {
                    Debug.Log("[ClientSocket][ConnectCallBack] 确认的链接实现");
                    OnConnectSuccess();
                }
                else
                {
                    Debug.LogError("[ClientSocket][ConnectCallBack] m_Socket为null或未连接");
                    if (m_Socket != null)
                    {
                        m_Socket.Disconnect(true);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError($"[ClientSocket][ConnectCallBack] 异常: {ex}");
            }
            finally
            {
                if (onConnected != null)
                {
                    Debug.Log($"[ClientSocket][ConnectCallBack] onConnected({m_Socket != null && m_Socket.Connected})");
                    onConnected(m_Socket != null && m_Socket.Connected);
                    onConnected = null;
                }
            }
 
        }
 
        onConnected = null;
    }
 
    /// <summary>
    /// 关闭链接
    /// </summary>
    public void CloseConnect()
    {
        Debug.Log("==== CloseConnect");
        Debug.unityLogger.logEnabled = true;
        Debug.Log("[ClientSocket][CloseConnect] ==== CloseConnect");
        try
        {
            isStopTreading = true;
            if (m_packageThread != null)
            {
                m_packageThread.Abort();
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex);
        }
 
        try
        {
            if (m_Socket != null && m_Socket.Connected)
            {
                m_Socket.Shutdown(SocketShutdown.Both);
                m_Socket.Close();
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex);
        }
 
        sendQueue.Clear();
        m_Socket = null;
    }
 
    /// <summary>
    /// 链接成功
    /// </summary>
    private void OnConnectSuccess()
    {
        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已存在,先Abort");
            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;
    }
 
    /// <summary>
    /// 接收信息
    /// </summary>
    private void ReceiveInfo()
    {
        while (!isStopTreading)
        {
            try
            {
                var shutdown = false;
                if (!m_Socket.Connected)
                {
                    shutdown = true;
                }
 
                if (!shutdown)
                {
                    var dataLength = m_Socket.Receive(bufferBytes);
                    if (dataLength <= 0)
                    {
                        shutdown = true;
                    }
                    else
                    {
                        getBytesTotal += dataLength;
                        var bytes = new byte[dataLength];
                        Array.Copy(bufferBytes, 0, bytes, 0, dataLength);
                        ReadInfo(bytes);
                    }
                }
 
                if (shutdown)
                {
                    isStopTreading = true;
                    m_Socket.Shutdown(SocketShutdown.Both);
                    m_Socket.Close();
                }
            }
            catch (Exception e)
            {
                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,关闭Socket");
                    isStopTreading = true;
                    m_Socket.Shutdown(SocketShutdown.Both);
                    m_Socket.Close();
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"[ClientSocket][ReceiveInfo] 异常: {e}");
            }
        }
        Debug.Log("[ClientSocket][ReceiveInfo] 接收线程退出");
 
    }
 
    /// <summary>
    /// 解析数据包: FFCC+封包长度+封包(封包头+数据)结构体内容
    /// </summary>
    /// <param name="vBytes"></param>
    private void ReadInfo(byte[] vBytes)
    {
        try
        {
            byte[] fixBytes = vBytes;
            // 如果存在留包,则并包
            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);
            }
 
            fragmentBytes = null; // 清理掉留包
            int vReadIndex = 0; // 初始指针
            byte[] vPackBytes;
            int vLeavingLeng = 0;
            int vBodyLeng = 0;
            int vTotalLeng = fixBytes.Length;
            GameNetPackBasic vNetpack;
 
            while (vReadIndex < vTotalLeng)
            {
                vLeavingLeng = vTotalLeng - vReadIndex;
                if (vLeavingLeng < 6) // 未符合包的最低限度字节量, 留包
                {
                    fragmentBytes = new byte[vLeavingLeng];
                    Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
                    break;
                }
                vBodyLeng = BitConverter.ToInt32(fixBytes, vReadIndex + 2);
                if (vBodyLeng > vLeavingLeng - 6)// 未完整的包则留包
                {
                    fragmentBytes = new byte[vLeavingLeng];
                    Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
                    break;
                }
                vPackBytes = new byte[vBodyLeng];
                Array.Copy(fixBytes, vReadIndex + 6, vPackBytes, 0, vBodyLeng); // 提取包的字节内容
                                                                                // 完整的包则读包
 
                vPackBytes = encoder.BaseXorSub(vPackBytes);
                Array.Copy(vPackBytes, 0, vCmdBytes, 0, 2);
                var cmd = (ushort)((ushort)(vCmdBytes[0] << 8) + vCmdBytes[1]);
                bool isRegist = false;  // 未注册封包处理
 
                // 处理主工程的封包
                if (PackageRegedit.Contain(cmd))
                {
                    vNetpack = PackageRegedit.TransPack(socketType, cmd, vPackBytes);
                    if (vNetpack != null)
                    {
                        // if (Launch.Instance.EnableNetLog)
                        // {
                        //     Debug.LogFormat("收包:{0}", vNetpack.GetType().Name);
                        // }
                        m_LastPackageTime = DateTime.Now;
                        GameNetSystem.Instance.PushPackage(vNetpack, this.socketType);
                        isRegist = true;
                    }
                }
 
 
                vReadIndex += 6 + vBodyLeng;
 
                // 未注册封包处理
                if (!isRegist)
                {
#if UNITY_EDITOR
                    PackageRegedit.TransPack(socketType, cmd, vPackBytes);
#endif
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogErrorFormat("收包异常:{0}", ex);
        }
 
    }
 
    // TCP Socket 的 SendBytes(私有方法,由统一的 SendInfo 调用)
    private void SendBytes(byte[] bytes)
    {
        // 调试日志:输出发送的字节数据
        // string hexString = "[TCP] SendBytes Length=" + bytes.Length + " Data=" + BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 32)).Replace("-", " ");
        // if (bytes.Length > 32) hexString += "...";
        // Debug.Log(hexString);
        
        try
        {
            if (sendQueue.Count > 0)
            {
                sendQueue.Enqueue(bytes);
            }
            else
            {
                m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
            }
        }
        catch
        {
            Debug.LogError("网络数据包发送异常");
        }
    }
 
    /// <summary>
    /// 发送完成的回调
    /// </summary>
    /// <param name="vAsyncSend"></param>
    private void SendInfoCallBack(IAsyncResult vAsyncSend)
    {
        try
        {
            if (sendQueue.Count > 0)
            {
                var bytes = sendQueue.Dequeue();
                m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
 
    public void DispatchMessageQueue()
    {
        // TCP不需要轮询
    }
 
#else
    // ==================== WebSocket 实现(WebGL平台)====================
    
    public async void Connect(string _ip, int _port, Action<bool> _onConnected)
    {
        Debug.unityLogger.logEnabled = true;
        ip = _ip;
        port = _port;
        onConnected = _onConnected;
 
        var scheme = Application.absoluteURL.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws";
        webSocketUrl = $"{scheme}://{_ip}:{_port}";
        webSocketOpened = false;
        Debug.Log($"[ClientSocket-WebSocket] 开始连接: {webSocketUrl}, pageUrl={Application.absoluteURL}");
        
        try
        {
            webSocket = new WebSocket(webSocketUrl);
            
            // 注册WebSocket回调
            webSocket.OnOpen += OnWebSocketOpen;
            webSocket.OnMessage += OnWebSocketMessage;
            webSocket.OnError += OnWebSocketError;
            webSocket.OnClose += OnWebSocketClose;
            
            await webSocket.Connect();
        }
        catch (Exception ex)
        {
            Debug.LogError($"[ClientSocket-WebSocket] 连接异常: {ex.Message}");
            if (onConnected != null)
            {
                onConnected(false);
                onConnected = null;
            }
        }
    }
    
    private void OnWebSocketOpen()
    {
        webSocketOpened = true;
        Debug.Log($"[ClientSocket-WebSocket] 连接成功: {webSocketUrl}");
        m_LastPackageTime = DateTime.Now;
        
        if (onConnected != null)
        {
            onConnected(true);
            onConnected = null;
        }
    }
    
    private void OnWebSocketMessage(byte[] data)
    {
        try
        {
            getBytesTotal += data.Length;
            
            byte[] fixBytes = data;
            // TCP-to-WS网关按TCP缓冲区大小拆分,需跨消息重组(与TCP ReadInfo逻辑一致)
            if (fragmentBytes != null && fragmentBytes.Length > 0)
            {
                fixBytes = new byte[fragmentBytes.Length + data.Length];
                Array.Copy(fragmentBytes, 0, fixBytes, 0, fragmentBytes.Length);
                Array.Copy(data, 0, fixBytes, fragmentBytes.Length, data.Length);
            }
            fragmentBytes = null;
 
            int vReadIndex = 0;
            byte[] vPackBytes;
            int vLeavingLeng = 0;
            int vBodyLeng = 0;
            int vTotalLeng = fixBytes.Length;
            GameNetPackBasic vNetpack;
 
            while (vReadIndex < vTotalLeng)
            {
                vLeavingLeng = vTotalLeng - vReadIndex;
                if (vLeavingLeng < 6)
                {
                    fragmentBytes = new byte[vLeavingLeng];
                    Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
                    break;
                }
                
                // 校验FFCC包头,防止数据错位
                if (fixBytes[vReadIndex] != 0xFF || fixBytes[vReadIndex + 1] != 0xCC)
                {
                    Debug.LogError($"[ClientSocket-WebSocket] FFCC包头异常: {fixBytes[vReadIndex]:X2} {fixBytes[vReadIndex + 1]:X2}, 丢弃剩余 {vLeavingLeng} 字节");
                    fragmentBytes = null;
                    break;
                }
                
                vBodyLeng = BitConverter.ToInt32(fixBytes, vReadIndex + 2);
                if (vBodyLeng <= 0)
                {
                    Debug.LogError($"[ClientSocket-WebSocket] 包体长度非法: {vBodyLeng}, 丢弃");
                    fragmentBytes = null;
                    break;
                }
                if (vBodyLeng > vLeavingLeng - 6)
                {
                    fragmentBytes = new byte[vLeavingLeng];
                    Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
                    break;
                }
                
                vPackBytes = new byte[vBodyLeng];
                Array.Copy(fixBytes, vReadIndex + 6, vPackBytes, 0, vBodyLeng);
 
                vPackBytes = encoder.BaseXorSub(vPackBytes);
                Array.Copy(vPackBytes, 0, vCmdBytes, 0, 2);
                var cmd = (ushort)((ushort)(vCmdBytes[0] << 8) + vCmdBytes[1]);
                bool isRegist = false;
 
                if (PackageRegedit.Contain(cmd))
                {
                    vNetpack = PackageRegedit.TransPack(socketType, cmd, vPackBytes);
                    if (vNetpack != null)
                    {
                        m_LastPackageTime = DateTime.Now;
                        GameNetSystem.Instance.PushPackage(vNetpack, socketType);
                        isRegist = true;
                    }
                }
 
                vReadIndex += 6 + vBodyLeng;
 
                if (!isRegist)
                {
#if UNITY_EDITOR
                    PackageRegedit.TransPack(socketType, cmd, vPackBytes);
#endif
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"[ClientSocket-WebSocket] 收包异常:{ex}");
        }
    }
    
    private void OnWebSocketError(string error)
    {
        Debug.LogError($"[ClientSocket-WebSocket] 错误: {error}");
        if (!webSocketOpened && onConnected != null)
        {
            onConnected(false);
            onConnected = null;
        }
    }
    
    private void OnWebSocketClose(WebSocketCloseCode code)
    {
        Debug.LogError($"[ClientSocket-WebSocket] 连接关闭: code={code}, url={webSocketUrl}, opened={webSocketOpened}");
        if (!webSocketOpened && onConnected != null)
        {
            onConnected(false);
            onConnected = null;
        }
        OnDisconnected?.Invoke();
    }
    
    public async void CloseConnect()
    {
        Debug.Log("[ClientSocket-WebSocket] ==== CloseConnect\n" + System.Environment.StackTrace);
        fragmentBytes = null;
        
        if (webSocket != null)
        {
            sendQueue.Clear();
            await webSocket.Close();
            webSocket = null;
        }
    }
    
    private bool isSending = false;
    
    private async void SendBytes(byte[] bytes)
    {
        // 调试日志:输出发送的字节数据
        // string hexString = "[WebSocket] SendBytes Length=" + bytes.Length + " Data=" + BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 32)).Replace("-", " ");
        // if (bytes.Length > 32) hexString += "...";
        // Debug.Log(hexString);
        
        if (webSocket == null || webSocket.State != WebSocketState.Open)
        {
            Debug.LogError("[ClientSocket-WebSocket] 未连接,无法发送");
            return;
        }
        
        // 队列机制
        lock (sendQueue)
        {
            if (isSending)
            {
                sendQueue.Enqueue(bytes);
                return;
            }
            isSending = true;
        }
        
        try
        {
            await webSocket.Send(bytes);
            
            // 处理队列中的下一个
            lock (sendQueue)
            {
                if (sendQueue.Count > 0)
                {
                    byte[] nextBytes = sendQueue.Dequeue();
                    isSending = false;
                    SendBytes(nextBytes); // 递归发送
                }
                else
                {
                    isSending = false;
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"[ClientSocket-WebSocket] 发送异常: {ex.Message}");
            lock (sendQueue)
            {
                isSending = false;
            }
        }
    }
    
    // WebGL需要在Update中轮询消息
    public void DispatchMessageQueue()
    {
#if UNITY_EDITOR
        webSocket?.DispatchMessageQueue();
#endif
    }
    
#endif
 
    // ==================== 统一的公共接口(两个平台通用)====================
    
    /// <summary>
    /// 发送协议包
    /// </summary>
    public void SendInfo(GameNetPackBasic protocol)
    {
        if (!connected)
        {
            return;
        }
 
        if (protocol == null)
        {
            Debug.LogError("要发的信息对象为空");
            return;
        }
 
        if (protocol.combineBytes == null)
        {
            protocol.WriteToBytes();
        }
        protocol.CombineDatas(encoder);
        
#if UNITY_EDITOR
        NetPkgCtl.RecordPackage(socketType, protocol.vInfoCont, NetPackagetType.Client, 
            protocol.ToString(), FieldPrint.PrintFields(protocol), FieldPrint.PrintFieldsExpand(protocol, true));
#endif
        
        // 调试日志:查看 combineBytes 的内容
        string hexString = "[SendInfo] Protocol=" + protocol.ToString() + " combineBytes.Length=" + protocol.combineBytes.Length + 
            " Data=" + BitConverter.ToString(protocol.combineBytes, 0, Math.Min(protocol.combineBytes.Length, 32)).Replace("-", " ");
        if (protocol.combineBytes.Length > 32) hexString += "...";
        Debug.Log(hexString);
        
        sendBytesTotal += protocol.combineBytes.Length;
        SendBytes(protocol.combineBytes);
    }
 
#if UNITY_EDITOR
    /// <summary>
    /// 发送原始字节数据
    /// </summary>
    public void SendInfo(byte[] vBytes)
    {
        if (!connected)
        {
            Debug.LogError("尚未与该后端链接!无法发送信息");
            return;
        }
 
        if (vBytes == null || vBytes.Length < 2)
        {
            Debug.LogError("要发的信息数据为空或数据不足");
            return;
        }
 
        // 加密并组装包头
        vBytes = encoder.BaseXorAdd(vBytes);
        byte[] vFrameHead = new byte[] { 255, 204 };
        byte[] vMsgBodyLength = BitConverter.GetBytes(vBytes.Length);
        byte[] vTotal = new byte[vBytes.Length + 6];
        Array.Copy(vFrameHead, 0, vTotal, 0, vFrameHead.Length);
        Array.Copy(vMsgBodyLength, 0, vTotal, 2, vMsgBodyLength.Length);
        Array.Copy(vBytes, 0, vTotal, 6, vBytes.Length);
 
        SendBytes(vTotal);
    }
#endif
}