我知道这是与此问题相关的另一篇文章:)
我试图重写我的项目以使用SignalR自我主机库(根据this教程),但现在无法通过此错误。
我按如下方式初始化连接:
$.connection.hub.url = "http://localhost:8182/signalr";
$.connection.hub.logging = true;
var connection = $.connection.hub.start();
console.log(connection.state());
connection.done(function () {
console.log(connection.state());
[...] // initialising viewmodels in here...
});
初始化第一个视图模型时,我会根据主题主题获取错误:
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Auto detected cross domain url. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Negotiating with 'http://localhost:8182/signalr/negotiate?clientProtocol=1.3'. jquery.signalR-2.0.3.js:76
pending main.js:218
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:8182/signalr/connect?transport=serverSentEvents&connection…pdQlpKjRBpUhB0PY4itVfHasSmixQAAAB0HA7hA9gYDflrhGockG%2FZR55tLg%3D%3D&tid=1'. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: EventSource connected. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000. jquery.signalR-2.0.3.js:76
resolved main.js:220
Object {getAccountActions: function, getIncidentActionCount: function, getIncidentActions: function, refreshAccountActions: function, refreshActions: function…}
IncidentAction.js:12
Uncaught Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()
我在connection.done之后的console.log连接状态,并且显示为已解决。
同样在我初始化的第一个视图模型中,我可以看到console.log connection.server和我所有的服务器方法,如上面的代码片段所示。
服务器端代码:
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8182";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
任何建议,为什么即使连接被解决也会出现此错误?
编辑:最初我将服务器上的Owin Startup类移到我解决方案中的另一个项目中,我认为这导致了问题。当我把它放在我的signalR自主项目中时,错误就消失了
答案 0 :(得分:2)
应连接连接状态
试试这样:
$.connection.hub.start().done(function () {
console.log(connection.state());
}) .fail(function (error) {
console.log('Invocation of start failed. Error:'+ error)
});