簡體   English   中英

SignalR更新中斷頁面上的布局和腳本

[英]SignalR update breaks layout and scripts on page

當SignalR更新進入我們的頁面以更新我們的模態時,我們的項目名稱會更改,我們的腳本似乎已損壞。

簡要概述:我們的SignalR更新可以很好地發送到網站,但是數據本身的名稱無效。 更新后,我們的商品會以格式錯誤的名稱刷新。 首先,SignalR不應更新我們的名稱,而且我似乎在我們的代碼中找不到對該名稱的任何引用。 關閉模式,我們的Highcharts和Angular腳本會引發控制台錯誤。

服務器端代碼:

public partial class Device
{
 if (device != null)
 {
  if ((Enumerables.DeviceType)device.Type == Enumerables.DeviceType.Store)
   SignalrClient.UpdateStore(device.DeviceID);
  else // check if need to update a modal on the dashboard
  {
   foreach (var key in SignalrClient.DevicesDictionary.Keys)
   {
    var devices = SignalrClient.DevicesDictionary[key];
    if (devices != null)
    {
     if (devices.Contains(device.DeviceID))
      SignalrClient.UpdateModal(key, device.DeviceID);
    }
   }
  }
 }
}

class SignalrClient
{
 public static async Task Start()
 {
  if (_hubConnection == null || _hubConnection.State == ConnectionState.Disconnected)
  {
   _hubConnection = new HubConnection("http://stevessiteofamazingboats.net/");
   _dashboardHubProxy = _hubConnection.CreateHubProxy("DashboardHub");
   _dashboardHubProxy.On("OnRegisterDevice", new Action<string, int>(OnRegisterDevice));
   _dashboardHubProxy.On("OnDeregisterDevices", new Action<string>(OnDeregisterDevices));
   _dashboardHubProxy.On("OnDeregisterDevice", new Action<string, int>(OnDeregisterDevice));
   await _hubConnection.Start();
  }
 }
 public static async void UpdateModal(string connectionId, int deviceId)
 {
  await Start();
  if (_hubConnection.State == ConnectionState.Connected)
  await _dashboardHubProxy.Invoke("UpdateModal", new object[] { connectionId, deviceId });
  }
}

public class DashboardHub : Hub
{
 private static string EventHubConnectionId {get;set;}
 private AlarmDBEntities db = Utils.DbContext;

 public void UpdateModal(string connectionId, int deviceId)
 {
  var db = Utils.DbContext;
  var device = db.Device.Find(deviceId);
  var modal = new Portal.DeviceModalViewModel()
  {
   DeviceId = deviceId,
   SuctionGroups = device.Device1.Where(x => (Enumerables.DeviceType)x.Type == Enumerables.DeviceType.SuctionGroup).Select(x => new DeviceModalViewModel.SGNode()
   {
   SubChildren = x.Device1.Where(y => (Enumerables.DeviceType)y.Type == Enumerables.DeviceType.Compressor).Select(y => new DeviceModalViewModel.DeviceNode()
    {
    DeviceId = y.DeviceID,
    Name = y.Name,
    Amp = db.Property.Where(z => z.Name == "Amps" && z.DeviceID == y.DeviceID).OrderByDescending(z => z.CreatedOn).Select(z => z.Value).FirstOrDefault()
   }).OrderBy(y => y.Name).ToList()
  }).OrderBy(x => x.Name).ToList(),
 };
}

客戶端javascript。 viewModel包含格式錯誤的名稱:在JSFiddle上可查看https://jsfiddle.net/wmqdyv8r/

這是我們的Angular控制台錯誤:

angular.min.js:6 Uncaught Error: [ng:areq]    
http://errors.angularjs.org/1.5.7/ng/areq?
p0=HeaderController&p1=not%20a%20function%2C%20got%20undefined
at angular.min.js:6
at sb (angular.min.js:22)
at Qa (angular.min.js:23)
at angular.min.js:89
at ag (angular.min.js:72)
at m (angular.min.js:64)
at g (angular.min.js:58)
at g (angular.min.js:58)
at g (angular.min.js:58)
at g (angular.min.js:58)

angular.min.js:312 WARNING: Tried to load angular more than once.

如果在SignalR刷新后嘗試打開圖表,則會顯示此Highcharts錯誤:

store.js:856 Uncaught TypeError: $(...).highcharts is not a function
at Object.success (store.js:856)
at c (<anonymous>:1:132617)
at Object.fireWith [as resolveWith] (<anonymous>:1:133382)
at b (<anonymous>:1:168933)
at XMLHttpRequest.<anonymous> (<anonymous>:1:173769)

同樣,在關閉模態之后,我們的主頁將刷新,現在拋出此錯誤:

Exception: Sequence contains no elements
Type: System.InvalidOperationException

主要問題是更新事件正在破壞某些東西。 命名問題的優先級較低,盡管我確定這是相關的。

找到並解決了問題!

發現格式錯誤的名稱始終會在真實姓名的前5個字符處進行剪裁,因此我將其固定在底部附近,盡管我仍然不知道剪裁發生的位置。

更嚴重的問題是,腳本中斷也已解決。 在UpdateModal方法中,腳本之一正在尋找storeID字段以及deviceID字段。 在將日志打印到Chrome javascript控制台后,我可以看到storeID始終返回0,即使先前已在UpdateModal方法之前對其進行了初始化。

我要做的就是 跟隨該死的火車 在DeviceId下添加在此處看到的storeID字段:

public void UpdateModal(string connectionId, int deviceId)
{
 var db = Utils.DbContext;
 var device = db.Device.Find(deviceId);
 var modal = new Portal.DeviceModalViewModel()
 {
  DeviceId = deviceId,
  *THIS*-> StoreId = db.Device.Where(x => device.Name.Contains(x.Name.Replace("-Store", "")) && x.ParentID == null).Select(x => x.DeviceID).FirstOrDefault(),
  SuctionGroups = device.Device1.Where(x => (Enumerables.DeviceType)x.Type == 
  Enumerables.DeviceType.SuctionGroup).Select(x => new 
  DeviceModalViewModel.SGNode()
  {
   SubChildren = x.Device1.Where(y => (Enumerables.DeviceType)y.Type == 
   Enumerables.DeviceType.Compressor).Select(y => new 
   DeviceModalViewModel.DeviceNode()
   {
    DeviceId = y.DeviceID,
    *ALSO THIS*-> Name = "12345Comp " + y.Name.Substring(y.Name.Length - 2),
    Amp = db.Property.Where(z => z.Name == "Amps" && z.DeviceID == y.DeviceID).OrderByDescending(z => z.CreatedOn).Select(z => z.Value).FirstOrDefault()
    }).OrderBy(y => y.Name).ToList()
   }).OrderBy(x => x.Name).ToList(),
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM