Skip to content

Commit 90326e4

Browse files
committed
Merge branch 'master' into dev
2 parents ec3b136 + 30c9b7d commit 90326e4

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

Assets/Plugins/WebSocket/WebSocket.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,75 @@
99

1010
using AOT;
1111
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+
}
1281

1382
namespace NativeWebSocket
1483
{
@@ -490,6 +559,7 @@ public void DispatchMessageQueue()
490559

491560
public async Task Receive()
492561
{
562+
WebSocketCloseCode closeCode = WebSocketCloseCode.Abnormal;
493563
await new WaitForBackgroundThread();
494564

495565
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[8192]);
@@ -531,7 +601,7 @@ public async Task Receive()
531601
else if (result.MessageType == WebSocketMessageType.Close)
532602
{
533603
await Close();
534-
OnClose?.Invoke(WebSocketHelpers.ParseCloseCodeEnum((int)result.CloseStatus));
604+
closeCode = WebSocketHelpers.ParseCloseCodeEnum((int)result.CloseStatus);
535605
break;
536606
}
537607
}
@@ -540,7 +610,11 @@ public async Task Receive()
540610
catch (Exception)
541611
{
542612
m_TokenSource.Cancel();
543-
OnClose?.Invoke(WebSocketCloseCode.Abnormal);
613+
}
614+
finally
615+
{
616+
await new WaitForUpdate();
617+
OnClose?.Invoke(closeCode);
544618
}
545619
}
546620

0 commit comments

Comments
 (0)