Browse Source

添加SignalR

master
wanggang 1 year 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;
namespace Win.Sfs.SettleAccount.Controllers
{
public class HomeController : AbpController
{
private readonly IHubContext<PageHub> _hubContext;
public HomeController(IHubContext<PageHub> 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");
}
}
}

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.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<PageHub>("/api/hub"));
app.ApplicationServices.UseScheduler(scheduler =>
{
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 { 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));

30
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 };

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