diff --git a/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs b/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs index 32acbd8..a21157c 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.url = "ws://localhost:3000"; + //options.keepAliveInterval = TimeSpan.FromSeconds(10); //Ping Interval. + //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..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); + } - this.instanceId = instanceId; + //headers????? + //creditentials... + + this.instanceId = instanceId; } ~WebSocket () { @@ -373,6 +374,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 +388,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.url = 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 = new Uri(options.url); + headers = options.headers; + subprotocols = options.subprotocols; + keepAliveInterval = options.keepAliveInterval; + //Credentials... string protocol = uri.Scheme; if (!protocol.Equals("ws") && !protocol.Equals("wss")) @@ -468,6 +436,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();