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

发布时间:2020-10-23 11:06:02 作者:Lexan
来源:脚本之家 阅读:167

本文为大家分享了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框架检测任务管理器详解

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

推荐阅读:
  1. python框架flask表单实现详解
  2. 人脸检测中AdaBoost算法详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nancyfx 任务管理器

上一篇:js-tab选项卡

下一篇:layer弹出层全屏及关闭方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》