好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

NancyFx框架检测任务管理器详解

本文为大家分享了nancyfx框架检测任务管理器的具体方法,供大家参考,具体内容如下

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

然后建三个文件夹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 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类

?

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页面在根目录下

?

<!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类

?

[assembly:owinstartup( typeof ( nancyfxtaskmanager.startup))]

namespace nancyfxtaskmanager

{

   public class startup

   {

     public void configuration(iappbuilder app)

     {

       app.mapsignalr().usenancy();

      

     }

   }

}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://HdhCmsTestcnblogs测试数据/R00R/archive/2017/10/25/7733037.html

dy("nrwz");

查看更多关于NancyFx框架检测任务管理器详解的详细内容...

  阅读:44次