javascript SignalR 调用方法:必须先启动连接,然后才能发送数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17445306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:21:34  来源:igfitidea点击:

SignalR invoke method: connection must be started before data can be sent

javascriptsignalr

提问by Youngjae

There are lots of 'connection must be started before data can be sent' issues in here and GitHub, but I hardly find hub related problems.

这里和 GitHub 中有很多“在发送数据之前必须启动连接”的问题,但我几乎找不到与集线器相关的问题。

$(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!'); });
    });

The above code gives to execute a method when user clicks the button. I can check the method works with my WPF .NET app.

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

I can get Connection Id successfully, but when I click the button it says 'SignalR invoke method: connection must be started before data can be sent. Call .start() before .send()' error.

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

What did I wrong?

我做错了什么?

回答by Youngjae

Read tutorialcarefully and it works now.

仔细阅读教程,现在可以使用了。

 $(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'); });;
    });