【发布时间】:2016-12-05 11:00:46
【问题描述】:
嘿,我正在关注 here 找到的 SignalR 简单示例教程。负责创建聊天系统。我已经完成了,当我点击发送时没有任何反应。
没有错误,调试器没有进入 Hub 类的 Send 方法,我在按钮 onclick 中放置了一个警报,它没有到达那里意味着 $.connection.hub.start().done( function () 没有启动,所以我猜它永远不会完成。
我打开了日志记录,所有的日志都是:
[21:46:52 GMT-0600(山地夏令时间)] SignalR:客户端订阅了集线器“chathub”。
[21:46:52 GMT-0600(山地夏令时间)] SignalR:正在与 '/signalr/negotiate?clientProtocol=1.5&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) });