ChartHub.cs
2.06 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using HslCommunication.Enthernet;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;
using WebApp;
namespace WebMvc
{
public class ChartHub : Hub
{
private static Broadcaster _broadcaster;
public ChartHub(IHubContext<ChartHub> hubContext, IOptions<AppSetting> appConfiguration)
{
if (_broadcaster == null)
{
_broadcaster = new Broadcaster(hubContext, appConfiguration.Value.NetPushServer, appConfiguration.Value.NetPushPort);
}
}
public override Task OnConnectedAsync()
{
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
return base.OnDisconnectedAsync(exception);
}
}
/// <summary>
/// 数据广播器
/// </summary>
public class Broadcaster
{
private readonly IHubContext<ChartHub> _hubContext;
private NetPushClient pushClient;
public Broadcaster(IHubContext<ChartHub> hubContext, string netPushServer, int netPushPort)
{
// 获取所有连接的句柄,方便后面进行消息广播
_hubContext = hubContext;
// 实例化一个数据
pushClient = new NetPushClient(netPushServer, netPushPort, "STK");
pushClient.CreatePush(StkNetPushCallBack);
// 实例化一个数据
pushClient = new NetPushClient(netPushServer, netPushPort, "AGV");
pushClient.CreatePush(AgvNetPushCallBack);
}
private void StkNetPushCallBack(NetPushClient pushClient, string str)
{
JArray json = JArray.Parse(str);
_hubContext.Clients.All.SendAsync("sendStkData", json);
}
private void AgvNetPushCallBack(NetPushClient pushClient, string str)
{
JArray json = JArray.Parse(str);
_hubContext.Clients.All.SendAsync("sendAgvData", json);
}
}
}