Skip to content

Commit b4eb357

Browse files
committed
invocator instance resolve bug fix, header provider
1 parent 0b53200 commit b4eb357

File tree

13 files changed

+70
-27
lines changed

13 files changed

+70
-27
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ProxyWebSocketsBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private void RegisterInternal<TInvocator>()
1717
{
1818
var invocatorType = typeof(TInvocator);
1919
InvocatorFactory.Invocators.Add(invocatorType);
20+
_services.AddTransient(invocatorType);
2021
_services.AddSingleton<IWebSocketConnector<TInvocator>, ClientWebSocketConnectorOfT<TInvocator>>();
2122
}
2223

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ConnectionManager : IConnectionManager
1919
{
2020
private readonly IServiceProvider _serviceProvider;
2121
private readonly IHandshakeStateTransport _initState;
22+
private readonly IHeaderProvider _headerProvider;
2223
private readonly ILoggerFactory _loggerFactory;
2324
private readonly IStreamCompressor _compressor;
2425

@@ -27,11 +28,13 @@ public class ConnectionManager : IConnectionManager
2728
public ConnectionManager(IServiceProvider serviceProvider,
2829
IStreamCompressor compressor,
2930
IHandshakeStateTransport initState,
31+
IHeaderProvider headerProvider,
3032
ILoggerFactory loggerFactory)
3133
{
3234
_serviceProvider = serviceProvider;
3335
_compressor = compressor;
3436
_initState = initState;
37+
_headerProvider = headerProvider;
3538
_loggerFactory = loggerFactory;
3639
Connections = new ConcurrentDictionary<string, WebSocketTransport>(StringComparer.OrdinalIgnoreCase);
3740
}
@@ -60,6 +63,7 @@ private async Task<byte[]> PrepareFramesBytesAsync(byte[] body, IDictionary<stri
6063
properties.Add(CompressedKey, compressed);
6164
}
6265

66+
_headerProvider.Invoke(properties);
6367
string props = JsonConvert.SerializeObject(properties);
6468
byte[] header = Encoding.UTF8.GetBytes($"{props}");
6569

@@ -228,6 +232,7 @@ public Task SendAsync(string connectionId, WebSocketMessageContext context)
228232
throw new ArgumentOutOfRangeException(nameof(transport));
229233
}
230234

235+
_headerProvider.Invoke(context.Header);
231236
var segments = context.ToSegment();
232237
var descriptor = new WebSocketMessageDescriptor
233238
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NetCoreStack.WebSockets.Internal;
2+
using System.Collections.Generic;
3+
4+
namespace NetCoreStack.WebSockets
5+
{
6+
public class DefaultHeaderProvider : IHeaderProvider
7+
{
8+
public void Invoke(IDictionary<string, object> header)
9+
{
10+
if (header == null)
11+
{
12+
return;
13+
}
14+
15+
if (!header.TryGetValue(SocketsConstants.WSFQN, out object host))
16+
{
17+
header.Add(SocketsConstants.WSFQN, FQNHelper.Name);
18+
}
19+
}
20+
}
21+
}

src/NetCoreStack.WebSockets/Extensions/SocketServiceCollectionExtensions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ public static void AddNativeWebSockets<TInvocator>(this IServiceCollection servi
1616
{
1717
throw new ArgumentNullException(nameof(services));
1818
}
19-
19+
2020
services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
21+
services.TryAdd(ServiceDescriptor.Singleton<IHeaderProvider, DefaultHeaderProvider>());
2122
services.TryAdd(ServiceDescriptor.Singleton<IStreamCompressor, GZipStreamCompressor>());
2223
services.TryAdd(ServiceDescriptor.Transient<IHandshakeStateTransport, DefaultHandshakeStateTransport>());
2324

2425
services.AddSingleton<IConnectionManager, ConnectionManager>();
2526
services.AddTransient(typeof(IServerWebSocketCommandInvocator), typeof(TInvocator));
2627
}
28+
29+
public static void AddNativeWebSockets<TInvocator>(this IServiceCollection services, string hostname)
30+
where TInvocator : IServerWebSocketCommandInvocator
31+
{
32+
AddNativeWebSockets<TInvocator>(services);
33+
}
2734
}
28-
}
35+
}

src/NetCoreStack.WebSockets/Extensions/WebSocketExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public static ArraySegment<byte> ToSegment(this WebSocketMessageContext webSocke
102102
return new ArraySegment<byte>(content, 0, content.Length);
103103
}
104104

105-
public static string GetConnectionId(this WebSocketMessageContext webSocketContext)
105+
public static string GetConnectionId(this WebSocketMessageContext context)
106106
{
107-
if (webSocketContext == null)
107+
if (context == null)
108108
{
109-
throw new ArgumentNullException(nameof(webSocketContext));
109+
throw new ArgumentNullException(nameof(context));
110110
}
111111

112112
object connectionId = null;
113-
if (webSocketContext.Header.TryGetValue(SocketsConstants.ConnectionId, out connectionId))
113+
if (context.Header.TryGetValue(SocketsConstants.ConnectionId, out connectionId))
114114
{
115115
return connectionId.ToString();
116116
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace NetCoreStack.WebSockets
4+
{
5+
public interface IHeaderProvider
6+
{
7+
void Invoke(IDictionary<string, object> header);
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace NetCoreStack.WebSockets.Internal
5+
{
6+
internal static class FQNHelper
7+
{
8+
internal static readonly string Name = Environment.MachineName;
9+
}
10+
}

src/NetCoreStack.WebSockets/Internal/SocketsConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public static class SocketsConstants
44
{
55
public static byte[] Splitter = new byte[] { 0x1f };
66
public const int ChunkSize = 1024 * 4;
7+
public const string WSFQN = "X-NetCoreStack-WSHost";
78
public const string CompressedKey = "Compressed";
89
public const string ConnectorName = "ConnectorName";
910
public const string ConnectionId = "ConnectionId";

src/NetCoreStack.WebSockets/Internal/WebSocketMessageDescriptor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public class WebSocketMessageDescriptor
99
public ArraySegment<byte> Segments { get; set; }
1010
public WebSocketMessageType MessageType { get; set; }
1111
public bool EndOfMessage { get; set; }
12-
public bool IsQueue { get; set; }
1312
public CancellationToken CancellationToken { get; }
1413

1514
public WebSocketMessageDescriptor()

test/ServerTestApp/Controllers/DiscoveryController.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using Common.Libs;
2-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
32
using Microsoft.AspNetCore.Routing;
43
using Microsoft.Extensions.Caching.Distributed;
54
using Microsoft.Extensions.Logging;
65
using NetCoreStack.WebSockets;
76
using NetCoreStack.WebSockets.Internal;
87
using ServerTestApp.Models;
98
using System;
10-
using System.Collections.Generic;
119
using System.Linq;
1210
using System.Net.WebSockets;
11+
using System.Text;
1312
using System.Threading;
1413
using System.Threading.Tasks;
1514

@@ -62,20 +61,9 @@ public async Task<IActionResult> BroadcastBinaryAsync([FromBody]SimpleModel mode
6261
[HttpPost(nameof(SendBinaryAsync))]
6362
public async Task<IActionResult> SendBinaryAsync()
6463
{
65-
try
66-
{
67-
foreach (KeyValuePair<string, CacheItemDescriptor> entry in CacheHelper.CacheKeys)
68-
{
69-
var routeValueDictionary = new RouteValueDictionary(new { Key = entry.Key });
70-
var bytes = _distrubutedCache.Get(entry.Key);
71-
await _connectionManager.BroadcastBinaryAsync(bytes, routeValueDictionary);
72-
}
73-
}
74-
catch (Exception ex)
75-
{
76-
throw ex;
77-
}
78-
64+
var routeValueDictionary = new RouteValueDictionary(new { Key = "SomeKey" });
65+
var bytes = Encoding.UTF8.GetBytes("Hello World");
66+
await _connectionManager.BroadcastBinaryAsync(bytes, routeValueDictionary);
7967
return Ok();
8068
}
8169

0 commit comments

Comments
 (0)