我有一个SignalR Server方法,如下所述。我能够调用
中的方法 public void AddNotification(string message, string toUser)
{
lock (connectedUsers)
{
if (connectedUsers.ContainsKey(toUser))
{
string receiverID = connectedUsers[toUser];
hubContext.Clients.Client(receiverID).addMessage(message);
}
}
}
我能够使用JS Client从javascript调用该方法。
$(function () {
$("#sendControls").hide("fast");
$.connection.hub.url = 'http://localhost:45210/signalr/hubs';
// Proxy created on the fly
selfHostHub = $.connection.selfHostHub;
// Declare a function on the hub so the server can invoke it
selfHostHub.client.addMessage = function (message) {
$('#messages').append("<li>" + message + "</li>");
};
// Start the connection
$.connection.hub.start().done(function () {
$("#Endcall").click(function () {
selfHostHub.server.addNotification(username + " has disconnected call for loanid " + loanid, supervisorName);
});
});
});
是否可以在javascript函数内调用Server方法而不是OnClick事件。请提供任何解决方案。