Skip to content

Commit 75ac399

Browse files
author
Gaeel Bradshaw
committed
Adds optional subprotocol
1 parent de70d56 commit 75ac399

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

NativeWebSocket/Assets/WebSocket/WebSocket.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,34 @@ public WebSocket (string url, Dictionary<string, string> headers = null) {
240240
this.instanceId = instanceId;
241241
}
242242

243+
public WebSocket (string url, string subprotocol, Dictionary<string, string> headers = null) {
244+
if (!WebSocketFactory.isInitialized) {
245+
WebSocketFactory.Initialize ();
246+
}
247+
248+
int instanceId = WebSocketFactory.WebSocketAllocate (url);
249+
WebSocketFactory.instances.Add (instanceId, this);
250+
251+
WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol);
252+
253+
this.instanceId = instanceId;
254+
}
255+
256+
public WebSocket (string url, List<string> subprotocols, Dictionary<string, string> headers = null) {
257+
if (!WebSocketFactory.isInitialized) {
258+
WebSocketFactory.Initialize ();
259+
}
260+
261+
int instanceId = WebSocketFactory.WebSocketAllocate (url);
262+
WebSocketFactory.instances.Add (instanceId, this);
263+
264+
foreach (string subprotocol in subprotocols) {
265+
WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol);
266+
}
267+
268+
this.instanceId = instanceId;
269+
}
270+
243271
~WebSocket () {
244272
WebSocketFactory.HandleInstanceDestroy (this.instanceId);
245273
}
@@ -344,6 +372,7 @@ public class WebSocket : IWebSocket
344372

345373
private Uri uri;
346374
private Dictionary<string, string> headers;
375+
private List<string> subprotocols;
347376
private ClientWebSocket m_Socket = new ClientWebSocket();
348377

349378
private CancellationTokenSource m_TokenSource;
@@ -368,6 +397,48 @@ public WebSocket(string url, Dictionary<string, string> headers = null)
368397
this.headers = headers;
369398
}
370399

400+
subprotocols = new List<string>();
401+
402+
string protocol = uri.Scheme;
403+
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
404+
throw new ArgumentException("Unsupported protocol: " + protocol);
405+
}
406+
407+
public WebSocket(string url, string subprotocol, Dictionary<string, string> headers = null)
408+
{
409+
uri = new Uri(url);
410+
411+
if (headers == null)
412+
{
413+
this.headers = new Dictionary<string, string>();
414+
}
415+
else
416+
{
417+
this.headers = headers;
418+
}
419+
420+
subprotocols = new List<string> {subprotocol};
421+
422+
string protocol = uri.Scheme;
423+
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
424+
throw new ArgumentException("Unsupported protocol: " + protocol);
425+
}
426+
427+
public WebSocket(string url, List<string> subprotocols, Dictionary<string, string> headers = null)
428+
{
429+
uri = new Uri(url);
430+
431+
if (headers == null)
432+
{
433+
this.headers = new Dictionary<string, string>();
434+
}
435+
else
436+
{
437+
this.headers = headers;
438+
}
439+
440+
this.subprotocols = subprotocols;
441+
371442
string protocol = uri.Scheme;
372443
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
373444
throw new ArgumentException("Unsupported protocol: " + protocol);
@@ -392,6 +463,10 @@ public async Task Connect()
392463
m_Socket.Options.SetRequestHeader(header.Key, header.Value);
393464
}
394465

466+
foreach (string subprotocol in subprotocols) {
467+
m_Socket.Options.AddSubProtocol(subprotocol);
468+
}
469+
395470
await m_Socket.ConnectAsync(uri, m_CancellationToken);
396471
OnOpen?.Invoke();
397472

@@ -652,6 +727,9 @@ public static class WebSocketFactory
652727
[DllImport ("__Internal")]
653728
public static extern int WebSocketAllocate (string url);
654729

730+
[DllImport ("__Internal")]
731+
public static extern int WebSocketAddSubProtocol (int instanceId, string subprotocol);
732+
655733
[DllImport ("__Internal")]
656734
public static extern void WebSocketFree (int instanceId);
657735

NativeWebSocket/Assets/WebSocket/WebSocket.jslib

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ var LibraryWebSocket = {
142142
var id = webSocketState.lastId++;
143143

144144
webSocketState.instances[id] = {
145+
subprotocols: [],
145146
url: urlStr,
146147
ws: null
147148
};
@@ -150,6 +151,19 @@ var LibraryWebSocket = {
150151

151152
},
152153

154+
/**
155+
* Add subprotocol to instance
156+
*
157+
* @param instanceId Instance ID
158+
* @param subprotocol Subprotocol name to add to instance
159+
*/
160+
WebSocketAddSubProtocol: function(instanceId, subprotocol) {
161+
162+
var subprotocolStr = Pointer_stringify(subprotocol);
163+
webSocketState.instances[instanceId].subprotocols.push(subprotocolStr);
164+
165+
},
166+
153167
/**
154168
* Remove reference to WebSocket instance
155169
*
@@ -188,7 +202,7 @@ var LibraryWebSocket = {
188202
if (instance.ws !== null)
189203
return -2;
190204

191-
instance.ws = new WebSocket(instance.url);
205+
instance.ws = new WebSocket(instance.url, instance.subprotocols);
192206

193207
instance.ws.binaryType = 'arraybuffer';
194208

0 commit comments

Comments
 (0)