9
9
10
10
using AOT ;
11
11
using System . Runtime . InteropServices ;
12
+ using UnityEngine ;
13
+ using System . Collections ;
14
+
15
+ public class MainThreadUtil : MonoBehaviour
16
+ {
17
+ public static MainThreadUtil Instance { get ; private set ; }
18
+ public static SynchronizationContext synchronizationContext { get ; private set ; }
19
+
20
+ [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
21
+ public static void Setup ( )
22
+ {
23
+ Instance = new GameObject ( "MainThreadUtil" )
24
+ . AddComponent < MainThreadUtil > ( ) ;
25
+ synchronizationContext = SynchronizationContext . Current ;
26
+ }
27
+
28
+ public static void Run ( IEnumerator waitForUpdate )
29
+ {
30
+ synchronizationContext . Post ( _ => Instance . StartCoroutine (
31
+ waitForUpdate ) , null ) ;
32
+ }
33
+
34
+ void Awake ( )
35
+ {
36
+ gameObject . hideFlags = HideFlags . HideAndDontSave ;
37
+ DontDestroyOnLoad ( gameObject ) ;
38
+ }
39
+ }
40
+
41
+ public class WaitForUpdate : CustomYieldInstruction
42
+ {
43
+ public override bool keepWaiting
44
+ {
45
+ get { return false ; }
46
+ }
47
+
48
+ public MainThreadAwaiter GetAwaiter ( )
49
+ {
50
+ var awaiter = new MainThreadAwaiter ( ) ;
51
+ MainThreadUtil . Run ( CoroutineWrapper ( this , awaiter ) ) ;
52
+ return awaiter ;
53
+ }
54
+
55
+ public class MainThreadAwaiter : INotifyCompletion
56
+ {
57
+ Action continuation ;
58
+
59
+ public bool IsCompleted { get ; set ; }
60
+
61
+ public void GetResult ( ) { }
62
+
63
+ public void Complete ( )
64
+ {
65
+ IsCompleted = true ;
66
+ continuation ? . Invoke ( ) ;
67
+ }
68
+
69
+ void INotifyCompletion . OnCompleted ( Action continuation )
70
+ {
71
+ this . continuation = continuation ;
72
+ }
73
+ }
74
+
75
+ public static IEnumerator CoroutineWrapper ( IEnumerator theWorker , MainThreadAwaiter awaiter )
76
+ {
77
+ yield return theWorker ;
78
+ awaiter . Complete ( ) ;
79
+ }
80
+ }
12
81
13
82
namespace NativeWebSocket
14
83
{
@@ -490,6 +559,7 @@ public void DispatchMessageQueue()
490
559
491
560
public async Task Receive ( )
492
561
{
562
+ WebSocketCloseCode closeCode = WebSocketCloseCode . Abnormal ;
493
563
await new WaitForBackgroundThread ( ) ;
494
564
495
565
ArraySegment < byte > buffer = new ArraySegment < byte > ( new byte [ 8192 ] ) ;
@@ -531,7 +601,7 @@ public async Task Receive()
531
601
else if ( result . MessageType == WebSocketMessageType . Close )
532
602
{
533
603
await Close ( ) ;
534
- OnClose ? . Invoke ( WebSocketHelpers . ParseCloseCodeEnum ( ( int ) result . CloseStatus ) ) ;
604
+ closeCode = WebSocketHelpers . ParseCloseCodeEnum ( ( int ) result . CloseStatus ) ;
535
605
break ;
536
606
}
537
607
}
@@ -540,7 +610,11 @@ public async Task Receive()
540
610
catch ( Exception )
541
611
{
542
612
m_TokenSource . Cancel ( ) ;
543
- OnClose ? . Invoke ( WebSocketCloseCode . Abnormal ) ;
613
+ }
614
+ finally
615
+ {
616
+ await new WaitForUpdate ( ) ;
617
+ OnClose ? . Invoke ( closeCode ) ;
544
618
}
545
619
}
546
620
0 commit comments