asp.net mvc - Can't invoke client functions from the Server -
i'm working on small concept trying work signalr. in scenario connected clients need refresh when 1 of clients perform update, update performed through ajax request. because there might different instances of page, depending on parameter, i'm using groups this.
the client side code looks followed:
<script> window.onload = function () { dorefresh(); } $(function () { var hub = $.connection.commonhub; hub.client.refresh = function () { dorefresh();//this doesn't called other clients }; $.connection.hub.start(); $.connection.hub.start().done(function () { hub.server.join("mypage" + @model.id + ""); }); }); function htmlencode(value) { var encodedvalue = $('<div />').text(value).html(); return encodedvalue; } function dorefresh() { $.ajax({ url: '@url.action("loaddata")', cache: false, data: "&id=" + @model.id + "", success: function (html) { $("#conversation").empty(); $("#conversation").append(html); }, error: function (xhr, status, err) { alert('response code:' + xhr.status + '\r\n[error:' + err + '] ' + status); } }); return false; }; function doupdate() { $.ajax({ url: '@url.action("doupdate")', cache: false, data: "&id=" + @model.id + "&posterid=" + @useraccount.accountid + "&message=" + $("#textbox").val().replaceall("\n", "[br]"), success: function () { $("#textbox").empty(); dorefresh(); }, error: function () { $("#textbox").empty(); dorefresh(); } }); return false; }; </script>
in controller, following functions part of scenario:
public class mycontroller : controller { private hubs.commonhub hub = new hubs.commonhub(); //some other controller methods public partialviewresult loaddata(int id) { mymodel item = connection.db.mydata.firstordefault(x => x.id == id); return partialview(item); } public virtual emptyresult doupdate(int id, int posterid, string message) { message = message.replace("[br]", "\r\n"); mymodel item = connection.db.mydata.firstordefault(x => x.id == id); account poster = connection.db.accounts.firstordefault(x => x.id == posterid);; item.updates.add(new update() { posterid = posterid, poster = poster, id = id, item = item, postdate = datetime.utcnow, message = message.replace(environment.newline,"\r\n") }); connection.db.savechanges(); hub.refresh("mypage" + item.id); return null; } }
and finally, hub class looks this:
using system; using system.collections.generic; using system.data.entity; using system.linq; using system.threading.tasks; using system.web; using microsoft.aspnet.signalr; using myproject.models; namespace myproject.hubs { public class commonhub : hub { private string myinfo; public override task onconnected() { myinfo = context.connectionid; return base.onconnected(); } public task join(string groupname) { return groups.add(context.connectionid, groupname); } public task leave(string groupname) { return groups.remove(context.connectionid, groupname); } public void refresh(string groupname) { var context = globalhost.connectionmanager.gethubcontext<commonhub>(); context.clients.group(groupname).refresh(); } } }
the join
task in hub called every connect of new browser window. refresh
method invoked on hub, invoking window has page refreshed. client side refresh
function gets invoked once when debugging, invoking client.
i have no idea why other clients not doing updates, please help.
Comments
Post a Comment