【问题标题】:SignalR client function not called when transport=serverSentEvents当传输 = serverSentEvents 时未调用 SignalR 客户端函数
【发布时间】:2013-01-08 09:32:57
【问题描述】:

在我的机器上,我发现 Chrome 中没有调用 SignalR 客户端函数。

我的 SignalR 测试应用程序在我的 iphone 上与 IE9、Firefox 甚至 Safari 一起运行良好。看看 Fiddler,这些浏览器似乎都在协商 transport=longPolling。但是 Chrome 会使用 transport=serverSentEvents 协商连接,我假设这就是 Chrome 中不调用客户端函数的原因。

更多细节: 我在 Windows 7 上使用完整的 IIS(不是 IIS express)。我使用的是 SignalR 版本 1.0.0-rc2。我已经禁用了我的 AVG 防火墙并且 Windows 防火墙没有运行。 Chrome 的版本是 24.0.1312.56,在撰写本文时是最新的。该应用程序在 localhost 上调用。

在 Chrome 上,signalR 连接似乎正常进行 - 调用了 $.connection.hub.start().done 回调函数。但在那之后,即使其他浏览器工作正常,客户端函数也不会被调用。

在客户端代码中,我使用

打开了日志记录
$.connection.hub.logging = true;

我可以在 Chrome 的 javascript 控制台中看到与成功连接相对应的日志消息。 作为参考,这些日志消息是

[20:22:16 GMT+0800 (W. Australia Standard Time)] SignalR: Negotiating with '/SignalRChat-RC/signalr/negotiate'. jquery.signalR-1.0.0-rc2.js:54
[20:22:16 GMT+0800 (W. Australia Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost/SignalRChat-RC/signalr/connect?transport=serverSentEvents&…7-22c5dbf27e0d&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=3' jquery.signalR-1.0.0-rc2.js:54
[20:22:16 GMT+0800 (W. Australia Standard Time)] SignalR: EventSource connected jquery.signalR-1.0.0-rc2.js:54
[20:22:16 GMT+0800 (W. Australia Standard Time)] SignalR: Now monitoring keep alive with a warning timeout of 40000 and a connection lost timeout of 60000 jquery.signalR-1.0.0-rc2.js:54

但是当调用客户端方法时,Chrome 的 javascript 控制台中没有记录任何消息。

有趣的是,发送方法在 Chrome 上运行良好。其他客户端显示从 Chrome 发送的消息,即使 Chrome 本身看不到它。 该应用程序几乎是http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalrsignalR 教程中的聊天应用程序

如果我在 start 方法中明确指定 longPolling,即

$.connection.hub.start({ transport: 'longPolling' })

然后 Chrome 工作正常。但我的期望是我应该能够允许浏览器协商它们的连接,并且一切都会正常工作。

作为参考,我的客户端代码的相关部分如下所示:

$(function () {
    // Turn on logging to the javascript console
    $.connection.hub.logging = true;

    // set up an error-handling function
    $.connection.hub.error(function (err) {
        alert("Error signalR:" + JSON.stringify(err));
    });

    // Declare a proxy to reference the hub.  
    var chat = $.connection.chatHub;

    // Create a function that the hub can call to broadcast messages.
    // This function is never called when running in Chrome with the default signalR connection
    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. 
    // Use $.connection.hub.start({ transport: 'longPolling' }) for reliability
    // Use $.connection.hub.start() to demonstrate that Chrome doesn't receive messages
    $.connection.hub.start().done(function () {
        // Enable the "Send" button
        $('#sendmessage').removeAttr('disabled');
        $('#sendmessage').click(function () {
            // Call the Send method on the hub.  
            chat.server.send($('#displayname').val(), $('#message').val());
            // Clear text box and reset focus for next comment.  
            $('#message').val('').focus();
        });
    });
});

谁能看出我做错了什么?

【问题讨论】:

  • 哇...今天突然我在带有 Signalr 2.4.1 的 Chrome 上遇到了这个确切的问题!客户端消息在 Firefox 上运行良好。但不是在 Chrome 上。

标签: google-chrome signalr


【解决方案1】:

尝试在服务器集线器上设置EnableJSONP = False。这解决了我遇到的类似问题。

【讨论】:

    【解决方案2】:

    可能与缓冲响应有关。

    https://github.com/SignalR/SignalR/issues/1944

    【讨论】:

      【解决方案3】:

      我在 Win7 Google Chrome 24 中尝试了该示例,它运行良好。

      您可以解决安装Fiddler 和在javascript 中设置断点的问题

      POST /signalr/send?transport=serverSentEvents&connectionId=6ff0bffa-c31e-4d85-9aff-24f4528555ee HTTP/1.1
      Host: localhost:43637
      Connection: keep-alive
      Content-Length: 113
      Accept: application/json, text/javascript, */*; q=0.01
      Origin: 
      X-Requested-With: XMLHttpRequest
      User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
      Content-Type: application/x-www-form-urlencoded
      Referer: /index.html
      Accept-Encoding: gzip,deflate,sdch
      Accept-Language: en-US,en;q=0.8
      Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
      
      data=%7B%22H%22%3A%22chathub%22%2C%22M%22%3A%22Send%22%2C%22A%22%3A%5B%22gus%22%2C%22hello%22%5D%2C%22I%22%3A0%7D
      

      【讨论】:

        猜你喜欢
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多