NancyFx框架之检测任务管理器
2018-06-22 07:53:34来源:未知 阅读 ()
先建一个空的项目和之前的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 }
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报表显示图片
下一篇:我的收藏之数据库优化
- 详解ABP框架中Session功能的使用方法 2020-01-18
- 详解ABP框架中的数据过滤器与数据传输对象的使用 2020-01-18
- 解析ABP框架中的日志功能 2019-11-27
- 详解ABP框架的参数有效性验证和权限验证 2019-11-25
- 详解ASP.NET样板项目ABP框架的特性 2019-11-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash