private async Task ConnectTask() { if (Connection == null && !IsSelf) { var builder = new HubConnectionBuilder() .WithUrl(Address + "/_cluster", HttpTransportType.WebSockets, o => { o.Headers.Add("X-SignalR-Cluster-Token", _membershipProvider.ClusterToken); o.Headers.Add("X-SignalR-Cluster-Node", Name); o.Headers.Add("X-SignalR-Cluster-Node-Id", _membershipProvider.GetOwnUniqueId()); }) .AddMessagePackProtocol(); Connection = builder.Build(); Connection.Closed += (ex) => { Connection = null; IsConnected = false; return(ConnectTask()); }; while (!_isDisposed) { try { await Connection.StartAsync(); IsConnected = true; var remoteUniqueId = await Connection.InvokeAsync <string>("GetUniqueId"); if (remoteUniqueId == _membershipProvider.GetOwnUniqueId()) { IsSelf = true; Connection.StopAsync(); } else { UniqueId = remoteUniqueId; } break; } catch { //ignore? } await Task.Delay(1000); } } }
public Task PerformAction(DirectoryAction action) { var _hub = _hubs.GetOrAdd(action.Hub, _ => new ClusterNodeHubDirectory()); lock (_syncObject) { var operationSequence = this.Sequence; if (_hub.PerformOperation(action)) { var remoteOperation = new RemoteDirectoryOperation(_membershipProvider.GetOwnUniqueId(), Sequence, action); this.Sequence = this.Sequence == Int32.MaxValue ? 0 : this.Sequence++; _actionHistory.Add(action); return(_clusterClient.BroadcastDirectoryOperation(new[] { remoteOperation })); } } return(Task.CompletedTask); }
public Task <string> GetUniqueId() { return(Task.FromResult(_membershipProvider.GetOwnUniqueId())); }