Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dsuryd/dotNetify
Browse files Browse the repository at this point in the history
  • Loading branch information
dicky authored and dicky committed Mar 15, 2020
2 parents c1902a3 + 3ff5499 commit a0a4090
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 300 deletions.
1 change: 1 addition & 0 deletions DevApp/DevApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="DotNetify.Elements" Version="1.2.0" />
<PackageReference Include="DotNetify.Pulse" Version="0.5.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.1.5" />
<PackageReference Include="System.Reactive" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.0.0" />
<PackageReference Include="AspNet.Security.OpenIdConnect.Server" Version="2.0.0" />
Expand Down
4 changes: 4 additions & 0 deletions DevApp/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import './app/styles/prism.css';
import * as views from './app/views';
import 'dotnetify-elements/dotnetify-elements.css';

//import { MessagePackHubProtocol } from '@aspnet/signalr-protocol-msgpack';
//const protocol = new MessagePackHubProtocol();
//dotnetify.hubOptions.connectionBuilder = builder => builder.withHubProtocol(protocol);

dotnetify.debug = true;

// Import all the routeable views into the global window variable.
Expand Down
592 changes: 318 additions & 274 deletions DevApp/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions DevApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@aspnet/signalr": "^1.1.4",
"@aspnet/signalr-protocol-msgpack": "^1.1.0",
"chartjs-plugin-streaming": "~1.7.1",
"dotnetify-elements": "^1.1.0",
"jquery": "~3.4.1",
Expand Down
4 changes: 3 additions & 1 deletion DevApp/server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public void ConfigureServices(IServiceCollection services)
// Add OpenID Connect server to produce JWT access tokens.
services.AddAuthenticationServer();

services.AddSignalR();
services.AddSignalR()
//.AddMessagePackProtocol()
;
services.AddDotNetify();
services.AddDotNetifyPulse();
services.AddMvc();
Expand Down
34 changes: 23 additions & 11 deletions DotNetifyLib.SignalR.Owin/DotNetifyHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DotNetify.Security;
using System.Linq;

namespace DotNetify
{
Expand Down Expand Up @@ -118,16 +119,7 @@ public override Task OnDisconnected(bool stopCalled)
/// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
public void Request_VM(string vmId, object vmArg)
{
JObject data = null;
if (vmArg != null)
{
if (vmArg is JObject)
// Newtonsoft.Json protocol.
data = vmArg as JObject;
else
// MessagePack protocol.
data = JObject.FromObject(vmArg);
}
object data = NormalizeType(vmArg);

try
{
Expand Down Expand Up @@ -159,9 +151,11 @@ public void Request_VM(string vmId, object vmArg)
/// <param name="vmData">View model update data, where key is the property path and value is the property's new value.</param>
public void Update_VM(string vmId, Dictionary<string, object> vmData)
{
var data = vmData?.ToDictionary(x => x.Key, x => NormalizeType(x.Value));

try
{
_hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, vmData, null, Principal);
_hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, data, null, Principal);
_hubPipeline.RunMiddlewares(_hubContext, ctx =>
{
Principal = ctx.Principal;
Expand Down Expand Up @@ -265,6 +259,24 @@ internal void HandleMulticastMessage(string messageType, string vmId, string ser

#endregion Server Responses

/// <summary>
/// Normalizes the type of the object argument to JObject when possible.
/// </summary>
/// <param name="data">Arbitrary object.</param>
/// <returns>JObject if the object is convertible; otherwise unchanged.</returns>
private object NormalizeType(object data)
{
if (data == null)
return null;
else if (data is JObject)
// Newtonsoft.Json protocol.
return data as JObject;
else if (!(data.GetType().IsPrimitive || data is string))
// MessagePack protocol.
return JObject.FromObject(data);
return data;
}

/// <summary>
/// Runs the view model filter.
/// </summary>
Expand Down
40 changes: 26 additions & 14 deletions DotNetifyLib.SignalR/DotNetifyHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Text.Json;
Expand Down Expand Up @@ -123,19 +124,7 @@ public override Task OnDisconnectedAsync(Exception exception)
/// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
public void Request_VM(string vmId, object vmArg)
{
JObject data = null;
if (vmArg != null)
{
if (vmArg is JsonElement)
// System.Text.Json protocol.
data = JObject.Parse(((JsonElement) vmArg).GetRawText());
else if (vmArg is JObject)
// Newtonsoft.Json protocol.
data = vmArg as JObject;
else
// MessagePack protocol.
data = JObject.FromObject(vmArg);
}
object data = NormalizeType(vmArg);

try
{
Expand Down Expand Up @@ -168,10 +157,12 @@ public void Request_VM(string vmId, object vmArg)
/// <param name="vmData">View model update data, where key is the property path and value is the property's new value.</param>
public void Update_VM(string vmId, Dictionary<string, object> vmData)
{
var data = vmData?.ToDictionary(x => x.Key, x => NormalizeType(x.Value));

try
{
_callerContext = Context;
_hubContext = new DotNetifyHubContext(_callerContext, nameof(Update_VM), vmId, vmData, null, Principal);
_hubContext = new DotNetifyHubContext(_callerContext, nameof(Update_VM), vmId, data, null, Principal);
_hubPipeline.RunMiddlewares(_hubContext, ctx =>
{
Principal = ctx.Principal;
Expand Down Expand Up @@ -280,6 +271,27 @@ internal void HandleMulticastMessage(string messageType, string vmId, string ser

#endregion Server Responses

/// <summary>
/// Normalizes the type of the object argument to JObject when possible.
/// </summary>
/// <param name="data">Arbitrary object.</param>
/// <returns>JObject if the object is convertible; otherwise unchanged.</returns>
private object NormalizeType(object data)
{
if (data == null)
return null;
else if (data is JsonElement)
// System.Text.Json protocol.
return JObject.Parse(((JsonElement)data).GetRawText());
else if (data is JObject)
// Newtonsoft.Json protocol.
return data as JObject;
else if (!(data.GetType().IsPrimitive || data is string))
// MessagePack protocol.
return JObject.FromObject(data);
return data;
}

/// <summary>
/// Runs the view model filter.
/// </summary>
Expand Down

0 comments on commit a0a4090

Please sign in to comment.