protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); var builder = new HubConnectionBuilder(); var httpConnectionOptions = HttpConnectionFactoryInternal.createHttpConnectionOptions(); // work around constructor call httpConnectionOptions.Url = navigationManager.ToAbsoluteUri(url); builder.Services.AddSingleton <EndPoint>(new UriEndPoint(httpConnectionOptions.Url)); var opt = Microsoft.Extensions.Options.Options.Create <Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionOptions>(httpConnectionOptions); builder.Services.AddSingleton(opt); builder.Services.AddSingleton <Microsoft.AspNetCore.Connections.IConnectionFactory, HttpConnectionFactoryInternal>(); // normal stuff HubConnection hubConnection = builder.Build(); await hubConnection.StartAsync(); isConnected = true; connectionStatus = "Connected :-)"; hubConnection.Closed += async(s) => { isConnected = false; connectionStatus = "Disconnected"; await _connection.StartAsync(); isConnected = true; }; hubConnection.On <string>("notification", m => { notifications.Add(m); StateHasChanged(); }); }
private async Task SetupSignalRConnection() { //https://github.com/dotnet/aspnetcore/issues/25259#issuecomment-683143179 var builder = new HubConnectionBuilder(); var httpConnectionOptions = HttpConnectionFactoryInternal.createHttpConnectionOptions(); // work around constructor call httpConnectionOptions.Url = NavigationManager.ToAbsoluteUri("/todohub"); builder.Services.AddSingleton <EndPoint>(new UriEndPoint(httpConnectionOptions.Url)); var opt = Microsoft.Extensions.Options.Options.Create(httpConnectionOptions); builder.Services.AddSingleton(opt); builder.Services.AddSingleton <IConnectionFactory, HttpConnectionFactoryInternal>(); hubConnection = builder.Build(); hubConnection.On <TodoItem>("ReceiveTodo", (item) => { todos.Add(item); StateHasChanged(); }); await hubConnection.StartAsync(); }
private async Task CreateConnectionAsync() { // https://github.com/dotnet/aspnetcore/issues/25259 var builder = new HubConnectionBuilder(); var httpConnectionOptions = HttpConnectionFactoryInternal.CreateHttpConnectionOptions(); // work around constructor call httpConnectionOptions.AccessTokenProvider = async() => { var accessTokenResult = await _tokenProvider.RequestAccessToken(); accessTokenResult.TryGetToken(out var accessToken); return(accessToken.Value); }; httpConnectionOptions.Url = _navigationManager.ToAbsoluteUri(Shared.Constants.MessagesHubPattern); builder.Services.AddSingleton <EndPoint>(new UriEndPoint(httpConnectionOptions.Url)); var opt = Microsoft.Extensions.Options.Options.Create(httpConnectionOptions); builder.Services.AddSingleton(opt); builder.Services.AddSingleton <IConnectionFactory, HttpConnectionFactoryInternal>(); _hubConnection = builder.Build(); _hubConnection.On <Message>("ReceiveMessageAsync", OnReceiveMessageAsync); await _hubConnection.StartAsync(); }