Saturday, 15 September 2012

javascript - Using SignalR to notify current caller -



javascript - Using SignalR to notify current caller -

the short version

in post action routine, how can send notification javascript running in client? seems signalr should easy solution, don't know how callback signalr client lives in javascript in post action's client page.

specifically, seems need "connection id" in order inquire signalr talk specific client, don't know how 1 of either in post action or in client javascript.

the whole (not ugly) story

in mvc app, post action may take long time complete. if post action decides take long time complete, want notify javascript on page can display "please wait..." notification user.

this seems signalr should create easy me. next basic tutorial, added hub , created js callback in view:

note hub has no methods. client needs read-only notification. has no need phone call methods on hub write message outside world.

public class myhub: hub { }

the view has form submit button , hidden "please wait" message signalr routine can display. view model has string property can utilize pass signalr "connection id" post controller (assuming can figure out how it).

@model signalrtest.models.myviewmodel @using (html.beginform()) { @html.hiddenfor(m => m.signalrconnectionid) <button type="submit" class="btn btn-primary">go it!</button> } <div id="hidden-msg" hidden="hidden"> <p>please wait...</p> </div> @section scripts { <!-- reference signalr library. --> <script src="~/scripts/jquery.signalr-2.1.2.min.js"></script> <!-- reference autogenerated signalr hub script. --> <script src="~/signalr/hubs"></script> <!-- signalr script update page --> <script> $(function () { // reference server "hub" class (camelcase) var hub = $.connection.interviewdonehub; // our connection id , store in hidden field // sent post action // var connectionid = $.connection.hub.id; //this doesn't work! // $('#@html.idfor(m => m.signalrconnectionid)').attr(connectionid, ''); // create function hub can phone call hub.client.mycallback = function () { $('#hidden-msg').show(); }; // start connection. $.connection.hub.start().done(function () { }); }); </script> }

meanwhile in post controller action, phone call js callback:

[httppost] public async task<actionresult> myaction(myviewmodel model) { // we've decided take while. tell client it... var context = globalhost.connectionmanager.gethubcontext<myhub>(); context.clients.all.mycallback(); //context.clients.client(model.signalrconnectionid).mycallback(); await task.delay(2000); homecoming redirecttoaction("nextpage"); }

now, problem: proof-of-concept test, utilize code phone call js callback:

var context = globalhost.connectionmanager.gethubcontext<myhub>(); context.clients.all.mycallback();

which works dandy. but, obviously, want phone call specific client associated post action. c# intellisense tells me should able call

context.clients.client("connectionid").mycallback();

but can't figure out how desired "connectionid" string. don't think i'll able client id in post controller because don't have sort of signalr connection client @ point. figured i'd allow client connection id , give post controller in view model, haven't found magic js code fetches connection id out of signalr framework.

i found several articles stated matter-of-factly:

connectionid = $.connection.hub.id;

but returns undefined. found signalr js client wiki page, says:

connection.id gets or sets client id current connection

but returns undefined.

which gets original question. maybe i'm not understanding fundamental connection id is...

okay believe see issue is. want start connection in javascript

$.connection.hub.start()

after that, should able connection.hub.id - if connection not started there no connection id @ all, why getting "undefined" value because has not been set until start connection.

actually bastardization of things, not sure if work should require minimal changes code. each time client connects hub can add together connect method

groups.add(context.connectionid, context.connectionid); // name same connection id since identified using

then in controller can add together next in action call

var context = globalhost.connectionmanager.gethubcontext<interviewdonehub>(); context.clients.groups(connectionid).mycallback();

if above not work going have connect hub using .net client. connected javascript can connect hub using c# , phone call method notify client want notified. did using webapi in past can done in asp.net mvc well.

javascript asp.net asp.net-mvc asp.net-mvc-4 signalr

No comments:

Post a Comment