Skip to content

Commit d846b27

Browse files
committed
Fix open/close events not called on main thread
1 parent 3911708 commit d846b27

19 files changed

+597
-2911
lines changed

Assets/MyWebSocketServer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ override public void OnClose(WebSocketConnection connection) {
2424
// Here is the same as OnOpen
2525
Debug.Log(connection.id);
2626
}
27+
28+
public void onConnectionOpened (WebSocketConnection connection) {
29+
Debug.Log("Connection opened: " + connection.id);
30+
}
2731

2832
public void onMessageReceived (WebSocketMessage message) {
2933
Debug.Log("Received new message: " + message.data);
3034
}
35+
36+
public void onConnectionClosed (WebSocketConnection connection) {
37+
Debug.Log("Connection closed: " + connection.id);
38+
}
3139
}

Assets/Scenes/SampleScene.unity

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,19 @@ MonoBehaviour:
239239
port: 8175
240240
onOpen:
241241
m_PersistentCalls:
242-
m_Calls: []
242+
m_Calls:
243+
- m_Target: {fileID: 944133942}
244+
m_TargetAssemblyTypeName: MyWebSocketServer, Assembly-CSharp
245+
m_MethodName: onConnectionOpened
246+
m_Mode: 0
247+
m_Arguments:
248+
m_ObjectArgument: {fileID: 0}
249+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
250+
m_IntArgument: 0
251+
m_FloatArgument: 0
252+
m_StringArgument:
253+
m_BoolArgument: 0
254+
m_CallState: 2
243255
onMessage:
244256
m_PersistentCalls:
245257
m_Calls:
@@ -257,7 +269,19 @@ MonoBehaviour:
257269
m_CallState: 2
258270
onClose:
259271
m_PersistentCalls:
260-
m_Calls: []
272+
m_Calls:
273+
- m_Target: {fileID: 944133942}
274+
m_TargetAssemblyTypeName: MyWebSocketServer, Assembly-CSharp
275+
m_MethodName: onConnectionClosed
276+
m_Mode: 0
277+
m_Arguments:
278+
m_ObjectArgument: {fileID: 0}
279+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
280+
m_IntArgument: 0
281+
m_FloatArgument: 0
282+
m_StringArgument:
283+
m_BoolArgument: 0
284+
m_CallState: 2
261285
--- !u!4 &944133943
262286
Transform:
263287
m_ObjectHideFlags: 0

Assets/WebSocketServer/WebSocketConnection.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace WebSocketServer {
1515

16+
public enum WebSocketEventType {
17+
Open,
18+
Close,
19+
Message
20+
}
21+
1622
public struct WebSocketMessage {
1723
public WebSocketMessage(WebSocketConnection connection, string data) {
1824
this.id = Guid.NewGuid().ToString();
@@ -25,6 +31,20 @@ public WebSocketMessage(WebSocketConnection connection, string data) {
2531
public string data { get; }
2632
}
2733

34+
public struct WebSocketEvent {
35+
public WebSocketEvent(WebSocketConnection connection, WebSocketEventType type, string data) {
36+
this.id = Guid.NewGuid().ToString();
37+
this.connection = connection;
38+
this.type = type;
39+
this.data = data;
40+
}
41+
42+
public string id { get; }
43+
public WebSocketEventType type { get; }
44+
public WebSocketConnection connection { get; }
45+
public string data { get; }
46+
}
47+
2848
public class WebSocketConnection {
2949

3050
public string id;
@@ -63,8 +83,8 @@ public bool Establish() {
6383
connectionHandler.Start();
6484

6585
// Call the server callback.
66-
server.onOpen.Invoke(this);
67-
server.OnOpen(this);
86+
WebSocketEvent wsEvent = new WebSocketEvent(this, WebSocketEventType.Open, null);
87+
server.events.Enqueue(wsEvent);
6888
return true;
6989
} else {
7090
return false;
@@ -79,17 +99,17 @@ private void HandleConnection () {
7999
if ((WebSocketOpCode)dataframe.opcode == WebSocketOpCode.Text) {
80100
// Let the server know of the message.
81101
string data = WebSocketProtocol.DecodeText(dataframe);
82-
WebSocketMessage message = new WebSocketMessage(this, data);
83-
server.messages.Enqueue(message);
102+
WebSocketEvent wsEvent = new WebSocketEvent(this, WebSocketEventType.Message, data);
103+
server.events.Enqueue(wsEvent);
84104
} else if ((WebSocketOpCode)dataframe.opcode == WebSocketOpCode.Close) {
85105
// Handle closing the connection.
86106
Debug.Log("Client closed the connection.");
87107
// Close the connection.
88108
stream.Close();
89109
client.Close();
90110
// Call server callback.
91-
server.onClose.Invoke(this);
92-
server.OnClose(this);
111+
WebSocketEvent wsEvent = new WebSocketEvent(this, WebSocketEventType.Close, null);
112+
server.events.Enqueue(wsEvent);
93113
// Jump out of the loop.
94114
break;
95115
}

Assets/WebSocketServer/WebSocketServer.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class WebSocketServer : MonoBehaviour
2929
private List<Thread> workerThreads;
3030
private TcpClient connectedTcpClient;
3131

32-
public ConcurrentQueue<WebSocketMessage> messages;
32+
public ConcurrentQueue<WebSocketEvent> events;
3333

3434
public string address;
3535
public int port;
@@ -42,7 +42,7 @@ void Awake() {
4242
}
4343

4444
void Start() {
45-
messages = new ConcurrentQueue<WebSocketMessage>();
45+
events = new ConcurrentQueue<WebSocketEvent>();
4646
workerThreads = new List<Thread>();
4747

4848
tcpListenerThread = new Thread (new ThreadStart(ListenForTcpConnection));
@@ -51,10 +51,19 @@ void Start() {
5151
}
5252

5353
void Update() {
54-
WebSocketMessage message;
55-
while (messages.TryDequeue(out message)) {
56-
onMessage.Invoke(message);
57-
this.OnMessage(message);
54+
WebSocketEvent wsEvent;
55+
while (events.TryDequeue(out wsEvent)) {
56+
if (wsEvent.type == WebSocketEventType.Open) {
57+
onOpen.Invoke(wsEvent.connection);
58+
this.OnOpen(wsEvent.connection);
59+
} else if (wsEvent.type == WebSocketEventType.Close) {
60+
onClose.Invoke(wsEvent.connection);
61+
this.OnClose(wsEvent.connection);
62+
} else if (wsEvent.type == WebSocketEventType.Message) {
63+
WebSocketMessage message = new WebSocketMessage(wsEvent.connection, wsEvent.data);
64+
onMessage.Invoke(message);
65+
this.OnMessage(message);
66+
}
5867
}
5968
}
6069

Logs/ApiUpdaterCheck.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,33 @@ C# parse time : 221ms
438438
candidates check time : 42ms
439439
console write time : 0ms
440440

441+
[api-updater (non-obsolete-error-filter)] 4/30/2022 8:20:48 PM : Starting /Applications/Unity/Hub/Editor/2020.3.16f1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
442+
[api-updater (non-obsolete-error-filter)]
443+
----------------------------------
444+
jit/startup time : 1426.16ms
445+
moved types parse time: 148ms
446+
candidates parse time : 5ms
447+
C# parse time : 472ms
448+
candidates check time : 157ms
449+
console write time : 1ms
450+
451+
[api-updater (non-obsolete-error-filter)] 4/30/2022 8:25:44 PM : Starting /Applications/Unity/Hub/Editor/2020.3.16f1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
452+
[api-updater (non-obsolete-error-filter)]
453+
----------------------------------
454+
jit/startup time : 155.097ms
455+
moved types parse time: 156ms
456+
candidates parse time : 6ms
457+
C# parse time : 540ms
458+
candidates check time : 131ms
459+
console write time : 1ms
460+
461+
[api-updater (non-obsolete-error-filter)] 4/30/2022 8:26:40 PM : Starting /Applications/Unity/Hub/Editor/2020.3.16f1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
462+
[api-updater (non-obsolete-error-filter)]
463+
----------------------------------
464+
jit/startup time : 157.439ms
465+
moved types parse time: 160ms
466+
candidates parse time : 7ms
467+
C# parse time : 511ms
468+
candidates check time : 120ms
469+
console write time : 1ms
470+

0 commit comments

Comments
 (0)