本文为大家分享了nancyfx框架检测任务管理器的具体方法,供大家参考,具体内容如下
先建一个空的项目和之前的nancyfx系列一样的步骤
然后建三个文件夹models,module,views
然后分别安装一下组件
jquery
microsoft.aspnet.signalr
microsoft.owin
nancy
nancy.owin
然后往model类里面添加cpuhub类,broadcaster类
cpuhub类
1
2
3
4
5
6
7
8
9
10
11
12
|
public class cpuhub:hub { private readonly broadcaster broadcaster; public cpuhub(): this (broadcaster.broadcaster) { } public cpuhub(broadcaster broadcaster) { this .broadcaster = broadcaster; } } |
broadcaster类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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页面在根目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!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类
1
2
3
4
5
6
7
8
9
10
11
12
|
[assembly:owinstartup( typeof ( nancyfxtaskmanager.startup))] namespace nancyfxtaskmanager { public class startup { public void configuration(iappbuilder app) { app.mapsignalr().usenancy(); } } } |
好了我们准备就绪,看看运行效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/R00R/archive/2017/10/25/7733037.html