fixes for hub

This commit is contained in:
Kukks 2018-12-27 20:55:46 +01:00
parent c7e2f979dd
commit ef9a633aa4
4 changed files with 33 additions and 24 deletions

View file

@ -38,6 +38,7 @@ using BTCPayServer.Logging;
using BTCPayServer.HostedServices;
using Meziantou.AspNetCore.BundleTagHelpers;
using System.Security.Claims;
using BTCPayServer.Hubs;
using BTCPayServer.Payments.Changelly;
using BTCPayServer.Security;
using Microsoft.AspNetCore.Mvc.ModelBinding;
@ -73,6 +74,7 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<TokenRepository>();
services.TryAddSingleton<EventAggregator>();
services.TryAddSingleton<CoinAverageSettings>();
services.TryAddSingleton<CrowdfundHubStreamer>();
services.TryAddSingleton<ApplicationDbContextFactory>(o =>
{
var opts = o.GetRequiredService<BTCPayServerOptions>();

View file

@ -14,17 +14,18 @@ using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
namespace BTCPayServer.Hubs
{
public class CrowdfundHub: Hub
{
private readonly AppsPublicController _AppsPublicController;
private readonly IServiceProvider _ServiceProvider;
public CrowdfundHub(AppsPublicController appsPublicController)
public CrowdfundHub(IServiceProvider serviceProvider)
{
_AppsPublicController = appsPublicController;
_ServiceProvider = serviceProvider;
}
public async Task ListenToCrowdfundApp(string appId)
{
@ -42,11 +43,16 @@ namespace BTCPayServer.Hubs
}
public async Task<string> CreateInvoice(ContributeToCrowdfund model)
public async Task CreateInvoice(ContributeToCrowdfund model)
{
model.RedirectToCheckout = false;
var result = await _AppsPublicController.ContributeToCrowdfund(Context.Items["app"].ToString(), model);
return (result as OkObjectResult)?.Value.ToString();
using (var scope = _ServiceProvider.CreateScope())
{
var controller = scope.ServiceProvider.GetService<AppsPublicController>();
model.RedirectToCheckout = false;
var result = await controller.ContributeToCrowdfund(Context.Items["app"].ToString(), model);
await Clients.Caller.SendCoreAsync("InvoiceCreated", new[] {(result as OkObjectResult)?.Value.ToString()});
}
}
public async Task PaymentReceived()

View file

@ -1,4 +1,5 @@
var app = null;
var eventAggregator = new Vue();
window.onload = function (ev) {

View file

@ -1,41 +1,41 @@
var hubListener = function(){
var statuses = {
DISCONNECTED: "disconnected",
CONNECTED: "connected",
CONNECTING: "connecting"
};
var status = "disconnected";
var connection = new signalR.HubConnectionBuilder().withUrl("/crowdfundHub").build();
var connection = new signalR.HubConnectionBuilder().withUrl("/apps/crowdfund/hub").build();
connection.onclose(function(){
this.status = statuses.DISCONNECTED;
eventAggregator.$emit("connection-lost");
console.error("Connection was closed. Attempting reconnect in 2s");
setTimeout(connect, 2000);
});
connection.on("InvoiceCreated", function(invoiceId){
eventAggregator.$emit("invoice-created", invoiceId);
});
function connect(){
status = statuses.CONNECTING;
eventAggregator.$emit("connection-pending");
connection
.start()
.then(function(){
this.status = statuses.CONNECTED;
connection.invoke("ListenToCrowdfundApp", srvModel.appId);
})
.catch(function (err) {
this.status = statuses.DISCONNECTED;
eventAggregator.$emit("connection-failed");
console.error("Could not connect to backend. Retrying in 2s", err );
setTimeout(connect, 2000);
});
}
eventAggregator.$on("contribute", function(model){
connection.invoke("CreateInvoice", model);
});
return {
statuses: statuses,
status: status,
connect: connect
};
}();