Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support subprotocols derived from WebSocket #125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Websocket.Client/IWebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Websocket.Client.Models;

namespace Websocket.Client
{
Expand All @@ -16,6 +15,11 @@ public interface IWebsocketClient : IDisposable
/// </summary>
Uri Url { get; set; }

/// <summary>
/// Validate if an incoming message is from sub-protocol
/// </summary>
Predicate< string> IsSubprotocolMessage { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used?


/// <summary>
/// Stream with received message (raw format)
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Websocket.Client/Models/DisconnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Websocket.Client
public class DisconnectionInfo
{
/// <inheritdoc />
public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus? closeStatus,
public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus closeStatus,
string closeStatusDescription, string subProtocol, Exception exception)
{
Type = type;
Expand All @@ -28,7 +28,7 @@ public class DisconnectionInfo
/// <summary>
/// Indicates the reason why the remote endpoint initiated the close handshake
/// </summary>
public WebSocketCloseStatus? CloseStatus { get; }
public WebSocketCloseStatus CloseStatus { get; }

/// <summary>
/// Allows the remote endpoint to describe the reason why the connection was closed
Expand Down Expand Up @@ -62,7 +62,7 @@ public class DisconnectionInfo
/// </summary>
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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Websocket.Client/Models/DisconnectionType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable once CheckNamespace

namespace Websocket.Client
{
/// <summary>
Expand All @@ -19,7 +20,7 @@ public enum DisconnectionType
/// <summary>
/// Type used when connection to websocket was lost by not receiving any message in given time-range
/// </summary>
NoMessageReceived = 2,
NoMessageReceived = 2,

/// <summary>
/// Type used when connection or reconnection returned error
Expand Down
2 changes: 1 addition & 1 deletion src/Websocket.Client/Models/ReconnectionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

// ReSharper disable once CheckNamespace
namespace Websocket.Client.Models
Copy link
Owner

@Marfusios Marfusios Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, put it back or we will need to deploy it under version 5.x.

namespace Websocket.Client
{
/// <summary>
/// Info about happened reconnection
Expand Down
3 changes: 2 additions & 1 deletion src/Websocket.Client/Models/ReconnectionType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable once CheckNamespace

namespace Websocket.Client
{
/// <summary>
Expand All @@ -19,7 +20,7 @@ public enum ReconnectionType
/// <summary>
/// Type used when connection to websocket was lost by not receiving any message in given time-range
/// </summary>
NoMessageReceived = 2,
NoMessageReceived = 2,

/// <summary>
/// Type used after unsuccessful previous reconnection
Expand Down
32 changes: 31 additions & 1 deletion src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,6 +40,34 @@ public partial class WebsocketClient : IWebsocketClient
private readonly Subject<ReconnectionInfo> _reconnectionSubject = new Subject<ReconnectionInfo>();
private readonly Subject<DisconnectionInfo> _disconnectedSubject = new Subject<DisconnectionInfo>();


/// <summary>
///
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments please

/// </summary>
/// <param name="clientFactory"></param>

public WebsocketClient(Func<ClientWebSocket> clientFactory = null)
: this(GetClientFactory(clientFactory))
{
}

/// <summary>
///
/// </summary>
public WebsocketClient(Func<Uri, CancellationToken, Task<WebSocket>> 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;
});
}

/// <summary>
/// A simple websocket client with built-in reconnection and error handling
/// </summary>
Expand Down Expand Up @@ -165,6 +192,9 @@ public bool IsReconnectionEnabled
/// <inheritdoc />
public ClientWebSocket NativeClient => GetSpecificOrThrow(_client);

/// <inheritdoc />
public Predicate<string> IsSubprotocolMessage { get; set; } = null;

/// <summary>
/// Terminate the websocket connection and cleanup everything
/// </summary>
Expand Down