前台关闭iframe页面时把iframe从内存中清理
var iframe = $('#' + iframeID).prop('contentWindow');
$('#' + iframeID).attr('src', 'about:blank');
try {
iframe.document.write('');
iframe.document.clear();
} catch (e) { }
//把iframe从页面移除
$('#' + iframeID).remove();
后台使用SignalR Hub的关闭连接事件,线程取消 CancellationTokenSource 对象一定要使用静态变量
using HenryMes.Model;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace HenryMes.WebProtal.SignalR.OEE.Equipment
{
[HubName("OEEEquipmentHub")]
public class OEEEquipmentHub : Hub
{
private static CancellationTokenSource tokenSource = new CancellationTokenSource();
private static CancellationToken token;
/// <summary>
///
/// </summary>
/// <param name="connectId">连接ID</param>
/// <param name="id">设备ID</param>
public void Send(string connectId, string id)
{
if (string.IsNullOrEmpty(id))
return;
if (tokenSource.IsCancellationRequested)
{
tokenSource = new CancellationTokenSource();
}
token = tokenSource.Token;
Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
try
{
var model = new OEEEquipmentModel();
Random rnd = new Random(System.Guid.NewGuid().GetHashCode());
//完成率
model.wan_cheng_lu = rnd.Next(10, 90).ToString();
//总在线时间
model.zong_zai_xian_shi_jian = rnd.Next(10, 90).ToString();
//总故障时间
model.zong_gu_zhang_shi_jian = rnd.Next(10, 90).ToString();
//计划停机时间
model.ji_hua_ting_ji_shi_jian = rnd.Next(10, 90).ToString();
//理想运行时间
model.li_xiang_yun_xing_shi_jian = rnd.Next(10, 90).ToString();
//总运行时间
model.zong_yun_xing_shi_jian = rnd.Next(10, 90).ToString();
//实际运行时间
model.shi_ji_yun_xing_shi_jian = rnd.Next(10, 90).ToString();
//可用率
model.ke_yong_lu = rnd.Next(10, 90).ToString();
//推送到前端
OEEEquipmentNotifier.RefreshEquipment(connectId, model);
}
catch (Exception ex)
{
}
//2秒推送一次
Thread.Sleep(2000);
}
}, token);
}
public override Task OnDisconnected(bool stopCalled)
{
if (_connections.Contains(Context.ConnectionId))
{
_connections.Remove(Context.ConnectionId);
}
try
{
if (_connections.Count == 0)
{
_tokenSource.Cancel();
_tokenSource.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
return base.OnDisconnected(stopCalled);
}
}
}
前台使用SignalR建立连接
var myHub = $.connection.OEEFactoryHub;
$.connection.hub.stop();
$.connection.hub.start().done(function () {
//客户端发送信息到服务器
myHub.server.send($.connection.hub.id);
});
前台接收后台推送过来的数据,并注册关闭页面的事件(在页面关闭时关闭SignalR)
//后端设备信息推送到前端
myHub.client.RefreshFactory = function (message) {
//总在线时间
app.zong_zai_xian_shi_jian = message.zong_zai_xian_shi_jian;
//总故障时间
app.zong_gu_zhang_shi_jian = message.zong_gu_zhang_shi_jian;
//计划停机时间
app.ji_hua_ting_ji_shi_jian = message.ji_hua_ting_ji_shi_jian;
//理想运行时间
app.li_xiang_yun_xing_shi_jian = message.li_xiang_yun_xing_shi_jian;
//总运行时间
app.zong_yun_xing_shi_jian = message.zong_yun_xing_shi_jian;
//实际运行时间
app.shi_ji_yun_xing_shi_jian = message.shi_ji_yun_xing_shi_jian;
//可用率
app.ke_yong_lu = message.ke_yong_lu;
//设备数量
app.she_bei_shu_liang = message.she_bei_shu_liang;
//停机数量
app.ting_ji_shu_liang = message.ting_ji_shu_liang;
//故障数量
app.gu_zhang_shu_liang = message.gu_zhang_shu_liang;
//运行数量
app.yun_xing_shu_liang = message.yun_xing_shu_liang;
app.echartsWorkshopProduction();
};
window.onbeforeunload = function (e) {
$.connection.hub.stop();
};