我正在使用 signalR 创建聊天应用程序。我添加了 signalr.js 1.0.1。当应用程序像这样开始时,我在 global.asax 中创建了信号器集线器:
RouteTable.Routes.MapHubs(new Microsoft.AspNet.SignalR.HubConfiguration() { EnableCrossDomain = true });
但我的问题是它有时会向我显示如下错误。
Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>.
编辑: 我已经为信号器动态添加了所有文件
<script lang="javascript" type="text/javascript">
//add all js files for signalR chat dynamically to body
function SetSignalRJs() {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", "Include/JS/JqueryPlugin/SignalR/jquery.signalR-1.0.1.js");
document.getElementsByTagName("body")[0].appendChild(fileref);
}
function SetHub() {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", "signalr/hubs");
document.getElementsByTagName("body")[0].appendChild(fileref);
}
function SetChatJs() {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", "Include/JS/CollectiveIntelligence/ChatApplication.js");
document.getElementsByTagName("body")[0].appendChild(fileref);
}
//apply timeout for all js files
setTimeout(SetSignalRJs, 1000);
setTimeout(SetHub, 5000);
setTimeout(SetChatJs, 6000);