嘿所以我遵循SignalR简单示例教程发现here。任务是创建一个聊天系统。我完成了它,当我点击发送没有任何反应。SignalR简单聊天示例:卡住谈判
没有错误,调试器没有进入Hub类的Send方法内部,我在按钮onclick中放了一个警报,并且它没有到达那里,这意味着$ .connection.hub.start()。done (函数()没有发射过,所以它从来没有做过伸手我猜
我打开日志,和所有日志得到的是:
[21点46分52秒GMT-0600(山日光时间)] SignalR:客户订阅集线器'chathub'
[21:46:52 GMT-0600(山区夏令时)] SignalR:与'/signalr/negotiate?clientProtocol=1协商& connectionData =%5B%7B%22name%22%3A%22chathub%22%7D%5D'。
看看下面的代码:
$(document).ready(function() {
$(function() {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
$.connection.hub.logging = true;
// 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.
alert($('#message').val());
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
});</script>
的问题是什么你知道吗?调试这是地狱!我完全按照步骤进行。
使用jquery 3,SignalR 2.2.1
尝试添加错误处理程序。 '$ .connection.hub.error(function(error){console.log('SignalR error:'+ error)});' –