我认为您没有添加自动生成的信号器中心脚本
<script src="signalr/hubs"></script>
就我而言,我的信号集线器位于webapi上。我包含了api所在的域:
<script src="somedomainorinstance/signalr/hubs"></script>
此外,在页面加载时,您可以添加此项以查看是否正在建立连接
$.connection.hub.url = url + "/signalr/hubs";
$.connection.hub.logging = true;
$.connection.hub.start()
.done(function () { console.log("Signal R connected"); })
.fail(function () { console.log("Could not Connect to signal R hub!"); });
//I added this to established the connection again in case of a disconnect
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000); // Re-start connection after 5 seconds
});
//put the rest of your code here
这就是我现在的设置方式。它工作得很好。
我希望这有帮助!干杯