diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Controllers/HomeController.cs b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Controllers/HomeController.cs index 98f7010f..b234e987 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Controllers/HomeController.cs +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Controllers/HomeController.cs @@ -1,14 +1,29 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using SettleAccount.Job.SignalR; using Volo.Abp.AspNetCore.Mvc; namespace Win.Sfs.SettleAccount.Controllers { public class HomeController : AbpController { + private readonly IHubContext _hubContext; + + public HomeController(IHubContext hubContext) + { + this._hubContext = hubContext; + } + [ResponseCache(NoStore = true)] public ActionResult Index() { return File("~/index.html", "text/html"); } + + public IActionResult Test() + { + this._hubContext.Clients.All.ServerToClient("test", "hello", ""); + return Json("ok"); + } } } diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs index 1fb49eea..69545588 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NUglify.Helpers; +using SettleAccount.Job.SignalR; using Win.Sfs.SettleAccount.Entities.BQ.Vmi; namespace Win.Sfs.SettleAccount; @@ -19,6 +20,7 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { + services.AddSignalR(o => o.EnableDetailedErrors=true); AppDomain.CurrentDomain.GetAssemblies().SelectMany(o => o.GetTypes()) .Where(o => o.IsClass && !o.IsAbstract && o.IsAssignableTo(typeof(IJobService))) .ForEach(o => services.AddTransient(o)); @@ -41,6 +43,8 @@ public class Startup public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { + app.UseRouting(); + app.UseEndpoints(endpoints => endpoints.MapHub("/api/hub")); app.ApplicationServices.UseScheduler(scheduler => { using var scope = app.ApplicationServices.CreateScope(); diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/router/index.js b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/router/index.js index 61294c3d..e2ccb1c4 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/router/index.js +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/router/index.js @@ -5,7 +5,7 @@ import NProgress from "../lib/nprogress/nprogress.vite-esm.js"; import { isLogin, hasPermission } from "../api/user.js"; import { useAppStore } from "../store/index.js"; import { listToTree } from "../utils/index.js"; -import { connection, connect } from "../signalr/index.js"; +import { connection, connect, connectionId } from "../signalr/index.js"; import remoteRoutes from "./routes.js"; NProgress.configure({ showSpinner: false }); @@ -82,7 +82,7 @@ router.afterEach((to) => { }); const refreshRouter = async () => { - //await connect(); + await connect(); const appStore = useAppStore(); const permissions = appStore.user.permissions; const serverRoutes = JSON.parse(JSON.stringify(remoteRoutes)); diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js index 6a1de7fe..4580e1ba 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js @@ -13,20 +13,20 @@ const connection = new signalR.HubConnectionBuilder() //.withAutomaticReconnect() .build(); const connect = async () => { - // if (await isLogin()) { - // if (connection.state === signalR.HubConnectionState.Disconnected) { - // connection - // .start() - // .then(function () { - // console.log("signalr connected"); - // }) - // .catch(async function (error) { - // console.log(error); - // await isLogin(); - // setTimeout(connect, 5000); - // }); - // } - // } + if (await isLogin()) { + if (connection.state === signalR.HubConnectionState.Disconnected) { + connection + .start() + .then(function () { + console.log("signalr connected"); + }) + .catch(async function (error) { + console.log(error); + await isLogin(); + setTimeout(connect, 5000); + }); + } + } }; connection.onclose(async () => { await connect(); @@ -38,4 +38,4 @@ connection.on("ServerToClient", (method, data) => { PubSub.publish(method, data); }); -export { connection, connect }; +export { connection, connect, connectionId }; diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/SignalR/PageHub.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/SignalR/PageHub.cs new file mode 100644 index 00000000..ebcdbfc8 --- /dev/null +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/SignalR/PageHub.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; + +namespace SettleAccount.Job.SignalR +{ + public class PageHub : Hub + { + public override async Task OnConnectedAsync() + { + await this.Groups.AddToGroupAsync(this.Context.ConnectionId, this.Context.ConnectionId).ConfigureAwait(false); + await Clients.Group(Context.ConnectionId).SendAsync("Connected", Context.ConnectionId).ConfigureAwait(false); + } + } + + public static class HubExtensions + { + public static void ServerToClient(this IClientProxy clientProxy, string method, string message, string toClient, string? fromClient = null) + { + clientProxy.SendAsync(nameof(ServerToClient), method, message, toClient, fromClient); + } + } +}