c# - SignalR how to call 2 different method in 1 Hub? -
so have 2 methods in 1 hub:
public class chathub : hub { public void sendmessage(string name, string message) { clients.all.addmessage(name, message); } public void sendannounce(string name) { clients.others.addmessage(name); } }
how use 2 methods in 1 hub in client side javascript?
i have in javascript,
for sendmessage(string name, string message) hub method:
var message = $("#txtmessage").val(); var userid = $("#lblusername").html(); chat.client.addmessage = function (frm, msg) { $messages.append("[" + frm + "] " + msg); }
invoke:
chat.server.sendmessage(userid, input);
for sendannounce(string name) hub method:
chat.client.addmessage = function (frm) { $announcement.append("<div>test</div>"); }
invoke:
var userid = $("#txtusername").val(); chat.server.announcement(userid);
but doesn't work(server uses last method in case announcement one).
works if make 2 hubs , put each method in different hubs, create different variables each hub this:
//create hub on air var chat = $.connection.chathub; var chat2 = $.connection.announcementhub;
then work 1 method "chat" variable , second method "chat2" variable.. works. this:
chat.client.addmessage = function (frm, msg) { $messages.append("[" + frm + "] " + msg); } chat2.client.addmessage = function (frm) { $announcement.append("<div> test</div>"); }); }
what not understand?
in javascript can't have method overloads. doing first declaring addmessage
function 2 parameters , overwrite function 1 parameter. last 1 being used
either use methods named differently, e.g. addmessage
, addannouncement
or use single method , check if second parameter defined. if not announcement.
Comments
Post a Comment