我有集线器类:
public class ChatHub : Hub
{
// Send message
public void Send(string message)
{
Clients.addMessage(DateTime.Now.ToString("HH:mm:ss") + " " + message);
}
}
和 javascript:
// Proxy created on the fly
var chat = $.connection.chatHub;
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function(message) {
alert("message:" + message);
$('#chat').append('<div>' + message + '</div>');
};
$("#sendButton").click(function () {
// Call the chat method on the server
chat.send($('#message').val())
.done(function () {
console.log('Success!')
})
.fail(function (e) {
console.warn(e);
})
});
// Start the connection
$.connection.hub.start();
所有连接都很好:
如果我在这里使用断点Clients.addMessage(DateTime.Now.ToString("HH:mm:ss") + " " + message); 一切都很好。
但我没有得到 javascript 函数的回调。 警报(“消息:” + 消息);从不执行