我创建了一个带有SignalR通知的ASP MVC 4应用程序。我在本地调试模式下运行它,也通过IIS发布在服务器上运行它。当使用Internet Explorer 11时,这主要适用于(稍后解释)预期:
HTML1300: Navigation occurred.
File: AllChanges
SignalR: Window unloading, stopping the connection.
SignalR: Stopping connection.
SignalR: Stopping forever frame.
SignalR: Fired ajax abort async = false.
SignalR: Stopping the monitoring of the keep alive.
JQMIGRATE: Migrate is installed, version 3.0.0
SignalR: Client subscribed to hub 'prismhub'.
SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22prismhub%22%7D%5D'.
SignalR: serverSentEvents transport starting.
SignalR: This browser doesn't support SSE.
SignalR: serverSentEvents transport failed to connect. Attempting to fall back.
SignalR: foreverFrame transport starting.
SignalR: Binding to iframe's load event.
SignalR: Iframe transport started.
SignalR: foreverFrame transport connected. Initiating start request.
SignalR: The start request succeeded. Transitioning to the connected state.
SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
Connected to signalR hub
SignalR: foreverFrame transport timed out when trying to connect.
**SignalR: Triggering client hub event 'sendUpdatedChange' on hub 'prismHub'.**
通知似乎无法在Chrome中使用。在日志中,我可以看到SignalR正在推出通知:
SignalR: Window unloading, stopping the connection.
SignalR: Stopping connection.
SignalR: EventSource calling close().
SignalR: Fired ajax abort async = false.
SignalR: Stopping the monitoring of the keep alive.
JQMIGRATE: Migrate is installed, version 3.0.0
SignalR: Client subscribed to hub 'prismhub'.
SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22prismhub%22%7D%5D'.
SignalR: serverSentEvents transport starting.
SignalR: Attempting to connect to SSE endpoint 'http://localhost:61159/signalr/connect?transport=serverSentEvents&clientPro…FNE6BKzqphux4&connectionData=%5B%7B%22name%22%3A%22prismhub%22%7D%5D&tid=9'.
SignalR: EventSource connected.
SignalR: serverSentEvents transport connected. Initiating start request.
SignalR: The start request succeeded. Transitioning to the connected state.
SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
Connected to signalR hub
**SignalR: Triggering client hub event 'sendUpdatedChange' on hub 'prismHub'.
SignalR: Triggering client hub event 'sendUpdatedChange' on hub 'prismHub'.
SignalR: Triggering client hub event 'sendUpdatedChange' on hub 'prismHub'.**
但我视图中的数据并没有改变(就像在Internet Explorer中一样)。 IE中偶尔会出现此问题。此外,刷新页面似乎打破了集线器连接。从IE控制台:
HTML1300: Navigation occurred.
File: AllChanges
JQMIGRATE: Migrate is installed, version 3.0.0
SignalR: Client subscribed to hub 'prismhub'.
SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22prismhub%22%7D%5D'.
SignalR: serverSentEvents transport starting.
SignalR: This browser doesn't support SSE.
SignalR: serverSentEvents transport failed to connect. Attempting to fall back.
SignalR: foreverFrame transport starting.
SignalR: Binding to iframe's load event.
SignalR: Iframe transport started.
SignalR: foreverFrame transport connected. Initiating start request.
SignalR: The start request succeeded. Transitioning to the connected state.
SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
Connected to signalR hub
SignalR: foreverFrame transport timed out when trying to connect.
值得注意的是,这些请求对使用linq查询命中数据库的控制器进行ajax调用,返回数据列表:
查看JS
$(document).ready(function () {
FetchChanges();
notifications.client.sendUpdatedChange = function () { FetchChanges(); };
}
function FetchChanges() {
var model = $('#divChanges');
$.ajax({
url: '/Changes/AllChangesItems',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html',
})
.done(function (result) {
model.empty().append(result);
})
}
控制器操作
public ActionResult AllChangesItems()
{
var username = CurrentUser().UserName;
var changes = db.Changes;
List<ChangeListingViewModel> vm = new List<ChangeListingViewModel>();
foreach (var c in changes)
{
vm.Add(new ChangeListingViewModel(c));
}
return PartialView("_ActiveItems", vm.ToList());
}
其中ChangeListingViewModel包含两个获取子行的linq查询(如果有人提供可以提供代码):
然而,更简单的&#39;查询似乎在IE和Chrome中正常工作,即推送通知触发更新视图的JS函数:
public void PinChange(int id)
{
CurrentUserDetails().User.PinnedChanges.Add(db.Changes.Find(id));
db.SaveChanges();
PrismHub.NotifyPinnedChange();
}
我的第一个想法是发生了某种超时,但我不确定为什么浏览器会有所不同。任何建议都将不胜感激。
编辑:更新了Hub信息 编辑2:更新了集线器方法以与其他代码段保持一致。
以下是我的集线器类的片段:
集线器
[HubName("prismHub")]
public class PrismHub : Hub
{
private static IHubContext context = GlobalHost.ConnectionManager.GetHubContext<PrismHub>();
[HubMethodName("notifyUpdatedChange")]
public static void NotifyUpdatedChange()
{
context.Clients.All.sendUpdatedChange();
}
}
答案 0 :(得分:2)
无意中偶然发现了解决方案。经过几周的搜索,我遇到了这篇文章:signalr client hub events not being triggered within a jquery .ready() method
通过在视图中解决这条JS来解决这个问题:
$(document).ready(function () {
FetchChanges();
notifications.client.sendUpdatedChange = function () { FetchChanges(); };
}
在$(document).ready()之外移动SignalR函数映射解决了它:
$(document).ready(function () {
FetchChanges();
}
notifications.client.sendUpdatedChange = function () { FetchChanges(); };
我还没有发现为什么这是一个问题,但我很高兴能找到解决方案。