8

在这里和 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!'); });
    });

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

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

我做错了什么?

4

1 回答 1

10

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

 $(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'); });;
    });
于 2013-07-03T15:14:48.383 回答