NancyFx框架之检测任务管理器

先建一个空的项目和之前的NancyFx系列一样的步骤

NancyFx框架之检测任务管理器_第1张图片

 

NancyFx框架之检测任务管理器_第2张图片

然后建三个文件夹Models,Module,Views

NancyFx框架之检测任务管理器_第3张图片

然后分别安装一下组件

  • jQuery
  • Microsoft.AspNet.SignalR
  • Microsoft.Owin
  • Nancy
  • Nancy.Owin

然后往Model类里面添加CPUHub类,Broadcaster类

CPUHub类

NancyFx框架之检测任务管理器_第4张图片

 

 1  public class CPUHub:Hub
 2     {
 3         private readonly Broadcaster broadcaster;
 4         public CPUHub():this(Broadcaster.broadcaster)
 5         {
 6 
 7         }
 8         public CPUHub(Broadcaster broadcaster)
 9         {
10             this.broadcaster = broadcaster;
11         }
12     }
View Code

 

Broadcaster类

NancyFx框架之检测任务管理器_第5张图片

 

 1  public class Broadcaster
 2     {
 3         private readonly static Lazy lazy = new Lazy(()=>new Broadcaster(GlobalHost.ConnectionManager.GetHubContext().Clients));
 4 
 5         private readonly TimeSpan timeSpan = TimeSpan.FromMilliseconds(1000);
 6         private readonly Timer timer;
 7         public static Broadcaster broadcaster
 8         {
 9             get { return lazy.Value; }
10         }
11         private IHubConnectionContext hubConnectionContext
12         {
13             get;
14             set;
15         }
16         private Broadcaster(IHubConnectionContext hubConnectionContexts)
17         {
18             hubConnectionContext = hubConnectionContexts;
19             timer = new Timer(BroadcastCpuUsage,null,timeSpan,timeSpan);
20         }
21         private void BroadcastCpuUsage(object o)
22         {
23             string cpu = GetCurrentCpu();
24 
25         }
26         private string GetCurrentCpu()
27         {
28             string currentCpu = "";
29             HttpClient httpClient = new HttpClient();
30             httpClient.BaseAddress = new Uri("http://localhost:3039");
31             var response = httpClient.GetAsync("api/cpu").Result;
32             if (response.IsSuccessStatusCode)
33             {
34                 currentCpu = response.Content.ReadAsStringAsync().Result;
35             }
36             return currentCpu;
37         }
38     }

 

然后在往Module里面添加 CPUModule类

 1  public class CPUModule:NancyModule
 2     {
 3         PerformanceCounter performanceCounter;
 4         public CPUModule():base("api/cpu")
 5         {
 6             InitializePerformanceCounter();
 7             Get("/",Lexan=> 
 8             {
 9                 int cpu = (int)Math.Ceiling(performanceCounter.NextValue());
10                 return Response.AsText(cpu.ToString());
11             });
12         }
13         private void InitializePerformanceCounter()
14         {
15             performanceCounter = new PerformanceCounter();
16             performanceCounter.CategoryName = "";
17             performanceCounter.CounterName = "";
18             performanceCounter.InstanceName = "";
19             performanceCounter.NextValue();
20             Thread.Sleep(1000);
21         }
22     }

NancyFx框架之检测任务管理器_第6张图片

 

 

 

然后添加index.html页面在根目录下

NancyFx框架之检测任务管理器_第7张图片

 1 
 2 
 3 
 4     NancyTaskManager
 5 
 6 
 7     
 8     
9 "cvPercentage"> 10
11
12 "cvGraph" height="450" width="600"> 13 14 15 16 17 18 19

继续往根目录里面添加Startup类

 1 [assembly:OwinStartup(typeof( NancyFxTaskManager.Startup))]
 2 namespace NancyFxTaskManager
 3 {
 4     public class Startup
 5     {
 6         public void Configuration(IAppBuilder app)
 7         {
 8             app.MapSignalR().UseNancy();
 9             
10         }
11     }
12 }

NancyFx框架之检测任务管理器_第8张图片

 

 

好了我们准备就绪,看看运行效果

NancyFx框架之检测任务管理器_第9张图片

感谢各位dalao的观看,如有不是,多多指教

你可能感兴趣的:(NancyFx框架之检测任务管理器)