javascript - SignalR working with groups is not working correctly -
i work signalr. group functionality not working correctly.
here hub:
public class baskethub : hub { public void login(int companyid) { string groupname = "company" + companyid; ihubcontext context = globalhost.connectionmanager.gethubcontext<baskethub>(); context.groups.add(context.connectionid, groupname); } public void logout(int companyid) { string groupname = "company" + companyid; ihubcontext context = globalhost.connectionmanager.gethubcontext<baskethub>(); context.groups.remove(context.connectionid, groupname); } }
here using server side:
public void orderconfirm(orderconfirmmodel orderconfirmmodel) { ihubcontext context = globalhost.connectionmanager.gethubcontext<baskethub>(); var groupname = "company" + orderconfirmmodel.companyid; context.clients.group(groupname).orderconfirm(orderconfirmmodel.didneworder); context.clients.group(groupname).newordercount(orderconfirmmodel.didneworder); context.clients.group(groupname).didgetnotification(orderconfirmmodel.basketid); }
here client:
$(function () { var baskethub = $.connection.baskethub; baskethub.client.newordercount = function (isneworder) { // code } baskethub.client.didgetnotification = function (basketid) { // code } baskethub.client.orderconfirm = function (isneworder) { // code } $.connection.hub.start(); });
here group add , remove client:
$(function () { var baskethub = $.connection.baskethub; $.connection.hub.start().done(function () { baskethub.server.login($scope.company.id); $('#logoutbtn').click(function () { baskethub.server.logout($scope.company.id); }); }); $(window).bind('beforeunload', function (e) { baskethub.server.logout($scope.company.id); }); });
i created group on baskethub's context. however, when try use group, dont reach it. , there no triggered notifications on client side.
i can use below:
context.clients.all.orderconfirm(orderconfirmmodel.didneworder); context.clients.all.newordercount(orderconfirmmodel.didneworder); context.clients.all.didgetnotification(orderconfirmmodel.basketid);
best regards.
i found solution. write angular controller run init every page load (like layout-controller). define hub here , emit functions. singnalr groups work fine.
$(function () { var baskethub = $.connection.baskethub; baskethub.client.orderconfirm = function (isneworder) { $rootscope.$emit("basketorderconfirm", isneworder); } baskethub.client.newordercount = function (isneworder) { $rootscope.$emit("newordercount", isneworder); } baskethub.client.didgetnotification = function (basketid) { $rootscope.$emit("didgetnotification", basketid); } $.connection.hub.start().done(function () { baskethub.server.login($scope.company.id); $('#logoutbtn').click(function () { baskethub.server.logout($scope.company.id); }); }); $(window).bind('beforeunload', function (e) { baskethub.server.logout($scope.company.id); }); });
Comments
Post a Comment