我有一个Web Api服务,它将signalR通知发送到我在同一个盒子上的MVC网站上运行的代理。经过大量调整,我已经让web服务与网站上的集线器进行了交谈,并通过调试日志进行了确认。
在网站方面,似乎一切都很好,它似乎引用了我的客户端浏览器来调用网站。下面我将粘贴Hub类和调试日志输出来证明这一点。
最后一行是
Clients.Client(conn).addNotification(notification);
然后我假设它会查找与该签名匹配的JavaScript方法。我有一个。下面是我的JS文件中的方法。
notifyProxy.client.addNotification = function (notification) {
$notificationTableBody.prepend(rowTemplate.supplant(formatNotification(notification)));
};
然而,设置了一个断点(使用chrome开发工具),该方法永远不会被调用。MVC网站上没有任何错误报告,开发人员工具控制台中也没有错误报告,尽管我不知道SignalR是否在其他地方写了错误。
唯一需要注意的是,在JS文件中,我有一行
$.connection.hub.start().done(init);
当我在Chrome开发工具中设置断点时会调用它。但是,init()方法从未被调用。这是否意味着hub.start()方法可能会失败?如果是的话,我该如何找出原因?
出了什么问题?我对信号员还很陌生,所以我疯了!
我的集线器类的代码和下面的调试输出
public void PushNotification(EventNotification notification, List<string> users)
{
_logger.WriteDebug("Entered push notification");
if (notification.FriendlyText.Length > 1000)
notification.FriendlyText = notification.FriendlyText.Substring(0, 1000) + "...";
if (users.Contains("*") && users.Count > 1)
{
users.RemoveAll(u => !u.Equals("*"));
}
foreach (var userName in users)
{
_logger.WriteDebug("Push notification for " + userName);
UserConnectionInformation user = null;
if (UserInformation.TryGetValue(userName.ToUpperInvariant(), out user))
{
_logger.WriteDebug("Adding " + notification.FriendlyText + ", " + user.ConnectionIds.Count + " connections");
user.Notifications.Add(notification);
lock (user.ConnectionIds)
{
foreach (var conn in user.ConnectionIds)
{
_logger.WriteDebug("Is Client null - {0}", Clients.Client(conn) == null);
_logger.WriteDebug("Conn {0}", conn);
var proxy = Clients.Client(conn) as ConnectionIdProxy;
_logger.WriteDebug("proxy {0}", proxy.ToString());
Clients.Client(conn).addNotification(notification);
_logger.WriteDebug("Added notification ");
}
}
}
}
}
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Entered push notification
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Push notification for kerslaj1
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Push notification for UKkerslaj1
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Adding Job 'testrose45job-11dec15121944' ('ROSE-0000045') cancellation has been requested by 'UKkerslaj1' on 'seflexpool3'., 1 connections
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Is Client null - False
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Conn 3d008947-0d96-4965-bb01-dfd1517c24a5
11-12-2015 12:19:43,808 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - proxy Microsoft.AspNet.SignalR.Hubs.ConnectionIdProxy
11-12-2015 12:19:44,803 [UKkerslaj1][20] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Added notification
查看是否可以从代理返回错误:
$.connection.hub.error(function (error) {
console.log('SignalR error: ' + error)
});
此外,在启动连接之前,您可能需要启用客户端日志记录:
// enable client side logging
$.connection.hub.logging = true;
$.connection.hub.start()
.done(function(){ console.log('Now connected, connection ID=' + $.connection.hub.id); })
.fail(function(){ console.log('Could not Connect!'); });
});
让我们看看我们是否还有进一步的进展,以及集线器连接的开始是否是实际的问题。