【发布时间】:2019-10-16 00:59:55
【问题描述】:
我正在阅读有关 Signal-R 的 Microsoft 教程,并希望在 .Net Core 中使用它而不是 Ajax,考虑到 Microsoft 的以下代码,在从在 broadcastMessage 函数中第一次调用(即定义为 javascript 函数)?
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// 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>: ' + 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.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
这样我们就可以检查接收到的数据并有条件地发起另一个调用。 或者,如果所有服务器调用都应该放在 $.connection.hub.start().done(function () {?
【问题讨论】: