【问题标题】:SignalR self host: Connection must be started before data can be sent. Call .start() before .send()SignalR 自主机:必须先启动连接,然后才能发送数据。在 .send() 之前调用 .start()
【发布时间】:2016-11-10 18:18:34
【问题描述】:

我知道这是另一个与这个问题有关的帖子:)

我正在尝试重写我的项目以使用 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 和我所有的服务器方法都在那里可见,就像上面的 sn-p 一样。

服务器端代码:

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 自托管项目中时,错误就消失了

【问题讨论】:

    标签: signalr


    【解决方案1】:

    连接状态应该是已连接

    试试这样:

     $.connection.hub.start().done(function () {
             console.log(connection.state());
         })  .fail(function (error) {
             console.log('Invocation of start failed. Error:'+ error)
         });
    

    【讨论】:

    • 这正是我在上面的代码中所做的(它只是分成两行)。我可以按照您的建议将它们合并为一行: var connection = $.connection.hub.start().done(function() { console.log(connection.state()); }).fail(function (error ) { console.log('调用启动失败。错误:' + 错误) });但结果是一样的:控制台将连接状态显示为“已解决”,但是当我初始化我的视图模型并尝试从服务器获取数据时,会显示来自线程主题的错误。
    • 您是否运行 fiddler 以查看是否有任何数据从您的客户端发送?
    • 您也可以在您的集线器中覆盖公共覆盖任务 OnConnected() 以查看客户端是否可以访问服务器
    • 我会接受它作为答案,因为它帮助我发现了问题。 OnConnected 方法从未在服务器上调用过。我已将服务器上的 Startup 类移动到我的解决方案中的另一个项目,我认为这会导致问题。当我把它和我的 signalR 自托管项目放在同一个地方时,错误就消失了
    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多