Skip to content

Commit 054746e

Browse files
committed
Add UnityEvent support
1 parent a5eea07 commit 054746e

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Assets/WebSocketServer/WebSocketConnection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public bool Establish() {
6363
connectionHandler.Start();
6464

6565
// Call the server callback.
66+
server.onOpen.Invoke(this);
6667
server.OnOpen(this);
6768
return true;
6869
} else {
@@ -81,11 +82,15 @@ private void HandleConnection () {
8182
WebSocketMessage message = new WebSocketMessage(this, data);
8283
server.messages.Enqueue(message);
8384
} else if ((WebSocketOpCode)dataframe.opcode == WebSocketOpCode.Close) {
84-
// Handle closing the connection
85+
// Handle closing the connection.
8586
Debug.Log("Client closed the connection.");
87+
// Close the connection.
8688
stream.Close();
8789
client.Close();
90+
// Call server callback.
91+
server.onClose.Invoke(this);
8892
server.OnClose(this);
93+
// Jump out of the loop.
8994
break;
9095
}
9196
} else {

Assets/WebSocketServer/WebSocketServer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
using UnityEngine.Events;
1313

1414
namespace WebSocketServer {
15+
[System.Serializable]
16+
public class WebSocketOpenEvent : UnityEvent<WebSocketConnection> {}
17+
1518
[System.Serializable]
1619
public class WebSocketMessageEvent : UnityEvent<WebSocketMessage> {}
1720

21+
[System.Serializable]
22+
public class WebSocketCloseEvent : UnityEvent<WebSocketConnection> {}
23+
1824
public class WebSocketServer : MonoBehaviour
1925
{
2026
// The tcpListenerThread listens for incoming WebSocket connections, then assigns the client to handler threads;
@@ -27,7 +33,9 @@ public class WebSocketServer : MonoBehaviour
2733

2834
public string address;
2935
public int port;
36+
public WebSocketOpenEvent onOpen;
3037
public WebSocketMessageEvent onMessage;
38+
public WebSocketCloseEvent onClose;
3139

3240
void Awake() {
3341
if (onMessage == null) onMessage = new WebSocketMessageEvent();

Logs/AssetImportWorker0.log

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,3 +2012,63 @@ System memory in use after: 167.5 MB.
20122012
Unloading 12 unused Assets to reduce memory usage. Loaded Objects now: 2634.
20132013
Total: 3.696991 ms (FindLiveObjects: 0.321621 ms CreateObjectMapping: 0.143795 ms MarkObjects: 3.090181 ms DeleteObjects: 0.138315 ms)
20142014

2015+
========================================================================
2016+
Received Prepare
2017+
Registering precompiled user dll's ...
2018+
Registered in 0.000589 seconds.
2019+
Begin MonoManager ReloadAssembly
2020+
Native extension for OSXStandalone target not found
2021+
Refreshing native plugins compatible for Editor in 0.41 ms, found 2 plugins.
2022+
Preloading 0 native plugins for Editor in 0.00 ms.
2023+
Mono: successfully reloaded assembly
2024+
- Completed reload, in 1.054 seconds
2025+
Platform modules already initialized, skipping
2026+
Refreshing native plugins compatible for Editor in 0.39 ms, found 2 plugins.
2027+
Preloading 0 native plugins for Editor in 0.00 ms.
2028+
Unloading 1807 Unused Serialized files (Serialized files now loaded: 0)
2029+
System memory in use before: 167.7 MB.
2030+
System memory in use after: 167.7 MB.
2031+
2032+
Unloading 12 unused Assets to reduce memory usage. Loaded Objects now: 2638.
2033+
Total: 2.408146 ms (FindLiveObjects: 0.300355 ms CreateObjectMapping: 0.071596 ms MarkObjects: 1.920714 ms DeleteObjects: 0.114462 ms)
2034+
2035+
========================================================================
2036+
Received Prepare
2037+
Registering precompiled user dll's ...
2038+
Registered in 0.000777 seconds.
2039+
Begin MonoManager ReloadAssembly
2040+
Native extension for OSXStandalone target not found
2041+
Refreshing native plugins compatible for Editor in 0.36 ms, found 2 plugins.
2042+
Preloading 0 native plugins for Editor in 0.00 ms.
2043+
Mono: successfully reloaded assembly
2044+
- Completed reload, in 1.051 seconds
2045+
Platform modules already initialized, skipping
2046+
Refreshing native plugins compatible for Editor in 0.36 ms, found 2 plugins.
2047+
Preloading 0 native plugins for Editor in 0.00 ms.
2048+
Unloading 1807 Unused Serialized files (Serialized files now loaded: 0)
2049+
System memory in use before: 167.9 MB.
2050+
System memory in use after: 167.9 MB.
2051+
2052+
Unloading 12 unused Assets to reduce memory usage. Loaded Objects now: 2642.
2053+
Total: 2.445527 ms (FindLiveObjects: 0.291766 ms CreateObjectMapping: 0.061581 ms MarkObjects: 1.980272 ms DeleteObjects: 0.110776 ms)
2054+
2055+
========================================================================
2056+
Received Prepare
2057+
Registering precompiled user dll's ...
2058+
Registered in 0.002262 seconds.
2059+
Begin MonoManager ReloadAssembly
2060+
Native extension for OSXStandalone target not found
2061+
Refreshing native plugins compatible for Editor in 0.36 ms, found 2 plugins.
2062+
Preloading 0 native plugins for Editor in 0.00 ms.
2063+
Mono: successfully reloaded assembly
2064+
- Completed reload, in 1.178 seconds
2065+
Platform modules already initialized, skipping
2066+
Refreshing native plugins compatible for Editor in 0.53 ms, found 2 plugins.
2067+
Preloading 0 native plugins for Editor in 0.00 ms.
2068+
Unloading 1807 Unused Serialized files (Serialized files now loaded: 0)
2069+
System memory in use before: 168.1 MB.
2070+
System memory in use after: 168.1 MB.
2071+
2072+
Unloading 12 unused Assets to reduce memory usage. Loaded Objects now: 2646.
2073+
Total: 3.040585 ms (FindLiveObjects: 0.363364 ms CreateObjectMapping: 0.067130 ms MarkObjects: 2.459490 ms DeleteObjects: 0.149128 ms)
2074+

0 commit comments

Comments
 (0)