本文為大家分享了NancyFx框架檢測(cè)任務(wù)管理器的具體方法,供大家參考,具體內(nèi)容如下
調(diào)兵山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
先建一個(gè)空的項(xiàng)目和之前的NancyFx系列一樣的步驟
然后建三個(gè)文件夾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頁(yè)面在根目錄下
NancyTaskManager
繼續(xù)往根目錄里面添加Startup類
[assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))] namespace NancyFxTaskManager { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR().UseNancy(); } } }
好了我們準(zhǔn)備就緒,看看運(yùn)行效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。