为什么包装我的JS SignalR回调函数我的$(函数()使它不被触发?
问题描述:
为什么在$(document).ready
处理程序或通过其简写$(function()
包装我的SignalR回调函数(从.NET C#服务器端集线器到JavaScript客户端)这个特殊的回调不会触发(的start
done
和断开工作的处理,无论它们被包裹或不...):
服务器端 - C#枢纽:
public class AppointmentHub : Hub
{
public void RefreshSchedulers(string ownerConnectionId, string userId)
{
Clients.User(userId).refreshScheduler(ownerConnectionId);
}
}
客户端 - 的JavaScript枢纽代理+回拨:
[...]
function getSchedulerInstance() {
return $('#scheduler').scheduler('instance');
}
$(function() {
// If I move this handler below out of the jQuery document ready ($(function()) ... it works... why?
$.connection.appointmentHub.client.refreshScheduler = function(ownerConnectionId) {
if (getAppointmentHubConnectionId() !== ownerConnectionId) {
getSchedulerInstance().repaint();
}
};
$.connection.appointmentHub.connection.disconnected(function() {
setTimeout(function() {
$.connection.appointmentHub.connection.start();
},
500);
});
$.connection.appointmentHub.connection.logging = true;
$.connection.appointmentHub.connection.start().done(function() {
});
});
function getAppointmentHubConnectionId() {
return $.connection.appointmentHub.connection.id;
}
function refreshScheduler() {
$.connection.appointmentHub.server.refreshSchedulers(getAppointmentHubConnectionId(), '@User.Identity.GetUserId()');
}
[...]
// Stuff below works
function onAppointmentDeleted(e) {
ui.notify("Appointment deleted.", "success", 600);
refreshScheduler();
}
function onAppointmentUpdated(e) {
refreshScheduler();
}
function onAppointmentAdded(e) {
refreshScheduler();
}
无法真正弄清楚,我在调试模式下检查了集线器正在调用refreshScheduler
,所以一切都应该没问题。
[编辑]
增加了一些日志:$.connection.appointmentHub.connection.logging = true;
客户端1(谷歌浏览器)
[18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: webSockets transport starting.
jquery.signalR-2.2.2.min.js:9 [18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: Connecting to websocket endpoint 'ws://localhost:52539/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=83lZVSflIHfvK0aJmXokuuQ9lC5%2F4WV6wtzkGcWKXBLrVPhQWC41mduPmu%2FMs3XM%2BezlduQ%2FKoSodH9HouPXECFS69bvKe770QV8iJbFusZPIgxLbFsLNOT8cWDsuV30hUE5QoFhjqBWto7%2BUAYnww%3D%3D&connectionData=%5B%7B%22name%22%3A%22userhub%22%7D%5D&tid=7'.
jquery.signalR-2.2.2.min.js:9 [18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: Websocket opened.
jquery.signalR-2.2.2.min.js:9 [18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: webSockets transport connected. Initiating start request.
jquery.signalR-2.2.2.min.js:9 [18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: The start request succeeded. Transitioning to the connected state.
jquery.signalR-2.2.2.min.js:9 [18:55:20 GMT-0400 (Eastern Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
jquery.signalR-2.2.2.min.js:9 [18:55:53 GMT-0400 (Eastern Daylight Time)] SignalR: Invoking appointmenthub.RefreshSchedulers
jquery.signalR-2.2.2.min.js:9 [18:55:53 GMT-0400 (Eastern Daylight Time)] SignalR: Invoked appointmenthub.RefreshSchedulers
jquery.signalR-2.2.2.min.js:9 [18:56:03 GMT-0400 (Eastern Daylight Time)] SignalR: Invoking appointmenthub.RefreshSchedulers
jquery.signalR-2.2.2.min.js:9 [18:56:03 GMT-0400 (Eastern Daylight Time)] SignalR: Invoked appointmenthub.RefreshSchedulers
客户端2器(Internet Explorer 11)
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Client subscribed to hub 'appointmenthub'.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Client subscribed to hub 'userhub'.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22appointmenthub%22%7D%2C%7B%22name%22%3A%22userhub%22%7D%5D'.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: webSockets transport starting.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Connecting to websocket endpoint 'ws://localhost:52539/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=rWeJC23FIMc%2FXJcoF4kw3DNAJIDLk6tZXgKAMe2qWo6jq3TWwwDm0FqgWq%2F7CE3VGer%2FPVKi7hYOqryGcMVvTE1%2BGuBdNhYn76X1u1rw5p3AHuFOn%2BxPFPca0%2FDnYIcV3P5bxQUDZG1IqZL6MGw12w%3D%3D&connectionData=%5B%7B%22name%22%3A%22appointmenthub%22%7D%2C%7B%22name%22%3A%22userhub%22%7D%5D&tid=4'.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Websocket opened.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: webSockets transport connected. Initiating start request.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: The start request succeeded. Transitioning to the connected state.
[18:55:34 GMT-0400 (Eastern Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
[18:55:53 GMT-0400 (Eastern Daylight Time)] SignalR: Triggering client hub event 'refreshScheduler' on hub 'AppointmentHub'.
[18:55:57 GMT-0400 (Eastern Daylight Time)] SignalR: Invoking appointmenthub.RefreshSchedulers
[18:55:57 GMT-0400 (Eastern Daylight Time)] SignalR: Triggering client hub event 'refreshScheduler' on hub 'AppointmentHub'.
[18:55:57 GMT-0400 (Eastern Daylight Time)] SignalR: Invoked appointmenthub.RefreshSchedulers
[18:55:59 GMT-0400 (Eastern Daylight Time)] SignalR: Invoking appointmenthub.RefreshSchedulers
[18:55:59 GMT-0400 (Eastern Daylight Time)] SignalR: Triggering client hub event 'refreshScheduler' on hub 'AppointmentHub'.
[18:55:59 GMT-0400 (Eastern Daylight Time)] SignalR: Invoked appointmenthub.RefreshSchedulers
[18:56:01 GMT-0400 (Eastern Daylight Time)] SignalR: Invoking appointmenthub.RefreshSchedulers
[18:56:01 GMT-0400 (Eastern Daylight Time)] SignalR: Triggering client hub event 'refreshScheduler' on hub 'AppointmentHub'.
[18:56:01 GMT-0400 (Eastern Daylight Time)] SignalR: Invoked appointmenthub.RefreshSchedulers
[18:56:03 GMT-0400 (Eastern Daylight Time)] SignalR: Triggering client hub event 'refreshScheduler' on hub 'AppointmentHub'.
对于一些原因,它的工作原理从Chrome浏览器到IE11,但不是相反... 我不认为它与浏览有关因为我试图在IE中打开另一个选项卡,并且还与Opera连接,但它仍然是第一个从Chrome浏览器到第一个实例IE11的实例...正在工作中...让处理程序脱离准备好的修复行为...仍然不是'真的得到了什么事情。
您是否试图在页面完全解析为DOM之前调用refreshScheduler?由于refreshScheduler的定义是在准备好的文档中创建的,所以直到该点之后,该逻辑才会运行。或者你是否说它以这种方式正确执行,但它内部的错误? – Taplar
@Taplar调用仅在页面完全加载后执行。 – Youna
那么,您在控制台中看到的错误究竟是什么? – Taplar