NancyFx框架之检测任务管理器

2018-06-22 07:53:34来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

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

然后分别安装一下组件

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

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

CPUHub类

 

 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类

 

 1  public class Broadcaster
 2     {
 3         private readonly static Lazy<Broadcaster> lazy = new Lazy<Broadcaster>(()=>new Broadcaster(GlobalHost.ConnectionManager.GetHubContext<CPUHub>().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     }

 

 

 

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

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>NancyTaskManager</title>
 5 </head>
 6 <body>
 7     <label id="lblVal"></label>
 8     <br />
 9     <canvas id="cvPercentage"></canvas>
10     <br />
11     <br />
12     <canvas id="cvGraph" height="450" width="600"></canvas>
13     <script src="Scripts/jquery-2.1.0.js"></script>
14     <script src="Scripts/jquery.signalR-2.0.2.js"></script>
15     <script src="Scripts/Chart.js"></script>
16     <script src="/signalr/hubs"></script>
17     <script src="Scripts/taskManager.js"></script>
18 </body>
19 </html>

继续往根目录里面添加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 }

 

 

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

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:RDLC报表显示图片

下一篇:我的收藏之数据库优化