Skip to content

Commit 182aca8

Browse files
CatoLeanTruetschelgalvesribeiro
authored andcommitted
(Temporary) Support for byte arrays. Fixes #19 (#20)
1 parent 3199ed9 commit 182aca8

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

src/Blazor.Extensions.SignalR.JS/src/HubConnectionManager.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ export class HubConnectionManager {
7979
return connection.invoke(methodName, ...args);
8080
}
8181

82-
public InvokeWithResultAsync = (connectionId: string, methodName: string, args: any[]): Promise<any> => {
82+
public InvokeWithResultAsync = async (connectionId: string, methodName: string, args: any[]): Promise<any> => {
8383
const connection = this.GetConnection(connectionId);
8484

85-
return connection.invoke(methodName, ...args);
85+
var result = await connection.invoke(methodName, ...args);
86+
87+
return this.ReplaceTypedArray(result);
8688
}
8789

8890
private GetConnection = (connectionId: string) => {
@@ -95,9 +97,27 @@ export class HubConnectionManager {
9597
return connection;
9698
}
9799

100+
private ReplaceTypedArray = (obj: any): any => {
101+
if (obj instanceof Int8Array ||
102+
obj instanceof Uint8Array ||
103+
obj instanceof Uint8ClampedArray ||
104+
obj instanceof Int16Array ||
105+
obj instanceof Uint16Array ||
106+
obj instanceof Int32Array ||
107+
obj instanceof Uint32Array ||
108+
obj instanceof Float32Array ||
109+
obj instanceof Float64Array)
110+
{
111+
obj = Array.prototype.slice.call(obj);
112+
}
113+
114+
return obj;
115+
}
116+
98117
public On = (connectionId: string, callback: DotNetReferenceType) => {
99118
const connection = this.GetConnection(connectionId);
100-
const handle = (...payloads) => callback.invokeMethodAsync<void>('On', payloads.map(payload => JSON.stringify(payload)));
119+
const handle = (...payloads) => callback.invokeMethodAsync<void>(
120+
'On', payloads.map(payload => JSON.stringify(this.ReplaceTypedArray(payload))));
101121

102122
this._handles.set(callback.invokeMethod<string>('get_Id'), handle);
103123

test/Blazor.Extensions.SignalR.Test.Client/Pages/Chat.cshtml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
</div>
1919
</form>
2020

21+
<h4>Byte array Arguments</h4>
22+
<form class="form-inline">
23+
<div class="input-append">
24+
<input type="button" class="btn" value="Gimme byte array arguments!" onclick=@DoByteArrayArg />
25+
26+
</div>
27+
</form>
28+
2129
<h4>To Everybody</h4>
2230
<form class="form-inline">
2331
<div class="input-append">

test/Blazor.Extensions.SignalR.Test.Client/Pages/ChatComponent.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ChatComponent : BlazorComponent
2525
private IDisposable _listHandle;
2626
private IDisposable _multiArgsHandle;
2727
private IDisposable _multiArgsComplexHandle;
28+
private IDisposable _byteArrayHandle;
2829
private HubConnection _connection;
2930

3031
protected override async Task OnInitAsync()
@@ -89,6 +90,14 @@ public Task DemoMultipleArgsComplex(object arg1, object arg2)
8990
return this.HandleArgs(arg1, arg2);
9091
}
9192

93+
public Task DemoByteArrayArg(byte[] array)
94+
{
95+
this._logger.LogInformation("Got byte array!");
96+
this._byteArrayHandle.Dispose();
97+
98+
return this.HandleArgs(BitConverter.ToString(array));
99+
}
100+
92101
private async Task<string> GetJwtToken(string userId)
93102
{
94103
var httpResponse = await this._http.GetAsync($"/generatetoken?user={userId}");
@@ -162,6 +171,14 @@ internal async Task DoMultipleArgs()
162171
await this._connection.InvokeAsync("DoMultipleArgsComplex");
163172
}
164173

174+
internal async Task DoByteArrayArg()
175+
{
176+
this._byteArrayHandle = this._connection.On<byte[]>("DemoByteArrayArg", this.DemoByteArrayArg);
177+
var array = await this._connection.InvokeAsync<byte[]>("DoByteArrayArg");
178+
179+
this._logger.LogInformation("Got byte returned from hub method array: {0}", BitConverter.ToString(array));
180+
}
181+
165182
internal async Task TellHubToDoStuff()
166183
{
167184
this._objectHandle = this._connection.On<DemoData>("DemoMethodObject", this.DemoMethodObject);

test/Blazor.Extensions.SignalR.Test.Server/Blazor.Extensions.SignalR.Test.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.4" />
99
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="0.6.0" />
10-
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.2" />
10+
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.4" />
1111
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.0.2" />
1212
</ItemGroup>
1313

test/Blazor.Extensions.SignalR.Test.Server/Hubs/ChatHub.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public override async Task OnDisconnectedAsync(Exception ex)
2727
await this.Clients.Others.SendAsync("Send", $"{this.Context.ConnectionId} left");
2828
}
2929

30+
public async Task<byte[]> DoByteArrayArg()
31+
{
32+
var array = new byte[] { 1, 2, 3 };
33+
34+
await this.Clients.All.SendAsync("DemoByteArrayArg", array);
35+
36+
return array;
37+
}
38+
3039
public Task DoMultipleArgs()
3140
{
3241
return this.Clients.All.SendAsync("DemoMultiArgs", "one", 2, "three", 4);

0 commit comments

Comments
 (0)