【问题标题】:SignalR Simple Chat Example: Stuck NegotiatingSignalR 简单聊天示例:谈判陷入僵局
【发布时间】:2016-12-05 11:00:46
【问题描述】:

嘿,我正在关注 here 找到的 SignalR 简单示例教程。负责创建聊天系统。我已经完成了,当我点击发送时没有任何反应。

没有错误,调试器没有进入 Hub 类的 Send 方法,我在按钮 onclick 中放置了一个警报,它没有到达那里意味着 $.connection.hub.start().done( function () 没有启动,所以我猜它永远不会完成。

我打开了日志记录,所有的日志都是:

[21:46:52 GMT-0600(山地夏令时间)] SignalR:客户端订阅了集线器“chathub”。

[21:46:52 GMT-0600(山地夏令时间)] SignalR:正在与 '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D% 协商5D'。

看下面的代码:

 $(document).ready(function () {
                $(function () {
                    // Declare a proxy to reference the hub. 
                    var chat = $.connection.chatHub;
                    $.connection.hub.logging = true;

                    // Create a function that the hub can call to broadcast messages.
                    chat.client.broadcastMessage = function (name, message) {
                        // Html encode display name and message. 
                        var encodedName = $('<div />').text(name).html();
                        var encodedMsg = $('<div />').text(message).html();
                        // Add the message to the page. 
                        $('#discussion').append('<li><strong>' + encodedName
                            + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
                    };
                    // Get the user name and store it to prepend to messages.
                    $('#displayname').val(prompt('Enter your name:', ''));
                    // Set initial focus to message input box.  
                    $('#message').focus();
                    // Start the connection.



                    $.connection.hub.start().done(function () {
                        $('#sendmessage').click(function () {
                            // Call the Send method on the hub. 
                            alert($('#message').val());
                            chat.server.send($('#displayname').val(), $('#message').val());
                            // Clear text box and reset focus for next comment. 
                            $('#message').val('').focus();
                        });
                    });
                });
            });</script>

你知道问题是什么吗?调试这是地狱!我已经完全按照演练进行了。

使用 Jquery 3、SignalR 2.2.1

【问题讨论】:

  • 尝试添加错误处理程序。 $.connection.hub.error(function (error) { console.log('SignalR error: ' + error) });

标签: c# asp.net signalr


【解决方案1】:

谢谢阿列克谢!你已经解决了我的问题,我打印出了错误(我不知道你可以),而且客户端和服务器版本(1.5 和 1.4)不匹配。我忘了更新 Nuget 包管理器中的服务器部分。

如果有人遇到此问题,请确保您已将所有 SignalR 库更新到最新或相同版本!

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2011-06-03
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    相关资源
    最近更新 更多