欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-14 14:17:10
本文为大家分享了nancyfx框架检测任务管理器的具体方法,供大家参考,具体内容如下 先建一个空的项目和之前的nancyfx系列一样的步骤 然后建三个文件夹mo...

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

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

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

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

然后建三个文件夹models,module,views

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

然后分别安装一下组件

jquery

microsoft.aspnet.signalr

microsoft.owin

nancy

nancy.owin

然后往model类里面添加cpuhub类,broadcaster类

cpuhub类

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

public class cpuhub:hub
  {
    private readonly broadcaster broadcaster;
    public cpuhub():this(broadcaster.broadcaster)
    {

    }
    public cpuhub(broadcaster broadcaster)
    {
      this.broadcaster = broadcaster;
    }
  }

broadcaster类

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

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);
    }
  }

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

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

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

<!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();
      
    }
  }
}

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

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

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

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

上一篇:

下一篇: