Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using Microsoft.AspNet.SignalR;
  8. using Microsoft.AspNet.SignalR.Hubs;
  9.  
  10. namespace MultiEditWithSignalRv2.SignalR
  11. {
  12. [HubName("blogHub")]
  13. public class BlogHub : Hub
  14. {
  15. protected static ConcurrentDictionary<string, string> connectionGroups = new ConcurrentDictionary<string, string>();
  16.  
  17. /// <summary>
  18. /// The method called from SignalR client (JS)
  19. /// </summary>
  20. /// <param name="message">String Data</param>
  21. /// <param name="append">A boolean value indicating
  22. /// if incoming value should be appended or overwritten
  23. /// when sent back to other clients</param>
  24. public void Send(string message, bool append = true)
  25. {
  26. // communicate only with group of clients
  27. // Clients.Group("group-name").ClientMethod();
  28. if (connectionGroups.TryGetValue(Context.ConnectionId, out string groupName))
  29. {
  30. // send the update to everyone in the group except the caller
  31. Clients.OthersInGroup(groupName).UpdateMessage(message, append);
  32. }
  33. }
  34.  
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. /// <param name="groupName"></param>
  39. public void JoinGroup(string groupName)
  40. {
  41. var joinedGroup = connectionGroups.AddOrUpdate(Context.ConnectionId, groupName,
  42. (key, existingVal) =>
  43. {
  44. // If this delegate is invoked, then the key already exists.
  45. Groups.Remove(key, existingVal);
  46. return groupName;
  47. });
  48. Groups.Add(Context.ConnectionId, joinedGroup);
  49. // let this connection know they've joined the group
  50. Clients.Client(Context.ConnectionId).JoinedGroup(joinedGroup);
  51. // request an update from everyone else in the group
  52. Clients.OthersInGroup(joinedGroup).InitializeNewMember(Context.User.Identity.IsAuthenticated ?
  53. Context.User.Identity.Name :
  54. null);
  55. }
  56.  
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="userName"></param>
  61. /// <param name="xPos"></param>
  62. /// <param name="yPos"></param>
  63. public void MouseMove(string userName, int xPos, int yPos)
  64. {
  65. if (connectionGroups.TryGetValue(Context.ConnectionId, out string groupName))
  66. {
  67. // send the update to everyone in the group except the caller
  68. Clients.OthersInGroup(groupName).MouseMove(userName, xPos, yPos);
  69. }
  70. }
  71.  
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. public void LeaveGroup()
  76. {
  77. if (connectionGroups.TryRemove(Context.ConnectionId, out string groupName))
  78. {
  79. Groups.Remove(Context.ConnectionId, groupName);
  80. }
  81. }
  82.  
  83. public override Task OnConnected()
  84. {
  85. JoinGroup(Context.ConnectionId);
  86. return base.OnConnected();
  87. }
  88.  
  89. public override Task OnDisconnected(bool stopCalled)
  90. {
  91. LeaveGroup();
  92. return base.OnDisconnected(stopCalled);
  93. }
  94. }
  95. }
Add Comment
Please, Sign In to add comment