c# - signalR2: OnDisconnected fires immediately on page refresh -


isnt ondisconnect method supposed wait default 30s before being fired? me fires instantly on page refresh(f5).

i have user object keeps track of users connections in hashset.

in hub have dictionary keep track of connected users.

onconnected: add user dictionary, if user there, add connectionid users hashset.

ondisconnected: remove connectionid calling users hashset, , if doesnt have connections left remove user object dictionary.

i need keep track of user object, , lose on every page refresh(f5) cause ondisconnected gets fired straight away , removes users connection , object. , when page loads again, new user object gets created, cause old 1 removed straight away.

my implementation looks this

  private static readonly concurrentdictionary<string, user> users          = new concurrentdictionary<string, user>();      public override task onconnected() {          string username = context.user.identity.name;         string connectionid = context.connectionid;          var user = users.getoradd(username, _ => new user {             name = username,             connectionids = new hashset<string>()         });          lock (user.connectionids) {              user.connectionids.add(connectionid);              // todo: broadcast connected user         }          return base.onconnected();          }          public override task ondisconnected() {          string username = context.user.identity.name;         string connectionid = context.connectionid;          user user;         users.trygetvalue(username, out user);          if (user != null) {              lock (user.connectionids) {                  user.connectionids.removewhere(cid => cid.equals(connectionid));                  if (!user.connectionids.any()) {                      user removeduser;                     users.tryremove(username, out removeduser);                      // might want broadcast info if                      // last connection of user , user actual                      // disconnected connections.                     clients.others.userdisconnected(username);                 }             }         }          return base.ondisconnected();     } 

so solved running task inside ondisconnected method , delaying method x seconds checking if user has reconnected, if hasn't remove him list.

public override task ondisconnected(bool stopcalled)         {                 task mytask = task.run(() =>                 {                     userdisconnected(context.user.identity.name, context.connectionid);                 });                return base.ondisconnected(stopcalled);         }   private async void userdisconnected(string un, string cid)         {             await task.delay(10000);             string username = un;             string connectionid = cid;              user user;             enqueueddictionary.trygetvalue(username, out user);              if (user != null)             {                 lock (user.connectionids)                 {                      user.connectionids.removewhere(cid => cid.equals(connectionid));                      if (!user.connectionids.any())                     {                          user removeduser;                         enqueueddictionary.tryremove(username, out removeduser);                         chatsession removedchatsession;                         groupchatsessions.tryremove(username, out removedchatsession);                         updateq(removeduser.qpos);                     }                 }             }         } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -