最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
NancyFx框架检测任务管理器详解
时间:2022-06-25 07:46:29 编辑:袖梨 来源:一聚教程网
本文为大家分享了NancyFx框架检测任务管理器的具体方法,供大家参考,具体内容如下
先建一个空的项目和之前的NancyFx系列一样的步骤
然后建三个文件夹Models,Module,Views
然后分别安装一下组件
jQuery
Microsoft.AspNet.SignalR
Microsoft.Owin
Nancy
Nancy.Owin
然后往Model类里面添加CPUHub类,Broadcaster类
CPUHub类
public class CPUHub:Hub { private readonly Broadcaster broadcaster; public CPUHub():this(Broadcaster.broadcaster) { } public CPUHub(Broadcaster broadcaster) { this.broadcaster = broadcaster; } }
Broadcaster类
public class Broadcaster { private readonly static Lazylazy = new Lazy (()=>new Broadcaster(GlobalHost.ConnectionManager.GetHubContext ().Clients)); private readonly TimeSpan timeSpan = TimeSpan.FromMilliseconds(1000); private readonly Timer timer; public static Broadcaster broadcaster { get { return lazy.Value; } } private IHubConnectionContext hubConnectionContext { get; set; } private Broadcaster(IHubConnectionContext hubConnectionContexts) { hubConnectionContext = hubConnectionContexts; timer = new Timer(BroadcastCpuUsage,null,timeSpan,timeSpan); } private void BroadcastCpuUsage(object o) { string cpu = GetCurrentCpu(); } private string GetCurrentCpu() { string currentCpu = ""; HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("http://localhost:3039"); var response = httpClient.GetAsync("api/cpu").Result; if (response.IsSuccessStatusCode) { currentCpu = response.Content.ReadAsStringAsync().Result; } return currentCpu; } }
然后在往Module里面添加CPUModule类
public class CPUModule:NancyModule { PerformanceCounter performanceCounter; public CPUModule():base("api/cpu") { InitializePerformanceCounter(); Get("/",Lexan=> { int cpu = (int)Math.Ceiling(performanceCounter.NextValue()); return Response.AsText(cpu.ToString()); }); } private void InitializePerformanceCounter() { performanceCounter = new PerformanceCounter(); performanceCounter.CategoryName = ""; performanceCounter.CounterName = ""; performanceCounter.InstanceName = ""; performanceCounter.NextValue(); Thread.Sleep(1000); } }
然后添加index.html页面在根目录下
NancyTaskManager
继续往根目录里面添加Startup类
[assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))] namespace NancyFxTaskManager { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR().UseNancy(); } } }
好了我们准备就绪,看看运行效果
相关文章
- 问剑长生新手怎么快速跟上战斗力 怎么零氪追赶战力 08-09
- 绝区零伊芙琳怎么样-绝区零伊芙琳角色介绍 08-09
- 山海奇闻2怎么快速升级-山海奇闻2快速升级方法分享 08-09
- 燕云十六声抢马任务通关-燕云十六声怎么抢马 08-09
- 梦幻西游梦幻山河画境巫蛊如何打-梦幻西游梦幻山河画境巫蛊打法公布 08-09
- 心渊梦境火之领域攻略-心渊梦境火之领域玩法讲解 08-09