parent
45b4972091
commit
87a5dc7869
@ -0,0 +1,39 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Datastore.Events;
|
||||
|
||||
namespace NzbDrone.Api.SignalR
|
||||
{
|
||||
public abstract class BasicResourceConnection<T> :
|
||||
NzbDronePersistentConnection,
|
||||
IHandleAsync<ModelEvent<T>>
|
||||
where T : ModelBase
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public BasicResourceConnection()
|
||||
{
|
||||
_logger = LogManager.GetCurrentClassLogger();
|
||||
}
|
||||
|
||||
protected override Task OnConnected(IRequest request, string connectionId)
|
||||
{
|
||||
_logger.Debug("SignalR client connected. ID:{0}", connectionId);
|
||||
return base.OnConnected(request, connectionId);
|
||||
}
|
||||
|
||||
public override Task ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext context)
|
||||
{
|
||||
_logger.Debug("Request: {0}", context);
|
||||
return base.ProcessRequest(context);
|
||||
}
|
||||
|
||||
public void HandleAsync(ModelEvent<T> message)
|
||||
{
|
||||
Connection.Broadcast(message);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using Microsoft.AspNet.SignalR;
|
||||
|
||||
namespace NzbDrone.Api.SignalR
|
||||
{
|
||||
public abstract class NzbDronePersistentConnection : PersistentConnection
|
||||
{
|
||||
public abstract string Resource { get; }
|
||||
}
|
||||
}
|
@ -1,26 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Nancy.Bootstrapper;
|
||||
using Nancy.Owin;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using NzbDrone.Api.SignalR;
|
||||
using Owin;
|
||||
using TinyIoC;
|
||||
|
||||
namespace NzbDrone.Owin.MiddleWare
|
||||
{
|
||||
public class SignalRMiddleWare : IOwinMiddleWare
|
||||
{
|
||||
private readonly INancyBootstrapper _nancyBootstrapper;
|
||||
private readonly IEnumerable<NzbDronePersistentConnection> _persistentConnections;
|
||||
|
||||
public SignalRMiddleWare(INancyBootstrapper nancyBootstrapper)
|
||||
public int Order { get { return 0; } }
|
||||
|
||||
public SignalRMiddleWare(IEnumerable<NzbDronePersistentConnection> persistentConnections, TinyIoCContainer container)
|
||||
{
|
||||
_nancyBootstrapper = nancyBootstrapper;
|
||||
_persistentConnections = persistentConnections;
|
||||
|
||||
SignalrDependencyResolver.Register(container);
|
||||
}
|
||||
|
||||
public void Attach(IAppBuilder appBuilder)
|
||||
{
|
||||
return;
|
||||
var nancyOwinHost = new NancyOwinHost(null, _nancyBootstrapper);
|
||||
appBuilder.Use((Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>>)(next => (Func<IDictionary<string, object>, Task>)nancyOwinHost.Invoke), new object[0]);
|
||||
foreach (var nzbDronePersistentConnection in _persistentConnections)
|
||||
{
|
||||
appBuilder.MapConnection("signalr/series", nzbDronePersistentConnection.GetType(), new ConnectionConfiguration { EnableCrossDomain = true });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
(function ($) {
|
||||
|
||||
var connection = $.connection('/signalr/series');
|
||||
|
||||
var _getStatus = function (status) {
|
||||
switch (status) {
|
||||
case 0:
|
||||
return 'connecting';
|
||||
case 1:
|
||||
return 'connected';
|
||||
case 2:
|
||||
return 'reconnecting';
|
||||
case 4:
|
||||
return 'disconnected';
|
||||
default:
|
||||
throw 'invalid status ' + status;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
connection.stateChanged(function (change) {
|
||||
|
||||
console.log('signalR [{0}]'.format(_getStatus(change.newState)));
|
||||
});
|
||||
|
||||
connection.received(function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
connection.error(function (error) {
|
||||
console.warn(error);
|
||||
});
|
||||
|
||||
connection.start();
|
||||
})(jQuery);
|
Loading…
Reference in new issue