From cc2fc6a0366f214af3bcf290ed385d620fdecc14 Mon Sep 17 00:00:00 2001 From: Selzler Date: Sun, 13 Mar 2022 01:32:17 -0300 Subject: [PATCH 1/2] Added Options for WebSocket. --- .../Samples~/WebSocketExample/Connection.cs | 10 +++ NativeWebSocket/Assets/WebSocket/WebSocket.cs | 73 +++++++------------ 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs b/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs index 32acbd8..8fdc8fc 100644 --- a/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs +++ b/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs @@ -12,6 +12,16 @@ public class Connection : MonoBehaviour // Start is called before the first frame update async void Start() { + //WebSocketOptions example. + //WebSocketOptions options = new WebSocketOptions(); + //options.uri = new Uri("ws://localhost:3000"); + //options.keepAliveInterval = TimeSpan.FromSeconds(1); + //headers... + //subprotocols... + //Credentials... + + //websocket = new WebSocket(options); + // websocket = new WebSocket("ws://echo.websocket.org"); websocket = new WebSocket("ws://localhost:3000"); diff --git a/NativeWebSocket/Assets/WebSocket/WebSocket.cs b/NativeWebSocket/Assets/WebSocket/WebSocket.cs index 503cf43..df07e1d 100644 --- a/NativeWebSocket/Assets/WebSocket/WebSocket.cs +++ b/NativeWebSocket/Assets/WebSocket/WebSocket.cs @@ -363,6 +363,15 @@ public void DelegateOnCloseEvent (int closeCode) { #else + public class WebSocketOptions + { + public Uri uri = new Uri("ws://localhost:3000"); + public Dictionary headers = new Dictionary(); + public List subprotocols = new List(); + public TimeSpan keepAliveInterval = TimeSpan.FromSeconds(30); //Ping interval + //Credentials... + } + public class WebSocket : IWebSocket { public event WebSocketOpenEventHandler OnOpen; @@ -373,6 +382,8 @@ public class WebSocket : IWebSocket private Uri uri; private Dictionary headers; private List subprotocols; + private TimeSpan keepAliveInterval; + //Credentials... private ClientWebSocket m_Socket = new ClientWebSocket(); private CancellationTokenSource m_TokenSource; @@ -385,60 +396,25 @@ public class WebSocket : IWebSocket private List> sendBytesQueue = new List>(); private List> sendTextQueue = new List>(); - public WebSocket(string url, Dictionary headers = null) + public WebSocket(string url) { - uri = new Uri(url); - - if (headers == null) - { - this.headers = new Dictionary(); - } - else - { - this.headers = headers; - } - - subprotocols = new List(); - - string protocol = uri.Scheme; - if (!protocol.Equals("ws") && !protocol.Equals("wss")) - throw new ArgumentException("Unsupported protocol: " + protocol); + WebSocketOptions options = new WebSocketOptions(); + options.uri = new Uri(url); + setWebSocketOptions(options); } - public WebSocket(string url, string subprotocol, Dictionary headers = null) + public WebSocket(WebSocketOptions options) { - uri = new Uri(url); - - if (headers == null) - { - this.headers = new Dictionary(); - } - else - { - this.headers = headers; - } - - subprotocols = new List {subprotocol}; - - string protocol = uri.Scheme; - if (!protocol.Equals("ws") && !protocol.Equals("wss")) - throw new ArgumentException("Unsupported protocol: " + protocol); + setWebSocketOptions(options); } - public WebSocket(string url, List subprotocols, Dictionary headers = null) + public void setWebSocketOptions(WebSocketOptions options) { - uri = new Uri(url); - - if (headers == null) - { - this.headers = new Dictionary(); - } - else - { - this.headers = headers; - } - - this.subprotocols = subprotocols; + uri = options.uri; + headers = options.headers; + subprotocols = options.subprotocols; + keepAliveInterval = options.keepAliveInterval; + //Credentials... string protocol = uri.Scheme; if (!protocol.Equals("ws") && !protocol.Equals("wss")) @@ -468,6 +444,9 @@ public async Task Connect() m_Socket.Options.AddSubProtocol(subprotocol); } + m_Socket.Options.KeepAliveInterval = keepAliveInterval; + //m_Socket.Options.Credentials... + await m_Socket.ConnectAsync(uri, m_CancellationToken); OnOpen?.Invoke(); From e5f7318dcb09661d9c460bae04fc7afcdd49380d Mon Sep 17 00:00:00 2001 From: Selzler Date: Sun, 13 Mar 2022 03:58:22 -0300 Subject: [PATCH 2/2] Add WebSocketOptions Webgl support. --- .../Samples~/WebSocketExample/Connection.cs | 4 +- NativeWebSocket/Assets/WebSocket/WebSocket.cs | 72 +++++++++---------- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs b/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs index 8fdc8fc..a21157c 100644 --- a/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs +++ b/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs @@ -14,8 +14,8 @@ async void Start() { //WebSocketOptions example. //WebSocketOptions options = new WebSocketOptions(); - //options.uri = new Uri("ws://localhost:3000"); - //options.keepAliveInterval = TimeSpan.FromSeconds(1); + //options.url = "ws://localhost:3000"; + //options.keepAliveInterval = TimeSpan.FromSeconds(10); //Ping Interval. //headers... //subprotocols... //Credentials... diff --git a/NativeWebSocket/Assets/WebSocket/WebSocket.cs b/NativeWebSocket/Assets/WebSocket/WebSocket.cs index df07e1d..bb4d962 100644 --- a/NativeWebSocket/Assets/WebSocket/WebSocket.cs +++ b/NativeWebSocket/Assets/WebSocket/WebSocket.cs @@ -199,6 +199,15 @@ public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter() } } + public class WebSocketOptions + { + public string url = "ws://localhost:3000"; + public Dictionary headers = new Dictionary(); + public List subprotocols = new List(); + public TimeSpan keepAliveInterval = TimeSpan.FromSeconds(30); //Ping interval + //Credentials... + } + #if UNITY_WEBGL && !UNITY_EDITOR /// @@ -229,43 +238,35 @@ public class WebSocket : IWebSocket { public event WebSocketErrorEventHandler OnError; public event WebSocketCloseEventHandler OnClose; - public WebSocket (string url, Dictionary headers = null) { - if (!WebSocketFactory.isInitialized) { - WebSocketFactory.Initialize (); - } - - int instanceId = WebSocketFactory.WebSocketAllocate (url); - WebSocketFactory.instances.Add (instanceId, this); - - this.instanceId = instanceId; + public WebSocket(string url) + { + WebSocketOptions options = new WebSocketOptions(); + options.url = url; + setWebSocketOptions(options); } - public WebSocket (string url, string subprotocol, Dictionary headers = null) { - if (!WebSocketFactory.isInitialized) { - WebSocketFactory.Initialize (); - } - - int instanceId = WebSocketFactory.WebSocketAllocate (url); - WebSocketFactory.instances.Add (instanceId, this); - - WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol); - - this.instanceId = instanceId; + public WebSocket(WebSocketOptions options) + { + setWebSocketOptions(options); } - public WebSocket (string url, List subprotocols, Dictionary headers = null) { - if (!WebSocketFactory.isInitialized) { + public void setWebSocketOptions(WebSocketOptions options) + { + if (!WebSocketFactory.isInitialized) { WebSocketFactory.Initialize (); - } + } - int instanceId = WebSocketFactory.WebSocketAllocate (url); - WebSocketFactory.instances.Add (instanceId, this); + int instanceId = WebSocketFactory.WebSocketAllocate (options.url); + WebSocketFactory.instances.Add (instanceId, this); - foreach (string subprotocol in subprotocols) { - WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol); - } + foreach (string subprotocol in options.subprotocols) { + WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol); + } + + //headers????? + //creditentials... - this.instanceId = instanceId; + this.instanceId = instanceId; } ~WebSocket () { @@ -363,15 +364,6 @@ public void DelegateOnCloseEvent (int closeCode) { #else - public class WebSocketOptions - { - public Uri uri = new Uri("ws://localhost:3000"); - public Dictionary headers = new Dictionary(); - public List subprotocols = new List(); - public TimeSpan keepAliveInterval = TimeSpan.FromSeconds(30); //Ping interval - //Credentials... - } - public class WebSocket : IWebSocket { public event WebSocketOpenEventHandler OnOpen; @@ -399,7 +391,7 @@ public class WebSocket : IWebSocket public WebSocket(string url) { WebSocketOptions options = new WebSocketOptions(); - options.uri = new Uri(url); + options.url = url; setWebSocketOptions(options); } @@ -410,7 +402,7 @@ public WebSocket(WebSocketOptions options) public void setWebSocketOptions(WebSocketOptions options) { - uri = options.uri; + uri = new Uri(options.url); headers = options.headers; subprotocols = options.subprotocols; keepAliveInterval = options.keepAliveInterval;