【发布时间】:2016-09-02 12:49:30
【问题描述】:
使用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 上启用 websockets。 SignalR 正在尝试使用 websockets 传输启动连接,当失败时,它会尝试使用 serverSentEvents 作为后备(如果这不起作用,它会尝试 longPolling)。您可以通过打开调试器工具并检查 SignalR 发出的请求来确认这一点。
-
@Pawel,你是绝对正确的!它正在使用 longPolling。通过在 Azure 上启用 WebSocket 并使用 HTTPS 访问解决了问题。谢谢!
标签: asp.net-mvc signalr signalr-hub