mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 14:03:29 -07:00
Bind any collection to SignalR with a single call.
This commit is contained in:
parent
87a5dc7869
commit
a6aba16902
13 changed files with 106 additions and 26 deletions
|
@ -1,5 +1,6 @@
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
@ -14,6 +15,7 @@ namespace NzbDrone.Api.SignalR
|
|||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
|
||||
public BasicResourceConnection()
|
||||
{
|
||||
_logger = LogManager.GetCurrentClassLogger();
|
||||
|
@ -33,7 +35,8 @@ namespace NzbDrone.Api.SignalR
|
|||
|
||||
public void HandleAsync(ModelEvent<T> message)
|
||||
{
|
||||
Connection.Broadcast(message);
|
||||
var context =((ConnectionManager)GlobalHost.ConnectionManager).GetConnection(GetType());
|
||||
context.Connection.Broadcast(message);
|
||||
}
|
||||
}
|
||||
}
|
42
NzbDrone.Api/SignalR/Serializer.cs
Normal file
42
NzbDrone.Api/SignalR/Serializer.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.SignalR.Json;
|
||||
|
||||
namespace NzbDrone.Api.SignalR
|
||||
{
|
||||
public class Serializer : IJsonSerializer
|
||||
{
|
||||
private readonly Common.IJsonSerializer _nzbDroneSerializer;
|
||||
private JsonNetSerializer _signalRSerializer;
|
||||
|
||||
public Serializer(Common.IJsonSerializer nzbDroneSerializer)
|
||||
{
|
||||
_signalRSerializer = new JsonNetSerializer();
|
||||
_nzbDroneSerializer = nzbDroneSerializer;
|
||||
|
||||
}
|
||||
|
||||
public void Serialize(object value, TextWriter writer)
|
||||
{
|
||||
if (value.GetType().FullName.StartsWith("NzbDrone"))
|
||||
{
|
||||
_nzbDroneSerializer.Serialize(value, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_signalRSerializer.Serialize(value, writer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public object Parse(string json, Type targetType)
|
||||
{
|
||||
if (targetType.FullName.StartsWith("NzbDrone"))
|
||||
{
|
||||
return _nzbDroneSerializer.Deserialize(json, targetType);
|
||||
}
|
||||
|
||||
return _signalRSerializer.Parse(json, targetType);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using Microsoft.AspNet.SignalR.Json;
|
||||
using TinyIoC;
|
||||
|
||||
namespace NzbDrone.Api.SignalR
|
||||
|
@ -13,6 +14,8 @@ namespace NzbDrone.Api.SignalR
|
|||
public static void Register(TinyIoCContainer container)
|
||||
{
|
||||
GlobalHost.DependencyResolver = new SignalrDependencyResolver(container);
|
||||
|
||||
container.Register<IJsonSerializer, Serializer>().AsSingleton();
|
||||
}
|
||||
|
||||
private SignalrDependencyResolver(TinyIoCContainer container)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue