diff --git a/src/Websocket.Client/IWebsocketClient.cs b/src/Websocket.Client/IWebsocketClient.cs index 36f5ec1..7a6e75f 100644 --- a/src/Websocket.Client/IWebsocketClient.cs +++ b/src/Websocket.Client/IWebsocketClient.cs @@ -2,7 +2,6 @@ using System.Net.WebSockets; using System.Text; using System.Threading.Tasks; -using Websocket.Client.Models; namespace Websocket.Client { @@ -16,6 +15,11 @@ public interface IWebsocketClient : IDisposable /// Uri Url { get; set; } + /// + /// Validate if an incoming message is from sub-protocol + /// + Predicate< string> IsSubprotocolMessage { get; set; } + /// /// Stream with received message (raw format) /// diff --git a/src/Websocket.Client/Models/DisconnectionInfo.cs b/src/Websocket.Client/Models/DisconnectionInfo.cs index 8b43dbf..98350fe 100644 --- a/src/Websocket.Client/Models/DisconnectionInfo.cs +++ b/src/Websocket.Client/Models/DisconnectionInfo.cs @@ -10,7 +10,7 @@ namespace Websocket.Client public class DisconnectionInfo { /// - public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus? closeStatus, + public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus closeStatus, string closeStatusDescription, string subProtocol, Exception exception) { Type = type; @@ -28,7 +28,7 @@ public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus? closeStat /// /// Indicates the reason why the remote endpoint initiated the close handshake /// - public WebSocketCloseStatus? CloseStatus { get; } + public WebSocketCloseStatus CloseStatus { get; } /// /// Allows the remote endpoint to describe the reason why the connection was closed @@ -62,7 +62,7 @@ public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus? closeStat /// public static DisconnectionInfo Create(DisconnectionType type, WebSocket client, Exception exception) { - return new DisconnectionInfo(type, client?.CloseStatus, client?.CloseStatusDescription, + return new DisconnectionInfo(type, (WebSocketCloseStatus) client?.CloseStatus, client?.CloseStatusDescription, client?.SubProtocol, exception); } } diff --git a/src/Websocket.Client/Models/DisconnectionType.cs b/src/Websocket.Client/Models/DisconnectionType.cs index a87ccf0..f23e460 100644 --- a/src/Websocket.Client/Models/DisconnectionType.cs +++ b/src/Websocket.Client/Models/DisconnectionType.cs @@ -1,4 +1,5 @@ // ReSharper disable once CheckNamespace + namespace Websocket.Client { /// @@ -19,7 +20,7 @@ public enum DisconnectionType /// /// Type used when connection to websocket was lost by not receiving any message in given time-range /// - NoMessageReceived = 2, + NoMessageReceived = 2, /// /// Type used when connection or reconnection returned error diff --git a/src/Websocket.Client/Models/ReconnectionInfo.cs b/src/Websocket.Client/Models/ReconnectionInfo.cs index 2884412..ddf1e0b 100644 --- a/src/Websocket.Client/Models/ReconnectionInfo.cs +++ b/src/Websocket.Client/Models/ReconnectionInfo.cs @@ -1,7 +1,7 @@ using System; // ReSharper disable once CheckNamespace -namespace Websocket.Client.Models +namespace Websocket.Client { /// /// Info about happened reconnection diff --git a/src/Websocket.Client/Models/ReconnectionType.cs b/src/Websocket.Client/Models/ReconnectionType.cs index 356743c..6194998 100644 --- a/src/Websocket.Client/Models/ReconnectionType.cs +++ b/src/Websocket.Client/Models/ReconnectionType.cs @@ -1,4 +1,5 @@ // ReSharper disable once CheckNamespace + namespace Websocket.Client { /// @@ -19,7 +20,7 @@ public enum ReconnectionType /// /// Type used when connection to websocket was lost by not receiving any message in given time-range /// - NoMessageReceived = 2, + NoMessageReceived = 2, /// /// Type used after unsuccessful previous reconnection diff --git a/src/Websocket.Client/WebsocketClient.cs b/src/Websocket.Client/WebsocketClient.cs index 0885239..ff3c007 100644 --- a/src/Websocket.Client/WebsocketClient.cs +++ b/src/Websocket.Client/WebsocketClient.cs @@ -10,7 +10,6 @@ using System.Threading.Tasks; using Websocket.Client.Exceptions; using Websocket.Client.Logging; -using Websocket.Client.Models; using Websocket.Client.Threading; namespace Websocket.Client @@ -41,6 +40,34 @@ public partial class WebsocketClient : IWebsocketClient private readonly Subject _reconnectionSubject = new Subject(); private readonly Subject _disconnectedSubject = new Subject(); + + /// + /// + /// + /// + + public WebsocketClient(Func clientFactory = null) + : this(GetClientFactory(clientFactory)) + { + } + + /// + /// + /// + public WebsocketClient(Func> connectionFactory = null) + { + _connectionFactory = connectionFactory ?? (async (uri, token) => + { + //var client = new ClientWebSocket + //{ + // Options = { KeepAliveInterval = new TimeSpan(0, 0, 5, 0) } + //}; + var client = new ClientWebSocket(); + await client.ConnectAsync(uri, token).ConfigureAwait(false); + return client; + }); + } + /// /// A simple websocket client with built-in reconnection and error handling /// @@ -165,6 +192,9 @@ public bool IsReconnectionEnabled /// public ClientWebSocket NativeClient => GetSpecificOrThrow(_client); + /// + public Predicate IsSubprotocolMessage { get; set; } = null; + /// /// Terminate the websocket connection and cleanup everything ///