2017-07-13 27 views
0

当SignalR更新通过我们的页面来更新我们的模式时,我们的项目名称会改变,我们的脚本似乎会中断。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。该视图模型包含错误名称: 上的jsfiddle https://jsfiddle.net/wmqdyv8r/

这是我们的角控制台错误可视:

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. 

这Highcharts错误显示出来,如果我们尝试SignalR刷新后打开图表:

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 

主要问题是更新事件正在破坏。命名问题是一个较低的优先级,虽然我确定它是相关的。

+0

阅读[如何创建一个最小的,完整的和可验证的示例](HTTPS:/ /stackoverflow.com/help/mcve)。您可以修剪您的代码并创建一个实例在jfiddle上? – morganfree

+0

刚刚在https://jsfiddle.net/wmqdyv8r/添加了JSFiddle,虽然没有C#它不会完全工作,但它应该让别人更好地了解发生了什么。 此处还添加了服务器端的SignalR代码,以显示更好的细节。 – Steveo

回答

0

发现并修复了问题!

发现格式错误的名称总是会修剪出真实名称的前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(), 
};