Skip to content

Commit 8678ec9

Browse files
committed
Text message API changed, bug fix
1 parent afecdbb commit 8678ec9

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ConnectionManager(IServiceProvider serviceProvider,
4242
Connections = new ConcurrentDictionary<string, WebSocketTransport>(StringComparer.OrdinalIgnoreCase);
4343
}
4444

45-
private async Task<byte[]> PrepareFramesBytesAsync(byte[] body, IDictionary<string, object> properties = null)
45+
private async Task<byte[]> ToBytesAsync(byte[] body, IDictionary<string, object> properties = null)
4646
{
4747
if (body == null)
4848
{
@@ -67,7 +67,7 @@ private async Task<byte[]> PrepareFramesBytesAsync(byte[] body, IDictionary<stri
6767

6868
_headerProvider.Invoke(properties);
6969
string props = JsonConvert.SerializeObject(properties);
70-
byte[] header = Encoding.UTF8.GetBytes($"{props}");
70+
byte[] header = Encoding.UTF8.GetBytes(props);
7171

7272
if (!compressed)
7373
{
@@ -187,22 +187,21 @@ public async Task BroadcastBinaryAsync(byte[] inputs, IDictionary<string, object
187187
return;
188188
}
189189

190-
var bytes = await PrepareFramesBytesAsync(inputs, properties);
190+
var bytes = await ToBytesAsync(inputs, properties);
191191
using (var stream = new MemoryStream(bytes))
192192
{
193193
await SendDataAsync(stream, WebSocketMessageType.Binary, Connections.Select(c => c.Key).ToArray());
194194
}
195195
}
196196

197-
public async Task BroadcastAsync(byte[] inputs, IDictionary<string, object> properties = null)
197+
public async Task BroadcastAsync(byte[] inputs)
198198
{
199199
if (!Connections.Any())
200200
{
201201
return;
202202
}
203203

204-
var bytes = await PrepareFramesBytesAsync(inputs, properties);
205-
using (var stream = new MemoryStream(bytes))
204+
using (var stream = new MemoryStream(inputs))
206205
{
207206
await SendDataAsync(stream, WebSocketMessageType.Text, Connections.Select(c => c.Key).ToArray());
208207
}
@@ -217,7 +216,7 @@ public async Task BroadcastBinaryAsync(WebSocketMessageContext context)
217216

218217
using (var ms = context.ToMemoryStream())
219218
{
220-
var bytes = await PrepareFramesBytesAsync(ms.ToArray());
219+
var bytes = await ToBytesAsync(ms.ToArray());
221220
using (var stream = new MemoryStream(bytes))
222221
{
223222
await SendDataAsync(stream, WebSocketMessageType.Binary, Connections.Select(c => c.Key).ToArray());
@@ -251,7 +250,7 @@ public async Task SendBinaryAsync(string connectionId, byte[] input, IDictionary
251250
throw new ArgumentOutOfRangeException(nameof(transport));
252251
}
253252

254-
byte[] bytes = await PrepareFramesBytesAsync(input, properties);
253+
byte[] bytes = await ToBytesAsync(input, properties);
255254
using (var stream = new MemoryStream(bytes))
256255
{
257256
await SendDataAsync(stream, WebSocketMessageType.Binary, connectionId);

src/NetCoreStack.WebSockets/Interfaces/IConnectionManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ public interface IConnectionManager
2323
/// Text message broadcaster
2424
/// </summary>
2525
/// <param name="inputs">Byte content</param>
26-
/// <param name="properties">Extra properties, header</param>
2726
/// <returns></returns>
28-
Task BroadcastAsync(byte[] inputs, IDictionary<string, object> properties = null);
27+
Task BroadcastAsync(byte[] inputs);
2928

3029
/// <summary>
3130
/// Binary message broadcaster

test/ServerTestApp/Controllers/DiscoveryController.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NetCoreStack.WebSockets;
66
using NetCoreStack.WebSockets.Internal;
77
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Serialization;
89
using ServerTestApp.Models;
910
using System;
1011
using System.Linq;
@@ -51,6 +52,27 @@ public async Task<IActionResult> SendAsync([FromBody]SimpleModel model)
5152
return Ok();
5253
}
5354

55+
[HttpPost(nameof(SendTextAsync))]
56+
public async Task<IActionResult> SendTextAsync([FromBody]SimpleModel model)
57+
{
58+
if (model != null)
59+
{
60+
var echo = $"Echo from server '{model.Key}' - {DateTime.Now}";
61+
var obj = new { message = echo };
62+
var webSocketContext = new WebSocketMessageContext { Command = WebSocketCommands.DataSend, Value = obj };
63+
64+
var str = JsonConvert.SerializeObject(webSocketContext, new JsonSerializerSettings
65+
{
66+
ContractResolver = new CamelCasePropertyNamesContractResolver()
67+
});
68+
69+
var bytes = Encoding.UTF8.GetBytes(str);
70+
await _connectionManager.BroadcastAsync(bytes);
71+
}
72+
73+
return Ok();
74+
}
75+
5476
[HttpPost(nameof(BroadcastBinaryAsync))]
5577
public async Task<IActionResult> BroadcastBinaryAsync([FromBody]SimpleModel model)
5678
{

0 commit comments

Comments
 (0)