@@ -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
0 commit comments