Skip to content

Commit 8ace84e

Browse files
Gencebay DemirGencebay Demir
authored andcommitted
Provide server current state (CacheReady, IndexOk etc.) during the handshake operation
1 parent 9b6307f commit 8ace84e

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Options;
3+
using NetCoreStack.WebSockets.Common;
34
using System;
45
using System.Linq;
56

@@ -9,6 +10,7 @@ public static class ServiceCollectionExtensions
910
{
1011
public static void AddProxyWebSockets(this IServiceCollection services, Action<ConnectorOptions> setup)
1112
{
13+
services.AddTransient<IHandshakeStateTransport, DefaultHandshakeStateTransport>();
1214
services.AddSingleton<IWebSocketConnector, ClientWebSocketConnector>();
1315
services.AddSingleton<InvocatorRegistry>();
1416
var connectorOptions = new ConnectorOptions { };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Threading.Tasks;
2+
3+
namespace NetCoreStack.WebSockets.Common
4+
{
5+
public class DefaultHandshakeStateTransport : IHandshakeStateTransport
6+
{
7+
public Task<object> GetStateAsync()
8+
{
9+
return Task.FromResult<object>(0);
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace NetCoreStack.WebSockets.Common
4+
{
5+
public interface IHandshakeStateTransport
6+
{
7+
Task<object> GetStateAsync();
8+
}
9+
}

src/NetCoreStack.WebSockets/Internal/ConnectionManagerExtensions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
using System.Net.WebSockets;
1+
using NetCoreStack.WebSockets.Common;
2+
using System.Net.WebSockets;
23
using System.Threading.Tasks;
34

45
namespace NetCoreStack.WebSockets.Internal
56
{
67
internal static class ConnectionManagerExtensions
78
{
8-
public static async Task Handshake(this IConnectionManager manager, WebSocket webSocket)
9+
public static async Task Handshake(this IConnectionManager manager,
10+
WebSocket webSocket,
11+
IHandshakeStateTransport initState)
912
{
1013
WebSocketTransport transport = new WebSocketTransport(webSocket);
1114

12-
var context = new WebSocketMessageContext
13-
{
14-
Command = WebSocketCommands.Handshake,
15-
Value = transport.ConnectionId
16-
};
15+
var context = new WebSocketMessageContext();
16+
context.Command = WebSocketCommands.Handshake;
17+
context.Value = transport.ConnectionId;
18+
context.State = await initState.GetStateAsync();
1719

1820
await manager.SendAsync(transport.ConnectionId, context, webSocket);
1921
await transport.Echo();

src/NetCoreStack.WebSockets/Internal/WebSocketMiddleware.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.Extensions.Logging;
3+
using NetCoreStack.WebSockets.Common;
34
using System.Threading.Tasks;
45

56
namespace NetCoreStack.WebSockets.Internal
67
{
78
public class WebSocketMiddleware
89
{
9-
private readonly RequestDelegate next;
10+
private readonly IHandshakeStateTransport _initState;
11+
private readonly RequestDelegate _next;
1012

11-
public WebSocketMiddleware(RequestDelegate next)
13+
public WebSocketMiddleware(RequestDelegate next, IHandshakeStateTransport initState)
1214
{
13-
this.next = next;
15+
_next = next;
16+
_initState = initState;
1417
}
1518

1619
public async Task Invoke(HttpContext httpContext,
@@ -20,11 +23,11 @@ public async Task Invoke(HttpContext httpContext,
2023
if (httpContext.WebSockets.IsWebSocketRequest)
2124
{
2225
var webSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
23-
await manager.Handshake(webSocket);
26+
await manager.Handshake(webSocket, _initState);
2427
}
2528
else
2629
{
27-
await next(httpContext);
30+
await _next(httpContext);
2831
}
2932
}
3033
}

src/NetCoreStack.WebSockets/SocketServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using NetCoreStack.WebSockets.Common;
23
using NetCoreStack.WebSockets.Internal;
34
using System;
45

@@ -13,6 +14,7 @@ public static void AddNativeWebSockets(this IServiceCollection services)
1314
throw new ArgumentNullException(nameof(services));
1415
}
1516

17+
services.AddTransient<IHandshakeStateTransport, DefaultHandshakeStateTransport>();
1618
services.AddSingleton<IConnectionManager, ConnectionManager>();
1719
}
1820
}

src/NetCoreStack.WebSockets/WebSocketExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using System;
23
using System.Net.WebSockets;
34
using System.Text;
4-
using Newtonsoft.Json;
5-
using System.Threading.Tasks;
65

76
namespace NetCoreStack.WebSockets
87
{

src/NetCoreStack.WebSockets/WebSocketMessageContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class WebSocketMessageContext
44
{
55
public WebSocketCommands? Command { get; set; }
66
public object Value { get; set; }
7+
public object State { get; set; }
78

89
public string CommandText
910
{

0 commit comments

Comments
 (0)