使用 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();
});
});
});
本地调试时运行正常,输入用户名后可以立即发送消息。但是当部署到Azure上时,输入用户名后,我必须等待大约5秒才能提交新消息(单击“发送”按钮没有响应),但在第一条消息之后,我可以立即发送以下所有消息。
对我来说,设置初始连接时看起来很慢($.connection.hub.start()
)。
这正常吗?如何提高这个简单应用程序的性能?
最佳答案
默认情况下,Azure 上未启用 WebSocket,并且默认情况下,客户端会尝试从 WebSocket 开始的不同传输。如果 websockets 不起作用,它将回退到 serverSentEvents,最后回退到 longPolling。这需要时间。确保在 Azure 上打开 Websockets 或指定仅使用 serverSentEvents 和 longPolling 传输。
关于asp.net-mvc - 设置 SignalR 连接时启动缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292852/