/// <summary> /// Called when a connection has disconnected gracefully from this hub instance, /// i.e. stop was called on the client. /// </summary> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> /// </returns> public override Task OnDisconnected(bool stopCalled) { var name = ((ServiceUser)Context.User).Id.Split(':')[1]; var table = GetConnectionTable(); try { if (!string.IsNullOrEmpty(name)) { var deleteOperation = TableOperation.Delete( new SignalRUserStore(name, Context.ConnectionId) { ETag = "*" }); TableOperation retrieveOperation = TableOperation.Retrieve <SignalRUserStore>(name, Context.ConnectionId); TableResult retrievedResult = table.Execute(retrieveOperation); SignalRUserStore checkEntity = retrievedResult.Result as SignalRUserStore; if (checkEntity != null) { table.Execute(deleteOperation); } } } catch (Exception ex) { //throw; } return(base.OnDisconnected(stopCalled)); }
/// <summary> /// Called when the connection connects to this hub instance. /// Code taken from: /// http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections /// </summary> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> /// </returns> public override Task OnConnected() { try { var currentUser = ((ServiceUser)Context.User).Id; var id = Context.ConnectionId; var table = GetConnectionTable(); table.CreateIfNotExists(); var entity = new SignalRUserStore( currentUser.Split(':')[1], Context.ConnectionId); var insertOperation = TableOperation.InsertOrReplace(entity); table.Execute(insertOperation); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } return(base.OnConnected()); }