Browse Source

添加SignalR

master
wanggang 2 years ago
parent
commit
e5e2383ce5
  1. 17
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Controllers/HomeController.cs
  2. 4
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs
  3. 4
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/router/index.js
  4. 30
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js
  5. 22
      code/src/Modules/SettleAccount/src/SettleAccount.Job/SignalR/PageHub.cs

17
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; using Volo.Abp.AspNetCore.Mvc;
namespace Win.Sfs.SettleAccount.Controllers namespace Win.Sfs.SettleAccount.Controllers
{ {
public class HomeController : AbpController public class HomeController : AbpController
{ {
private readonly IHubContext<PageHub> _hubContext;
public HomeController(IHubContext<PageHub> hubContext)
{
this._hubContext = hubContext;
}
[ResponseCache(NoStore = true)] [ResponseCache(NoStore = true)]
public ActionResult Index() public ActionResult Index()
{ {
return File("~/index.html", "text/html"); return File("~/index.html", "text/html");
} }
public IActionResult Test()
{
this._hubContext.Clients.All.ServerToClient("test", "hello", "");
return Json("ok");
}
} }
} }

4
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.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NUglify.Helpers; using NUglify.Helpers;
using SettleAccount.Job.SignalR;
using Win.Sfs.SettleAccount.Entities.BQ.Vmi; using Win.Sfs.SettleAccount.Entities.BQ.Vmi;
namespace Win.Sfs.SettleAccount; namespace Win.Sfs.SettleAccount;
@ -19,6 +20,7 @@ public class Startup
{ {
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddSignalR(o => o.EnableDetailedErrors=true);
AppDomain.CurrentDomain.GetAssemblies().SelectMany(o => o.GetTypes()) AppDomain.CurrentDomain.GetAssemblies().SelectMany(o => o.GetTypes())
.Where(o => o.IsClass && !o.IsAbstract && o.IsAssignableTo(typeof(IJobService))) .Where(o => o.IsClass && !o.IsAbstract && o.IsAssignableTo(typeof(IJobService)))
.ForEach(o => services.AddTransient(o)); .ForEach(o => services.AddTransient(o));
@ -41,6 +43,8 @@ public class Startup
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{ {
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapHub<PageHub>("/api/hub"));
app.ApplicationServices.UseScheduler(scheduler => app.ApplicationServices.UseScheduler(scheduler =>
{ {
using var scope = app.ApplicationServices.CreateScope(); using var scope = app.ApplicationServices.CreateScope();

4
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 { isLogin, hasPermission } from "../api/user.js";
import { useAppStore } from "../store/index.js"; import { useAppStore } from "../store/index.js";
import { listToTree } from "../utils/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"; import remoteRoutes from "./routes.js";
NProgress.configure({ showSpinner: false }); NProgress.configure({ showSpinner: false });
@ -82,7 +82,7 @@ router.afterEach((to) => {
}); });
const refreshRouter = async () => { const refreshRouter = async () => {
//await connect(); await connect();
const appStore = useAppStore(); const appStore = useAppStore();
const permissions = appStore.user.permissions; const permissions = appStore.user.permissions;
const serverRoutes = JSON.parse(JSON.stringify(remoteRoutes)); const serverRoutes = JSON.parse(JSON.stringify(remoteRoutes));

30
code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/signalr/index.js

@ -13,20 +13,20 @@ const connection = new signalR.HubConnectionBuilder()
//.withAutomaticReconnect() //.withAutomaticReconnect()
.build(); .build();
const connect = async () => { const connect = async () => {
// if (await isLogin()) { if (await isLogin()) {
// if (connection.state === signalR.HubConnectionState.Disconnected) { if (connection.state === signalR.HubConnectionState.Disconnected) {
// connection connection
// .start() .start()
// .then(function () { .then(function () {
// console.log("signalr connected"); console.log("signalr connected");
// }) })
// .catch(async function (error) { .catch(async function (error) {
// console.log(error); console.log(error);
// await isLogin(); await isLogin();
// setTimeout(connect, 5000); setTimeout(connect, 5000);
// }); });
// } }
// } }
}; };
connection.onclose(async () => { connection.onclose(async () => {
await connect(); await connect();
@ -38,4 +38,4 @@ connection.on("ServerToClient", (method, data) => {
PubSub.publish(method, data); PubSub.publish(method, data);
}); });
export { connection, connect }; export { connection, connect, connectionId };

22
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);
}
}
}
Loading…
Cancel
Save