本文为大家分享了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 Lazy<broadcaster> lazy = new Lazy<broadcaster>(()=>new broadcaster(GlobalHost.ConnectionManager.GetHubContext<cpuHub>().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页面在根目录下
<!DOCTYPE HTML><HTML><head> <Title>NancyTaskManager</Title></head><body> <label ID="lblVal"></label> <br /> <canvas ID="cvPercentage"></canvas> <br /> <br /> <canvas ID="cvGraph" height="450" wIDth="600"></canvas> <script src="Scripts/jquery-2.1.0.Js"></script> <script src="Scripts/jquery.signalR-2.0.2.Js"></script> <script src="Scripts/Chart.Js"></script> <script src="/signalr/hubs"></script> <script src="Scripts/taskManager.Js"></script></body></HTML>
继续往根目录里面添加Startup类
[assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))]namespace NancyFxTaskManager{ public class Startup { public voID Configuration(IAppBuilder app) { app.MapSignalR().UseNancy(); } }}
好了我们准备就绪,看看运行效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的NancyFx框架检测任务管理器详解全部内容,希望文章能够帮你解决NancyFx框架检测任务管理器详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)