Skip to content

Commit a72a788

Browse files
committed
Support for multiple clients. TODO: Handle disconnect.
1 parent 057f6e3 commit a72a788

File tree

5 files changed

+154
-115
lines changed

5 files changed

+154
-115
lines changed

Assets/WebSocketServer.cs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System.Text.RegularExpressions;
88
// For creating a thread
99
using System.Threading;
10+
// For List & ConcurrentQueue
11+
using System.Collections.Generic;
12+
using System.Collections.Concurrent;
1013
// Unity & Unity events
1114
using UnityEngine;
1215
using UnityEngine.Events;
@@ -15,14 +18,28 @@ namespace WebSocketServer {
1518
[System.Serializable]
1619
public class StringEvent : UnityEvent<string> {}
1720

21+
public struct WebSocketConnection {
22+
public WebSocketConnection(TcpClient client, NetworkStream stream, ConcurrentQueue<string> queue)
23+
{
24+
this.client = client;
25+
this.stream = stream;
26+
this.queue = queue;
27+
}
28+
29+
public TcpClient client { get; }
30+
public NetworkStream stream { get; }
31+
public ConcurrentQueue<string> queue { get; }
32+
}
33+
1834
public class WebSocketServer : MonoBehaviour
1935
{
36+
// The tcpListenerThread listens for incoming WebSocket connections, then assigns the client to handler threads;
2037
private TcpListener tcpListener;
2138
private Thread tcpListenerThread;
39+
private List<Thread> workerThreads;
2240
private TcpClient connectedTcpClient;
2341

24-
private bool hasNewMessage = false;
25-
private string message = "";
42+
private ConcurrentQueue<string> messages;
2643

2744
public string address;
2845
public int port;
@@ -34,50 +51,51 @@ void Awake() {
3451

3552
void Start()
3653
{
37-
tcpListenerThread = new Thread (new ThreadStart(WebSocketServerThread));
54+
messages = new ConcurrentQueue<string>();
55+
workerThreads = new List<Thread>();
56+
57+
tcpListenerThread = new Thread (new ThreadStart(ListenForTcpConnection));
3858
tcpListenerThread.IsBackground = true;
3959
tcpListenerThread.Start();
4060
}
4161

4262
void Update()
4363
{
44-
if (hasNewMessage) {
64+
string message;
65+
while (messages.TryDequeue(out message)) {
4566
onMessage.Invoke(message);
46-
hasNewMessage = false;
4767
}
4868
}
4969

50-
private void WebSocketServerThread () {
70+
private void ListenForTcpConnection () {
5171
try {
5272
// Create listener on <address>:<port>.
5373
tcpListener = new TcpListener(IPAddress.Parse(address), port);
5474
tcpListener.Start();
5575
Debug.Log("WebSocket server is listening for incoming connections.");
5676
while (true) {
57-
using (connectedTcpClient = tcpListener.AcceptTcpClient()) {
58-
// Get a stream object for reading
59-
using (NetworkStream stream = connectedTcpClient.GetStream()) {
60-
EstablishConnection(connectedTcpClient, stream);
61-
while (true) {
62-
message = ReceiveMessage(connectedTcpClient, stream);
63-
hasNewMessage = true;
64-
}
65-
}
66-
}
77+
connectedTcpClient = tcpListener.AcceptTcpClient();
78+
NetworkStream stream = connectedTcpClient.GetStream();
79+
WebSocketConnection connection = new WebSocketConnection(connectedTcpClient, stream, messages);
80+
EstablishConnection(connection);
81+
Thread worker = new Thread (new ParameterizedThreadStart(HandleConnection));
82+
worker.IsBackground = true;
83+
worker.Start(connection);
84+
workerThreads.Add(worker);
6785
}
6886
}
6987
catch (SocketException socketException) {
7088
Debug.Log("SocketException " + socketException.ToString());
7189
}
7290
}
7391

74-
private void EstablishConnection (TcpClient client, NetworkStream stream) {
92+
private void EstablishConnection (WebSocketConnection connection) {
7593
// Wait for enough bytes to be available
76-
while (!stream.DataAvailable);
77-
while(client.Available < 3);
94+
while (!connection.stream.DataAvailable);
95+
while(connection.client.Available < 3);
7896
// Translate bytes of request to string
79-
Byte[] bytes = new Byte[client.Available];
80-
stream.Read(bytes, 0, bytes.Length);
97+
Byte[] bytes = new Byte[connection.client.Available];
98+
connection.stream.Read(bytes, 0, bytes.Length);
8199
String data = Encoding.UTF8.GetString(bytes);
82100

83101
// Check if the input has a "GET" header. If so, initiate the connection.
@@ -96,11 +114,19 @@ private void EstablishConnection (TcpClient client, NetworkStream stream) {
96114
) + eol
97115
+ eol);
98116

99-
stream.Write(response, 0, response.Length);
117+
connection.stream.Write(response, 0, response.Length);
100118
Debug.Log("WebSocket client connected.");
101119
}
102120
}
103121

122+
private void HandleConnection (object parameter) {
123+
WebSocketConnection connection = (WebSocketConnection)parameter;
124+
while (true) {
125+
string message = ReceiveMessage(connection.client, connection.stream);
126+
connection.queue.Enqueue(message);
127+
}
128+
}
129+
104130
private string ReceiveMessage(TcpClient client, NetworkStream stream) {
105131
// Wait for data to be available, then read the data.
106132
while (!stream.DataAvailable);

Logs/ApiUpdaterCheck.txt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,93 @@ C# parse time : 269ms
4848
candidates check time : 38ms
4949
console write time : 0ms
5050

51+
[api-updater (non-obsolete-error-filter)] 6/14/2021 4:26:34 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
52+
[api-updater (non-obsolete-error-filter)]
53+
----------------------------------
54+
jit/startup time : 422.141ms
55+
moved types parse time: 59ms
56+
candidates parse time : 1ms
57+
C# parse time : 289ms
58+
candidates check time : 52ms
59+
console write time : 1ms
60+
61+
[api-updater (non-obsolete-error-filter)] 6/14/2021 4:58:28 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
62+
[api-updater (non-obsolete-error-filter)]
63+
----------------------------------
64+
jit/startup time : 120.863ms
65+
moved types parse time: 54ms
66+
candidates parse time : 1ms
67+
C# parse time : 280ms
68+
candidates check time : 53ms
69+
console write time : 1ms
70+
71+
[api-updater (non-obsolete-error-filter)] 6/14/2021 4:58:52 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
72+
[api-updater (non-obsolete-error-filter)]
73+
----------------------------------
74+
jit/startup time : 113.606ms
75+
moved types parse time: 52ms
76+
candidates parse time : 1ms
77+
C# parse time : 285ms
78+
candidates check time : 52ms
79+
console write time : 0ms
80+
81+
[api-updater (non-obsolete-error-filter)] 6/14/2021 4:59:05 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
82+
[api-updater (non-obsolete-error-filter)]
83+
----------------------------------
84+
jit/startup time : 57.943ms
85+
moved types parse time: 46ms
86+
candidates parse time : 1ms
87+
C# parse time : 222ms
88+
candidates check time : 44ms
89+
console write time : 0ms
90+
91+
[api-updater (non-obsolete-error-filter)] 6/14/2021 5:02:05 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
92+
[api-updater (non-obsolete-error-filter)]
93+
----------------------------------
94+
jit/startup time : 129.724ms
95+
moved types parse time: 53ms
96+
candidates parse time : 1ms
97+
C# parse time : 296ms
98+
candidates check time : 50ms
99+
console write time : 0ms
100+
101+
[api-updater (non-obsolete-error-filter)] 6/14/2021 5:17:24 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
102+
[api-updater (non-obsolete-error-filter)]
103+
----------------------------------
104+
jit/startup time : 107.62ms
105+
moved types parse time: 51ms
106+
candidates parse time : 1ms
107+
C# parse time : 327ms
108+
candidates check time : 48ms
109+
console write time : 1ms
110+
111+
[api-updater (non-obsolete-error-filter)] 6/14/2021 5:18:17 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
112+
[api-updater (non-obsolete-error-filter)]
113+
----------------------------------
114+
jit/startup time : 48.328ms
115+
moved types parse time: 47ms
116+
candidates parse time : 1ms
117+
C# parse time : 239ms
118+
candidates check time : 50ms
119+
console write time : 0ms
120+
121+
[api-updater (non-obsolete-error-filter)] 6/14/2021 5:18:35 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
122+
[api-updater (non-obsolete-error-filter)]
123+
----------------------------------
124+
jit/startup time : 47.651ms
125+
moved types parse time: 48ms
126+
candidates parse time : 1ms
127+
C# parse time : 233ms
128+
candidates check time : 42ms
129+
console write time : 0ms
130+
131+
[api-updater (non-obsolete-error-filter)] 6/14/2021 5:19:39 PM : Starting /Applications/Unity/2020.3.5f1c1/Unity.app/Contents/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe
132+
[api-updater (non-obsolete-error-filter)]
133+
----------------------------------
134+
jit/startup time : 104.064ms
135+
moved types parse time: 50ms
136+
candidates parse time : 1ms
137+
C# parse time : 263ms
138+
candidates check time : 46ms
139+
console write time : 1ms
140+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,19 @@ System memory in use after: 154.7 MB.
740740
Unloading 12 unused Assets to reduce memory usage. Loaded Objects now: 2384.
741741
Total: 2.573800 ms (FindLiveObjects: 0.244247 ms CreateObjectMapping: 0.064546 ms MarkObjects: 2.104607 ms DeleteObjects: 0.158731 ms)
742742

743+
========================================================================
744+
Received Import Request.
745+
Time since last request: 4669.823015 seconds.
746+
path: Assets/WebSocketServer.cs
747+
artifactKey: Guid(4ac217db4dad5436d9927ea08182cd9b) Importer(815301076,1909f56bfc062723c751e8b465ee728b)
748+
Start importing Assets/WebSocketServer.cs using Guid(4ac217db4dad5436d9927ea08182cd9b) Importer(815301076,1909f56bfc062723c751e8b465ee728b) -> (artifact id: 'f6c82d612f9f55c45f81556965d9af53') in 0.012955 seconds
749+
Import took 0.017233 seconds .
750+
751+
========================================================================
752+
Received Import Request.
753+
Time since last request: 0.028106 seconds.
754+
path: Assets/WebSocketServer.cs
755+
artifactKey: Guid(4ac217db4dad5436d9927ea08182cd9b) Importer(815301076,1909f56bfc062723c751e8b465ee728b)
756+
Start importing Assets/WebSocketServer.cs using Guid(4ac217db4dad5436d9927ea08182cd9b) Importer(815301076,1909f56bfc062723c751e8b465ee728b) -> (artifact id: 'f6c82d612f9f55c45f81556965d9af53') in 0.001711 seconds
757+
Import took 0.005219 seconds .
758+

Logs/shadercompiler-AssetImportWorker0.log

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)