Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //OnConnected event we are
  2. //Binding all the requested client into a list
  3. public override Task OnConnected()
  4. {
  5. //Creating UserConnection object
  6. UserConnection objUserConnection = new UserConnection();
  7. //Assigning UserName from a queryString
  8. objUserConnection.UserName = Context.Request.QueryString["UserName"].ToString();
  9. //Assigning ConnectionId
  10. objUserConnection.ConnectionID = Context.ConnectionId;
  11. //Adding object into list
  12. _listUserConnetion.Add(objUserConnection);
  13. if (_listUserConnetion != null)
  14. {
  15. //Sending data to connected cliets
  16. SendData(objUserConnection.ConnectionID);
  17. }
  18. return base.OnConnected();
  19. }
  20.  
  21. //OnDisconnected we will remove connectionId
  22. //From UserConnection list
  23. //It takes near around 35 seconds to call OnDisconnect
  24. public override Task OnDisconnected(bool stopCalled)
  25. {
  26. if (!stopCalled)
  27. {
  28. //Finding Connection based on ConnectionId
  29. var userToBeRemove = _listUserConnetion.Find(o => o.ConnectionID == Context.ConnectionId);
  30. //Removing object from list
  31. _listUserConnetion.Remove(userToBeRemove);
  32. }
  33. return base.OnDisconnected(false);
  34. }
  35.  
  36. //OnReconnect Signalr send the data to user
  37. public override Task OnReconnected()
  38. {
  39. //Check if UserExist
  40. var checkUserIsExists = _listUserConnetion.Find(o => o.ConnectionID == Context.ConnectionId);
  41. if (checkUserIsExists != null)
  42. {
  43. //Send the data
  44. SendData(checkUserIsExists.ConnectionID);
  45. }
  46. return base.OnReconnected();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement