设置 SignalR 连接的启动缓慢

Slow Startup for setup SignalR connection

使用 Microsoft tutorial code.

设置简单的 SignalR Chat Web 应用程序
!--SignalR script to update the chat page and send messages.--> 
<script>
    $(function () {
        // Reference the auto-generated proxy for the hub.  
        var chat = $.connection.chatHub;
        // Create a function that the hub can call back to display messages.
        chat.client.addNewMessageToPage = function (name, message) {
            // Add the message to the page. 
            $('#discussion').append('<li><strong>' + htmlEncode(name) 
                + '</strong>: ' + htmlEncode(message) + '</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. 
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            });
        });
    });

在本地调试的时候运行OK,输入用户名就可以发送消息了。但是当部署到 Azure 上时,输入用户名后,我必须等待大约 5 秒才能提交新消息(单击“发送”按钮没有响应),但是在第一条消息之后,我可以立即发送以下所有消息。

对我来说,设置初始连接时看起来很慢 ($.connection.hub.start())。

这正常吗?我怎样才能提高这个简单应用程序的性能?

默认情况下,Azure 上未启用 websockets,并且默认情况下,客户端会尝试从 websockets 开始的不同传输。如果 websockets 不起作用,它将回退到 serverSentEvents,最后是 longPolling。这需要时间。确保您在 Azure 上打开 websockets 或指定您只想使用 serverSentEvents 和 longPolling 传输。