【问题标题】:SignalR invoke method: connection must be started before data can be sentSignalR 调用方法:必须先启动连接才能发送数据
【发布时间】:2016-03-21 18:09:16
【问题描述】:

here 和 GitHub 有很多“必须先启动连接才能发送数据”的问题,但我几乎找不到与 hub 相关的问题。

$(function () {
        // Declare a proxy to reference the hub. 
        var connection = $.hubConnection('http://www.website.net/');
        var chat = connection.createHubProxy('MyHub');

        // Start the connection.
        $.connection.hub.start().done(function () {
            console.log('Connect! connection Id=' + $.connection.hub.id);

            $('#sendmessage').click(function () {
                chat.invoke('method1','0000').done(function () {
                    console.log ('Invocation of method1 succeeded');
                }).fail(function (error) {
                    console.log('Invocation of method1 failed. Error: ' + error);
                });
            });
        })
        .fail(function(){ console.log('Could not Connect!'); });
    });

上面的代码给出了当用户点击按钮时执行一个方法。 我可以检查该方法是否适用于我的 WPF .NET 应用程序。

我可以成功获取连接 ID,但是当我单击按钮时,它显示“SignalR 调用方法:必须先启动连接,然后才能发送数据。在 .send()' 错误之前调用 .start()。

我做错了什么?

【问题讨论】:

    标签: javascript signalr


    【解决方案1】:

    仔细阅读tutorial,它现在可以工作了。

     $(function () {
            // Declare a proxy to reference the hub. 
            var connection = $.hubConnection('http://www.website.net/');
            var chat = connection.createHubProxy('MyHub');
    
            connection.start().done(function() {
                console.log('Now connected, connection ID=' + connection.id); 
                // Wire up Send button to call sendmessage on the server.
                $('#sendmessage').click(function () {
                    chat.invoke('method1', '0000');
                    });
                })
                .fail(function(){ console.log('Could not connect'); });;
        });
    

    【讨论】:

    • 好点,教程没有说明连接和集线器代理必须在开始连接之前初始化
    猜你喜欢
    • 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
    相关资源
    最近更新 更多