Skip to content

Commit 5809399

Browse files
gedemgedem
authored andcommitted
Connector name, WebSocket options header set
1 parent d83dd64 commit 5809399

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ internal class ClientWebSocketConnector : IWebSocketConnector
1919
private readonly ILoggerFactory _loggerFactory;
2020
private readonly InvocatorRegistry _invocatorRegistry;
2121

22+
public string ConnectionId
23+
{
24+
get
25+
{
26+
return _connectionId;
27+
}
28+
}
29+
2230
public WebSocketState WebSocketState
2331
{
2432
get
@@ -40,26 +48,14 @@ public ClientWebSocketConnector(IOptions<ProxyOptions> options,
4048
Options = options.Value;
4149
}
4250

43-
private WebSocketMessageContext CreateConnectionContext()
44-
{
45-
var context = new WebSocketMessageContext();
46-
context.MessageType = WebSocketMessageType.Text;
47-
context.Command = WebSocketCommands.Connect;
48-
context.Header = new Dictionary<string, object>
49-
{
50-
["Name"] = Options.ConnectorName
51-
};
52-
53-
return context;
54-
}
55-
5651
public async Task ConnectAsync()
5752
{
5853
try
5954
{
6055
var name = Options.ConnectorName;
6156
var uri = new Uri($"ws://{Options.WebSocketHostAddress}");
6257
_webSocket = new ClientWebSocket();
58+
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, Options.ConnectorName);
6359
await _webSocket.ConnectAsync(uri, CancellationToken.None);
6460
var receiverContext = new WebSocketReceiverContext
6561
{
@@ -69,7 +65,9 @@ public async Task ConnectAsync()
6965
Options = Options,
7066
WebSocket = _webSocket
7167
};
72-
var receiver = new WebSocketReceiver(receiverContext, Close);
68+
var receiver = new WebSocketReceiver(receiverContext, Close, (connectionId) => {
69+
_connectionId = connectionId;
70+
});
7371
await receiver.ReceiveAsync();
7472
}
7573
catch (Exception ex)

src/NetCoreStack.WebSockets.ProxyClient/IWebSocketConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace NetCoreStack.WebSockets.ProxyClient
55
{
66
public interface IWebSocketConnector
77
{
8+
string ConnectionId { get; }
89
WebSocketState WebSocketState { get; }
910
ProxyOptions Options { get; }
1011
Task ConnectAsync();

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ await transport.WebSocket.SendAsync(segments,
110110
token);
111111
}
112112

113-
public async Task ConnectAsync(WebSocket webSocket)
113+
public async Task ConnectAsync(WebSocket webSocket, string connectorName = "")
114114
{
115-
WebSocketTransport transport = new WebSocketTransport(webSocket);
115+
WebSocketTransport transport = new WebSocketTransport(webSocket, connectorName);
116116
var connectionId = transport.ConnectionId;
117117
var context = new WebSocketMessageContext();
118118
context.Command = WebSocketCommands.Handshake;

src/NetCoreStack.WebSockets/Interfaces/IConnectionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IConnectionManager
1010
{
1111
ConcurrentDictionary<string, WebSocketTransport> Connections { get; }
1212

13-
Task ConnectAsync(WebSocket webSocket);
13+
Task ConnectAsync(WebSocket webSocket, string connectorName = "");
1414

1515
/// <summary>
1616
/// Text message broadcaster
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace NetCoreStack.WebSockets.Internal
22
{
3-
public class SocketsConstants
3+
public static class SocketsConstants
44
{
55
public static byte[] Splitter = new byte[] { 0x1f };
6-
public const string CompressedKey = "Compressed";
76
public const int ChunkSize = 1024 * 4;
7+
public const string CompressedKey = "Compressed";
8+
public const string ConnectorName = "ConnectorName";
89
}
910
}

src/NetCoreStack.WebSockets/Internal/WebSocketMiddleware.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.Extensions.Logging;
33
using Microsoft.Extensions.Options;
4+
using Microsoft.Extensions.Primitives;
45
using NetCoreStack.WebSockets.Interfaces;
56
using System;
67
using System.Net.WebSockets;
@@ -26,8 +27,15 @@ public async Task Invoke(HttpContext httpContext,
2627
{
2728
if (httpContext.WebSockets.IsWebSocketRequest)
2829
{
30+
string connectorName = string.Empty;
31+
StringValues headerValue = "";
32+
if (httpContext.Request.Headers.TryGetValue(SocketsConstants.ConnectorName, out headerValue))
33+
{
34+
connectorName = headerValue.ToString();
35+
}
36+
2937
var webSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
30-
await manager.ConnectAsync(webSocket);
38+
await manager.ConnectAsync(webSocket, connectorName);
3139
}
3240
else
3341
{

src/NetCoreStack.WebSockets/Internal/WebSocketReceiver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ public class WebSocketReceiver
1212
{
1313
private readonly WebSocketReceiverContext _context;
1414
private readonly Action<WebSocketReceiverContext> _closeCallback;
15+
private readonly Action<string> _handshakeCallback;
1516

16-
public WebSocketReceiver(WebSocketReceiverContext context, Action<WebSocketReceiverContext> closeCallback)
17+
public WebSocketReceiver(WebSocketReceiverContext context, Action<WebSocketReceiverContext> closeCallback, Action<string> handshakeCallback = null)
1718
{
1819
_context = context;
1920
_closeCallback = closeCallback;
21+
_handshakeCallback = handshakeCallback;
2022
}
2123

2224
private async Task InternalReceiveAsync()
@@ -29,7 +31,10 @@ private async Task InternalReceiveAsync()
2931
{
3032
var context = result.ToContext(buffer);
3133
if (context.Command == WebSocketCommands.Handshake)
34+
{
3235
_context.ConnectionId = context.Value?.ToString();
36+
_handshakeCallback?.Invoke(_context.ConnectionId);
37+
}
3338

3439
var _invocators = _context.InvocatorRegistry.GetInvocators(context, _context.Options);
3540
foreach (var invoker in _invocators)

src/NetCoreStack.WebSockets/Internal/WebSocketTransport.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ public class WebSocketTransport : IDisposable
77
{
88
public WebSocket WebSocket { get; }
99
public string ConnectionId { get; }
10+
public string ConnectorName { get; }
1011

11-
public WebSocketTransport(WebSocket webSocket)
12+
public WebSocketTransport(WebSocket webSocket, string connectorName)
1213
{
1314
ConnectionId = Guid.NewGuid().ToString();
1415
WebSocket = webSocket;
16+
ConnectorName = connectorName;
1517
}
1618

1719
public void Dispose()

test/ServerTestApp/Controllers/DiscoveryController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,14 @@ public async Task<IActionResult> SendCompressedBinaryAsync([FromBody]Context mod
120120

121121
return Ok();
122122
}
123+
124+
[HttpGet(nameof(GetConnections))]
125+
public IActionResult GetConnections()
126+
{
127+
var connections = _connectionManager.Connections
128+
.Select(x => new { ConnectionId = x.Value.ConnectionId, ConnectorName = x.Value.ConnectorName });
129+
130+
return Json(connections);
131+
}
123132
}
124133
}

0 commit comments

Comments
 (0)