├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── Microsoft.AspNetCore.SignalR.LoadBalancing ├── Microsoft.AspNetCore.SignalR.LoadBalancing.csproj └── SignalRSessionAffinity.cs ├── Microsoft.Extensions.ServiceDiscovery ├── Microsoft.Extensions.ServiceDiscovery.csproj └── ServiceDiscovery.cs ├── README.md ├── Sample ├── Hubs │ └── Chat.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Sample.csproj ├── appsettings.Development.json ├── appsettings.json ├── libman.json └── wwwroot │ ├── index.html │ └── microsoft-signalr │ ├── signalr.js │ └── signalr.min.js ├── SignalRClient ├── Program.cs └── SignalRClient.csproj ├── SmartRouter.sln ├── Yarp.Ingress ├── Program.cs ├── Properties │ └── launchSettings.json ├── Yarp.Ingress.csproj ├── appsettings.Development.json └── appsettings.json └── tye.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.tlog 97 | *.vspscc 98 | *.vssscc 99 | .builds 100 | *.pidb 101 | *.svclog 102 | *.scc 103 | 104 | # Chutzpah Test files 105 | _Chutzpah* 106 | 107 | # Visual C++ cache files 108 | ipch/ 109 | *.aps 110 | *.ncb 111 | *.opendb 112 | *.opensdf 113 | *.sdf 114 | *.cachefile 115 | *.VC.db 116 | *.VC.VC.opendb 117 | 118 | # Visual Studio profiler 119 | *.psess 120 | *.vsp 121 | *.vspx 122 | *.sap 123 | 124 | # Visual Studio Trace Files 125 | *.e2e 126 | 127 | # TFS 2012 Local Workspace 128 | $tf/ 129 | 130 | # Guidance Automation Toolkit 131 | *.gpState 132 | 133 | # ReSharper is a .NET coding add-in 134 | _ReSharper*/ 135 | *.[Rr]e[Ss]harper 136 | *.DotSettings.user 137 | 138 | # TeamCity is a build add-in 139 | _TeamCity* 140 | 141 | # DotCover is a Code Coverage Tool 142 | *.dotCover 143 | 144 | # AxoCover is a Code Coverage Tool 145 | .axoCover/* 146 | !.axoCover/settings.json 147 | 148 | # Coverlet is a free, cross platform Code Coverage Tool 149 | coverage*.json 150 | coverage*.xml 151 | coverage*.info 152 | 153 | # Visual Studio code coverage results 154 | *.coverage 155 | *.coveragexml 156 | 157 | # NCrunch 158 | _NCrunch_* 159 | .*crunch*.local.xml 160 | nCrunchTemp_* 161 | 162 | # MightyMoose 163 | *.mm.* 164 | AutoTest.Net/ 165 | 166 | # Web workbench (sass) 167 | .sass-cache/ 168 | 169 | # Installshield output folder 170 | [Ee]xpress/ 171 | 172 | # DocProject is a documentation generator add-in 173 | DocProject/buildhelp/ 174 | DocProject/Help/*.HxT 175 | DocProject/Help/*.HxC 176 | DocProject/Help/*.hhc 177 | DocProject/Help/*.hhk 178 | DocProject/Help/*.hhp 179 | DocProject/Help/Html2 180 | DocProject/Help/html 181 | 182 | # Click-Once directory 183 | publish/ 184 | 185 | # Publish Web Output 186 | *.[Pp]ublish.xml 187 | *.azurePubxml 188 | # Note: Comment the next line if you want to checkin your web deploy settings, 189 | # but database connection strings (with potential passwords) will be unencrypted 190 | *.pubxml 191 | *.publishproj 192 | 193 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 194 | # checkin your Azure Web App publish settings, but sensitive information contained 195 | # in these scripts will be unencrypted 196 | PublishScripts/ 197 | 198 | # NuGet Packages 199 | *.nupkg 200 | # NuGet Symbol Packages 201 | *.snupkg 202 | # The packages folder can be ignored because of Package Restore 203 | **/[Pp]ackages/* 204 | # except build/, which is used as an MSBuild target. 205 | !**/[Pp]ackages/build/ 206 | # Uncomment if necessary however generally it will be regenerated when needed 207 | #!**/[Pp]ackages/repositories.config 208 | # NuGet v3's project.json files produces more ignorable files 209 | *.nuget.props 210 | *.nuget.targets 211 | 212 | # Microsoft Azure Build Output 213 | csx/ 214 | *.build.csdef 215 | 216 | # Microsoft Azure Emulator 217 | ecf/ 218 | rcf/ 219 | 220 | # Windows Store app package directories and files 221 | AppPackages/ 222 | BundleArtifacts/ 223 | Package.StoreAssociation.xml 224 | _pkginfo.txt 225 | *.appx 226 | *.appxbundle 227 | *.appxupload 228 | 229 | # Visual Studio cache files 230 | # files ending in .cache can be ignored 231 | *.[Cc]ache 232 | # but keep track of directories ending in .cache 233 | !?*.[Cc]ache/ 234 | 235 | # Others 236 | ClientBin/ 237 | ~$* 238 | *~ 239 | *.dbmdl 240 | *.dbproj.schemaview 241 | *.jfm 242 | *.pfx 243 | *.publishsettings 244 | orleans.codegen.cs 245 | 246 | # Including strong name files can present a security risk 247 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 248 | #*.snk 249 | 250 | # Since there are multiple workflows, uncomment next line to ignore bower_components 251 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 252 | #bower_components/ 253 | 254 | # RIA/Silverlight projects 255 | Generated_Code/ 256 | 257 | # Backup & report files from converting an old project file 258 | # to a newer Visual Studio version. Backup files are not needed, 259 | # because we have git ;-) 260 | _UpgradeReport_Files/ 261 | Backup*/ 262 | UpgradeLog*.XML 263 | UpgradeLog*.htm 264 | ServiceFabricBackup/ 265 | *.rptproj.bak 266 | 267 | # SQL Server files 268 | *.mdf 269 | *.ldf 270 | *.ndf 271 | 272 | # Business Intelligence projects 273 | *.rdl.data 274 | *.bim.layout 275 | *.bim_*.settings 276 | *.rptproj.rsuser 277 | *- [Bb]ackup.rdl 278 | *- [Bb]ackup ([0-9]).rdl 279 | *- [Bb]ackup ([0-9][0-9]).rdl 280 | 281 | # Microsoft Fakes 282 | FakesAssemblies/ 283 | 284 | # GhostDoc plugin setting file 285 | *.GhostDoc.xml 286 | 287 | # Node.js Tools for Visual Studio 288 | .ntvs_analysis.dat 289 | node_modules/ 290 | 291 | # Visual Studio 6 build log 292 | *.plg 293 | 294 | # Visual Studio 6 workspace options file 295 | *.opt 296 | 297 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 298 | *.vbw 299 | 300 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 301 | *.vbp 302 | 303 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 304 | *.dsw 305 | *.dsp 306 | 307 | # Visual Studio 6 technical files 308 | *.ncb 309 | *.aps 310 | 311 | # Visual Studio LightSwitch build output 312 | **/*.HTMLClient/GeneratedArtifacts 313 | **/*.DesktopClient/GeneratedArtifacts 314 | **/*.DesktopClient/ModelManifest.xml 315 | **/*.Server/GeneratedArtifacts 316 | **/*.Server/ModelManifest.xml 317 | _Pvt_Extensions 318 | 319 | # Paket dependency manager 320 | .paket/paket.exe 321 | paket-files/ 322 | 323 | # FAKE - F# Make 324 | .fake/ 325 | 326 | # CodeRush personal settings 327 | .cr/personal 328 | 329 | # Python Tools for Visual Studio (PTVS) 330 | __pycache__/ 331 | *.pyc 332 | 333 | # Cake - Uncomment if you are using it 334 | # tools/** 335 | # !tools/packages.config 336 | 337 | # Tabs Studio 338 | *.tss 339 | 340 | # Telerik's JustMock configuration file 341 | *.jmconfig 342 | 343 | # BizTalk build output 344 | *.btp.cs 345 | *.btm.cs 346 | *.odx.cs 347 | *.xsd.cs 348 | 349 | # OpenCover UI analysis results 350 | OpenCover/ 351 | 352 | # Azure Stream Analytics local run output 353 | ASALocalRun/ 354 | 355 | # MSBuild Binary and Structured Log 356 | *.binlog 357 | 358 | # NVidia Nsight GPU debugger configuration file 359 | *.nvuser 360 | 361 | # MFractors (Xamarin productivity tool) working folder 362 | .mfractor/ 363 | 364 | # Local History for Visual Studio 365 | .localhistory/ 366 | 367 | # Visual Studio History (VSHistory) files 368 | .vshistory/ 369 | 370 | # BeatPulse healthcheck temp database 371 | healthchecksdb 372 | 373 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 374 | MigrationBackup/ 375 | 376 | # Ionide (cross platform F# VS Code tools) working folder 377 | .ionide/ 378 | 379 | # Fody - auto-generated XML schema 380 | FodyWeavers.xsd 381 | 382 | # VS Code files for those working on multiple tools 383 | .vscode/* 384 | !.vscode/settings.json 385 | !.vscode/tasks.json 386 | !.vscode/launch.json 387 | !.vscode/extensions.json 388 | *.code-workspace 389 | 390 | # Local History for Visual Studio Code 391 | .history/ 392 | 393 | # Windows Installer files from build outputs 394 | *.cab 395 | *.msi 396 | *.msix 397 | *.msm 398 | *.msp 399 | 400 | # JetBrains Rider 401 | *.sln.iml 402 | 403 | ## 404 | ## Visual studio for Mac 405 | ## 406 | 407 | 408 | # globs 409 | Makefile.in 410 | *.userprefs 411 | *.usertasks 412 | config.make 413 | config.status 414 | aclocal.m4 415 | install-sh 416 | autom4te.cache/ 417 | *.tar.gz 418 | tarballs/ 419 | test-results/ 420 | 421 | # Mac bundle stuff 422 | *.dmg 423 | *.app 424 | 425 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 426 | # General 427 | .DS_Store 428 | .AppleDouble 429 | .LSOverride 430 | 431 | # Icon must end with two \r 432 | Icon 433 | 434 | 435 | # Thumbnails 436 | ._* 437 | 438 | # Files that might appear in the root of a volume 439 | .DocumentRevisions-V100 440 | .fseventsd 441 | .Spotlight-V100 442 | .TemporaryItems 443 | .Trashes 444 | .VolumeIcon.icns 445 | .com.apple.timemachine.donotpresent 446 | 447 | # Directories potentially created on remote AFP share 448 | .AppleDB 449 | .AppleDesktop 450 | Network Trash Folder 451 | Temporary Items 452 | .apdisk 453 | 454 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 455 | # Windows thumbnail cache files 456 | Thumbs.db 457 | ehthumbs.db 458 | ehthumbs_vista.db 459 | 460 | # Dump file 461 | *.stackdump 462 | 463 | # Folder config file 464 | [Dd]esktop.ini 465 | 466 | # Recycle Bin used on file shares 467 | $RECYCLE.BIN/ 468 | 469 | # Windows Installer files 470 | *.cab 471 | *.msi 472 | *.msix 473 | *.msm 474 | *.msp 475 | 476 | # Windows shortcuts 477 | *.lnk 478 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/Sample/bin/Debug/net7.0/Sample.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/Sample", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/Sample/Sample.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/Sample/Sample.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/Sample/Sample.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /Microsoft.AspNetCore.SignalR.LoadBalancing/Microsoft.AspNetCore.SignalR.LoadBalancing.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Microsoft.AspNetCore.SignalR.LoadBalancing/SignalRSessionAffinity.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | using System.Text.Json.Serialization; 5 | using Microsoft.AspNetCore.Http; 6 | using Microsoft.AspNetCore.Http.Extensions; 7 | using Microsoft.Extensions.Primitives; 8 | using Yarp.ReverseProxy.Configuration; 9 | using Yarp.ReverseProxy.Model; 10 | using Yarp.ReverseProxy.SessionAffinity; 11 | using Yarp.ReverseProxy.Transforms; 12 | 13 | namespace Microsoft.Extensions.DependencyInjection; 14 | 15 | public static class SignalRSessionAffinity 16 | { 17 | public static IReverseProxyBuilder AddSignalRSessionAffinity(this IReverseProxyBuilder builder) 18 | { 19 | builder.Services.AddSingleton(); 20 | builder.Services.AddSingleton(); 21 | 22 | builder.AddTransforms(transforms => 23 | { 24 | if (transforms.Route.Metadata is { } metatada && metatada.ContainsKey("hub") is true) 25 | { 26 | // With YARP 2.0, request transforms can write short circuit the request 27 | transforms.AddResponseTransform(async c => 28 | { 29 | c.SuppressResponseBody = await AffinitizeNegotiateRequest(c.HttpContext); 30 | }); 31 | } 32 | }); 33 | 34 | return builder; 35 | } 36 | 37 | private static async Task AffinitizeNegotiateRequest(HttpContext httpContext) 38 | { 39 | // Check if should be affinitizing this route 40 | if (httpContext.GetReverseProxyFeature() is { } proxyFeature && 41 | proxyFeature is { Cluster.Config.SessionAffinity.AffinityKeyName: var affinityKey } && 42 | StringValues.IsNullOrEmpty(httpContext.Request.Query[affinityKey])) 43 | { 44 | var destination = (proxyFeature.ProxiedDestination, proxyFeature.AvailableDestinations) switch 45 | { 46 | (DestinationState proxied, _) => proxied, 47 | (_, [var one]) => one, 48 | (_, var many) => many[Random.Shared.Next(many.Count)], 49 | }; 50 | 51 | var hashes = httpContext.RequestServices.GetRequiredService(); 52 | 53 | // Redirect to the same URL with the destination hash added to the query string 54 | var req = httpContext.Request; 55 | var query = req.QueryString.Add(affinityKey, hashes.GetDestinationHash(destination)); 56 | 57 | var url = UriHelper.BuildAbsolute(req.Scheme, req.Host, req.PathBase, new(req.Path.Value!.Replace("/negotiate", "")), query); 58 | 59 | // The negoitate response supports redirecting the client to another URL, we're taking advantage of that here 60 | // to affinitize the request. https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/TransportProtocols.md#all-versions 61 | // back to whatever destination we landed on initially. 62 | 63 | // This will force a renegotiation. 64 | 65 | // This can be improved by natively supporting round tripping an affinity token from the response header 66 | // into the query string. 67 | 68 | httpContext.Response.Clear(); 69 | await httpContext.Response.WriteAsJsonAsync(new RedirectResponse(url), JsonContext.Default.RedirectResponse); 70 | 71 | return true; 72 | } 73 | return false; 74 | 75 | } 76 | 77 | private sealed class SignalRAffinity : ISessionAffinityPolicy 78 | { 79 | private readonly DestinationHashes _hashes; 80 | 81 | public SignalRAffinity(DestinationHashes hashes) => _hashes = hashes; 82 | 83 | public string Name => "SignalR"; 84 | 85 | public void AffinitizeResponse(HttpContext context, ClusterState cluster, SessionAffinityConfig config, DestinationState destination) 86 | { 87 | // Nothing is written to the response 88 | } 89 | 90 | public AffinityResult FindAffinitizedDestinations(HttpContext context, ClusterState cluster, SessionAffinityConfig config, IReadOnlyList destinations) 91 | { 92 | string? affinity = context.Request.Query[config.AffinityKeyName]; 93 | if (affinity is not null) 94 | { 95 | foreach (var d in destinations) 96 | { 97 | var hash = _hashes.GetDestinationHash(d); 98 | 99 | if (hash == affinity) 100 | { 101 | return new(d, AffinityStatus.OK); 102 | } 103 | } 104 | 105 | return new(null, AffinityStatus.DestinationNotFound); 106 | } 107 | 108 | return new(null, AffinityStatus.AffinityKeyNotSet); 109 | } 110 | } 111 | private sealed class DestinationHashes 112 | { 113 | private readonly ConditionalWeakTable _hashes = new(); 114 | 115 | public string GetDestinationHash(DestinationState destination) 116 | { 117 | return _hashes.GetValue(destination, static d => 118 | { 119 | var destinationUtf8Bytes = Encoding.UTF8.GetBytes(d.DestinationId.ToUpperInvariant()); 120 | return Convert.ToHexString(SHA256.HashData(destinationUtf8Bytes)).ToLowerInvariant(); 121 | }); 122 | } 123 | } 124 | } 125 | 126 | internal record struct RedirectResponse(string Url); 127 | 128 | [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] 129 | [JsonSerializable(typeof(RedirectResponse))] 130 | partial class JsonContext : JsonSerializerContext 131 | { 132 | 133 | } -------------------------------------------------------------------------------- /Microsoft.Extensions.ServiceDiscovery/Microsoft.Extensions.ServiceDiscovery.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Microsoft.Extensions.ServiceDiscovery/ServiceDiscovery.cs: -------------------------------------------------------------------------------- 1 | using System.Net.Http.Json; 2 | using System.Text.Json.Nodes; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Microsoft.Extensions.ServiceDiscovery; 7 | 8 | public static class ServiceDiscoveryExtensions 9 | { 10 | public static IServiceCollection AddTyeSeviceDiscvery(this IServiceCollection services) => 11 | services.AddSingleton(); 12 | 13 | } 14 | 15 | public interface IServiceDiscovery 16 | { 17 | ValueTask> GetAddressesAsync(string name); 18 | 19 | ValueTask GetAddressAsync(string name); 20 | } 21 | 22 | public record struct Replica(string Name, Uri Address); 23 | 24 | public class TyeServiceDiscovery : IServiceDiscovery 25 | { 26 | private readonly IConfiguration _configuration; 27 | private readonly HttpClient _client = new HttpClient(); 28 | 29 | public TyeServiceDiscovery(IConfiguration configuration) 30 | { 31 | _configuration = configuration; 32 | } 33 | 34 | public ValueTask GetAddressAsync(string name) 35 | { 36 | return ValueTask.FromResult(_configuration.GetServiceUri(name)); 37 | } 38 | 39 | public async ValueTask> GetAddressesAsync(string name) 40 | { 41 | // Quick check to see if this is even a service 42 | if (_configuration.GetServiceUri(name) is null) 43 | { 44 | return Array.Empty(); 45 | } 46 | 47 | // TODO: A TYE_HOST variable should be injected so we don't hard code 8000 48 | var serviceDefinition = await _client.GetFromJsonAsync($"http://127.0.0.1:8000/api/v1/services/{name}"); 49 | 50 | List? replicas = null; 51 | foreach (var (key, replica) in serviceDefinition!["replicas"]!.AsObject()) 52 | { 53 | var httpPort = replica!["ports"]!.AsArray().First(); 54 | var replicaAddress = $"http://127.0.0.1:{httpPort}"; 55 | 56 | replicas ??= new(); 57 | replicas.Add(new(key, new(replicaAddress))); 58 | } 59 | return replicas?.ToArray() ?? Array.Empty(); 60 | } 61 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Smart load balancing for SignalR 2 | 3 | When running SignalR behind a load balancer, it requires sticky sessions as SignalR is an inherently stateful technology. This can prove difficult in some pieces of infrastructure since it may be shared for all sorts of applications. 4 | 5 | This is a SignalR aware [YARP](https://github.com/microsoft/reverse-proxy/) session affinity provider that can be used to affinitize signalr 6 | connections. 7 | 8 | ## Running the Sample 9 | 10 | This requires [tye](https://github.com/dotnet/tye), which can be installed with the following command: 11 | 12 | ``` 13 | dotnet tool install --global Microsoft.Tye --version 0.11.0-alpha.22111.1 14 | ``` 15 | 16 | Run `tye run` in the root of the repository and it will launch 2 instances of the application, a load balancer 17 | and a redis container. 18 | 19 | Here's how it works: 20 | 21 | ![image](https://user-images.githubusercontent.com/95136/218275323-0c8f496e-976d-436e-b5ec-5f4bfe26500e.png) 22 | -------------------------------------------------------------------------------- /Sample/Hubs/Chat.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.SignalR; 2 | 3 | namespace Sample; 4 | 5 | class Chat : Hub 6 | { 7 | public Task Send(string s) 8 | { 9 | return Clients.All.SendAsync("Send", s); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Sample/Program.cs: -------------------------------------------------------------------------------- 1 | using Sample; 2 | 3 | var builder = WebApplication.CreateBuilder(args); 4 | 5 | var redisConnection = builder.Configuration.GetConnectionString("redis"); 6 | 7 | var signalr = builder.Services.AddSignalR(); 8 | 9 | if (redisConnection is not null) 10 | { 11 | signalr.AddStackExchangeRedis(redisConnection); 12 | } 13 | 14 | var app = builder.Build(); 15 | 16 | app.UseFileServer(); 17 | 18 | app.MapHub("/chat"); 19 | 20 | app.Run(); 21 | -------------------------------------------------------------------------------- /Sample/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:33290", 7 | "sslPort": 44350 8 | } 9 | }, 10 | "profiles": { 11 | "http": { 12 | "commandName": "Project", 13 | "dotnetRunMessages": true, 14 | "launchBrowser": true, 15 | "applicationUrl": "http://localhost:5041", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "https": { 21 | "commandName": "Project", 22 | "dotnetRunMessages": true, 23 | "launchBrowser": true, 24 | "applicationUrl": "https://localhost:7085;http://localhost:5041", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | }, 29 | "IIS Express": { 30 | "commandName": "IISExpress", 31 | "launchBrowser": true, 32 | "environmentVariables": { 33 | "ASPNETCORE_ENVIRONMENT": "Development" 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sample/Sample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Sample/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Information" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Sample/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*" 9 | } 10 | -------------------------------------------------------------------------------- /Sample/libman.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0", 3 | "defaultProvider": "cdnjs", 4 | "libraries": [ 5 | { 6 | "library": "microsoft-signalr@7.0.0", 7 | "destination": "wwwroot/microsoft-signalr/" 8 | } 9 | ] 10 | } -------------------------------------------------------------------------------- /Sample/wwwroot/index.html: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 |
15 | 16 |
    17 |
18 | 19 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Sample/wwwroot/microsoft-signalr/signalr.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define([], factory); 6 | else if(typeof exports === 'object') 7 | exports["signalR"] = factory(); 8 | else 9 | root["signalR"] = factory(); 10 | })(self, () => { 11 | return /******/ (() => { // webpackBootstrap 12 | /******/ "use strict"; 13 | /******/ // The require scope 14 | /******/ var __webpack_require__ = {}; 15 | /******/ 16 | /************************************************************************/ 17 | /******/ /* webpack/runtime/define property getters */ 18 | /******/ (() => { 19 | /******/ // define getter functions for harmony exports 20 | /******/ __webpack_require__.d = (exports, definition) => { 21 | /******/ for(var key in definition) { 22 | /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { 23 | /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); 24 | /******/ } 25 | /******/ } 26 | /******/ }; 27 | /******/ })(); 28 | /******/ 29 | /******/ /* webpack/runtime/global */ 30 | /******/ (() => { 31 | /******/ __webpack_require__.g = (function() { 32 | /******/ if (typeof globalThis === 'object') return globalThis; 33 | /******/ try { 34 | /******/ return this || new Function('return this')(); 35 | /******/ } catch (e) { 36 | /******/ if (typeof window === 'object') return window; 37 | /******/ } 38 | /******/ })(); 39 | /******/ })(); 40 | /******/ 41 | /******/ /* webpack/runtime/hasOwnProperty shorthand */ 42 | /******/ (() => { 43 | /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) 44 | /******/ })(); 45 | /******/ 46 | /******/ /* webpack/runtime/make namespace object */ 47 | /******/ (() => { 48 | /******/ // define __esModule on exports 49 | /******/ __webpack_require__.r = (exports) => { 50 | /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { 51 | /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); 52 | /******/ } 53 | /******/ Object.defineProperty(exports, '__esModule', { value: true }); 54 | /******/ }; 55 | /******/ })(); 56 | /******/ 57 | /************************************************************************/ 58 | var __webpack_exports__ = {}; 59 | // ESM COMPAT FLAG 60 | __webpack_require__.r(__webpack_exports__); 61 | 62 | // EXPORTS 63 | __webpack_require__.d(__webpack_exports__, { 64 | "AbortError": () => (/* reexport */ AbortError), 65 | "DefaultHttpClient": () => (/* reexport */ DefaultHttpClient), 66 | "HttpClient": () => (/* reexport */ HttpClient), 67 | "HttpError": () => (/* reexport */ HttpError), 68 | "HttpResponse": () => (/* reexport */ HttpResponse), 69 | "HttpTransportType": () => (/* reexport */ HttpTransportType), 70 | "HubConnection": () => (/* reexport */ HubConnection), 71 | "HubConnectionBuilder": () => (/* reexport */ HubConnectionBuilder), 72 | "HubConnectionState": () => (/* reexport */ HubConnectionState), 73 | "JsonHubProtocol": () => (/* reexport */ JsonHubProtocol), 74 | "LogLevel": () => (/* reexport */ LogLevel), 75 | "MessageType": () => (/* reexport */ MessageType), 76 | "NullLogger": () => (/* reexport */ NullLogger), 77 | "Subject": () => (/* reexport */ Subject), 78 | "TimeoutError": () => (/* reexport */ TimeoutError), 79 | "TransferFormat": () => (/* reexport */ TransferFormat), 80 | "VERSION": () => (/* reexport */ VERSION) 81 | }); 82 | 83 | ;// CONCATENATED MODULE: ./src/Errors.ts 84 | // Licensed to the .NET Foundation under one or more agreements. 85 | // The .NET Foundation licenses this file to you under the MIT license. 86 | /** Error thrown when an HTTP request fails. */ 87 | class HttpError extends Error { 88 | /** Constructs a new instance of {@link @microsoft/signalr.HttpError}. 89 | * 90 | * @param {string} errorMessage A descriptive error message. 91 | * @param {number} statusCode The HTTP status code represented by this error. 92 | */ 93 | constructor(errorMessage, statusCode) { 94 | const trueProto = new.target.prototype; 95 | super(`${errorMessage}: Status code '${statusCode}'`); 96 | this.statusCode = statusCode; 97 | // Workaround issue in Typescript compiler 98 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 99 | this.__proto__ = trueProto; 100 | } 101 | } 102 | /** Error thrown when a timeout elapses. */ 103 | class TimeoutError extends Error { 104 | /** Constructs a new instance of {@link @microsoft/signalr.TimeoutError}. 105 | * 106 | * @param {string} errorMessage A descriptive error message. 107 | */ 108 | constructor(errorMessage = "A timeout occurred.") { 109 | const trueProto = new.target.prototype; 110 | super(errorMessage); 111 | // Workaround issue in Typescript compiler 112 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 113 | this.__proto__ = trueProto; 114 | } 115 | } 116 | /** Error thrown when an action is aborted. */ 117 | class AbortError extends Error { 118 | /** Constructs a new instance of {@link AbortError}. 119 | * 120 | * @param {string} errorMessage A descriptive error message. 121 | */ 122 | constructor(errorMessage = "An abort occurred.") { 123 | const trueProto = new.target.prototype; 124 | super(errorMessage); 125 | // Workaround issue in Typescript compiler 126 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 127 | this.__proto__ = trueProto; 128 | } 129 | } 130 | /** Error thrown when the selected transport is unsupported by the browser. */ 131 | /** @private */ 132 | class UnsupportedTransportError extends Error { 133 | /** Constructs a new instance of {@link @microsoft/signalr.UnsupportedTransportError}. 134 | * 135 | * @param {string} message A descriptive error message. 136 | * @param {HttpTransportType} transport The {@link @microsoft/signalr.HttpTransportType} this error occurred on. 137 | */ 138 | constructor(message, transport) { 139 | const trueProto = new.target.prototype; 140 | super(message); 141 | this.transport = transport; 142 | this.errorType = 'UnsupportedTransportError'; 143 | // Workaround issue in Typescript compiler 144 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 145 | this.__proto__ = trueProto; 146 | } 147 | } 148 | /** Error thrown when the selected transport is disabled by the browser. */ 149 | /** @private */ 150 | class DisabledTransportError extends Error { 151 | /** Constructs a new instance of {@link @microsoft/signalr.DisabledTransportError}. 152 | * 153 | * @param {string} message A descriptive error message. 154 | * @param {HttpTransportType} transport The {@link @microsoft/signalr.HttpTransportType} this error occurred on. 155 | */ 156 | constructor(message, transport) { 157 | const trueProto = new.target.prototype; 158 | super(message); 159 | this.transport = transport; 160 | this.errorType = 'DisabledTransportError'; 161 | // Workaround issue in Typescript compiler 162 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 163 | this.__proto__ = trueProto; 164 | } 165 | } 166 | /** Error thrown when the selected transport cannot be started. */ 167 | /** @private */ 168 | class FailedToStartTransportError extends Error { 169 | /** Constructs a new instance of {@link @microsoft/signalr.FailedToStartTransportError}. 170 | * 171 | * @param {string} message A descriptive error message. 172 | * @param {HttpTransportType} transport The {@link @microsoft/signalr.HttpTransportType} this error occurred on. 173 | */ 174 | constructor(message, transport) { 175 | const trueProto = new.target.prototype; 176 | super(message); 177 | this.transport = transport; 178 | this.errorType = 'FailedToStartTransportError'; 179 | // Workaround issue in Typescript compiler 180 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 181 | this.__proto__ = trueProto; 182 | } 183 | } 184 | /** Error thrown when the negotiation with the server failed to complete. */ 185 | /** @private */ 186 | class FailedToNegotiateWithServerError extends Error { 187 | /** Constructs a new instance of {@link @microsoft/signalr.FailedToNegotiateWithServerError}. 188 | * 189 | * @param {string} message A descriptive error message. 190 | */ 191 | constructor(message) { 192 | const trueProto = new.target.prototype; 193 | super(message); 194 | this.errorType = 'FailedToNegotiateWithServerError'; 195 | // Workaround issue in Typescript compiler 196 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 197 | this.__proto__ = trueProto; 198 | } 199 | } 200 | /** Error thrown when multiple errors have occurred. */ 201 | /** @private */ 202 | class AggregateErrors extends Error { 203 | /** Constructs a new instance of {@link @microsoft/signalr.AggregateErrors}. 204 | * 205 | * @param {string} message A descriptive error message. 206 | * @param {Error[]} innerErrors The collection of errors this error is aggregating. 207 | */ 208 | constructor(message, innerErrors) { 209 | const trueProto = new.target.prototype; 210 | super(message); 211 | this.innerErrors = innerErrors; 212 | // Workaround issue in Typescript compiler 213 | // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 214 | this.__proto__ = trueProto; 215 | } 216 | } 217 | 218 | ;// CONCATENATED MODULE: ./src/HttpClient.ts 219 | // Licensed to the .NET Foundation under one or more agreements. 220 | // The .NET Foundation licenses this file to you under the MIT license. 221 | /** Represents an HTTP response. */ 222 | class HttpResponse { 223 | constructor(statusCode, statusText, content) { 224 | this.statusCode = statusCode; 225 | this.statusText = statusText; 226 | this.content = content; 227 | } 228 | } 229 | /** Abstraction over an HTTP client. 230 | * 231 | * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms. 232 | */ 233 | class HttpClient { 234 | get(url, options) { 235 | return this.send({ 236 | ...options, 237 | method: "GET", 238 | url, 239 | }); 240 | } 241 | post(url, options) { 242 | return this.send({ 243 | ...options, 244 | method: "POST", 245 | url, 246 | }); 247 | } 248 | delete(url, options) { 249 | return this.send({ 250 | ...options, 251 | method: "DELETE", 252 | url, 253 | }); 254 | } 255 | /** Gets all cookies that apply to the specified URL. 256 | * 257 | * @param url The URL that the cookies are valid for. 258 | * @returns {string} A string containing all the key-value cookie pairs for the specified URL. 259 | */ 260 | // @ts-ignore 261 | getCookieString(url) { 262 | return ""; 263 | } 264 | } 265 | 266 | ;// CONCATENATED MODULE: ./src/ILogger.ts 267 | // Licensed to the .NET Foundation under one or more agreements. 268 | // The .NET Foundation licenses this file to you under the MIT license. 269 | // These values are designed to match the ASP.NET Log Levels since that's the pattern we're emulating here. 270 | /** Indicates the severity of a log message. 271 | * 272 | * Log Levels are ordered in increasing severity. So `Debug` is more severe than `Trace`, etc. 273 | */ 274 | var LogLevel; 275 | (function (LogLevel) { 276 | /** Log level for very low severity diagnostic messages. */ 277 | LogLevel[LogLevel["Trace"] = 0] = "Trace"; 278 | /** Log level for low severity diagnostic messages. */ 279 | LogLevel[LogLevel["Debug"] = 1] = "Debug"; 280 | /** Log level for informational diagnostic messages. */ 281 | LogLevel[LogLevel["Information"] = 2] = "Information"; 282 | /** Log level for diagnostic messages that indicate a non-fatal problem. */ 283 | LogLevel[LogLevel["Warning"] = 3] = "Warning"; 284 | /** Log level for diagnostic messages that indicate a failure in the current operation. */ 285 | LogLevel[LogLevel["Error"] = 4] = "Error"; 286 | /** Log level for diagnostic messages that indicate a failure that will terminate the entire application. */ 287 | LogLevel[LogLevel["Critical"] = 5] = "Critical"; 288 | /** The highest possible log level. Used when configuring logging to indicate that no log messages should be emitted. */ 289 | LogLevel[LogLevel["None"] = 6] = "None"; 290 | })(LogLevel || (LogLevel = {})); 291 | 292 | ;// CONCATENATED MODULE: ./src/Loggers.ts 293 | // Licensed to the .NET Foundation under one or more agreements. 294 | // The .NET Foundation licenses this file to you under the MIT license. 295 | /** A logger that does nothing when log messages are sent to it. */ 296 | class NullLogger { 297 | constructor() { } 298 | /** @inheritDoc */ 299 | // eslint-disable-next-line 300 | log(_logLevel, _message) { 301 | } 302 | } 303 | /** The singleton instance of the {@link @microsoft/signalr.NullLogger}. */ 304 | NullLogger.instance = new NullLogger(); 305 | 306 | ;// CONCATENATED MODULE: ./src/Utils.ts 307 | // Licensed to the .NET Foundation under one or more agreements. 308 | // The .NET Foundation licenses this file to you under the MIT license. 309 | 310 | 311 | // Version token that will be replaced by the prepack command 312 | /** The version of the SignalR client. */ 313 | const VERSION = "7.0.0"; 314 | /** @private */ 315 | class Arg { 316 | static isRequired(val, name) { 317 | if (val === null || val === undefined) { 318 | throw new Error(`The '${name}' argument is required.`); 319 | } 320 | } 321 | static isNotEmpty(val, name) { 322 | if (!val || val.match(/^\s*$/)) { 323 | throw new Error(`The '${name}' argument should not be empty.`); 324 | } 325 | } 326 | static isIn(val, values, name) { 327 | // TypeScript enums have keys for **both** the name and the value of each enum member on the type itself. 328 | if (!(val in values)) { 329 | throw new Error(`Unknown ${name} value: ${val}.`); 330 | } 331 | } 332 | } 333 | /** @private */ 334 | class Platform { 335 | // react-native has a window but no document so we should check both 336 | static get isBrowser() { 337 | return typeof window === "object" && typeof window.document === "object"; 338 | } 339 | // WebWorkers don't have a window object so the isBrowser check would fail 340 | static get isWebWorker() { 341 | return typeof self === "object" && "importScripts" in self; 342 | } 343 | // react-native has a window but no document 344 | static get isReactNative() { 345 | return typeof window === "object" && typeof window.document === "undefined"; 346 | } 347 | // Node apps shouldn't have a window object, but WebWorkers don't either 348 | // so we need to check for both WebWorker and window 349 | static get isNode() { 350 | return !this.isBrowser && !this.isWebWorker && !this.isReactNative; 351 | } 352 | } 353 | /** @private */ 354 | function getDataDetail(data, includeContent) { 355 | let detail = ""; 356 | if (isArrayBuffer(data)) { 357 | detail = `Binary data of length ${data.byteLength}`; 358 | if (includeContent) { 359 | detail += `. Content: '${formatArrayBuffer(data)}'`; 360 | } 361 | } 362 | else if (typeof data === "string") { 363 | detail = `String data of length ${data.length}`; 364 | if (includeContent) { 365 | detail += `. Content: '${data}'`; 366 | } 367 | } 368 | return detail; 369 | } 370 | /** @private */ 371 | function formatArrayBuffer(data) { 372 | const view = new Uint8Array(data); 373 | // Uint8Array.map only supports returning another Uint8Array? 374 | let str = ""; 375 | view.forEach((num) => { 376 | const pad = num < 16 ? "0" : ""; 377 | str += `0x${pad}${num.toString(16)} `; 378 | }); 379 | // Trim of trailing space. 380 | return str.substr(0, str.length - 1); 381 | } 382 | // Also in signalr-protocol-msgpack/Utils.ts 383 | /** @private */ 384 | function isArrayBuffer(val) { 385 | return val && typeof ArrayBuffer !== "undefined" && 386 | (val instanceof ArrayBuffer || 387 | // Sometimes we get an ArrayBuffer that doesn't satisfy instanceof 388 | (val.constructor && val.constructor.name === "ArrayBuffer")); 389 | } 390 | /** @private */ 391 | async function sendMessage(logger, transportName, httpClient, url, content, options) { 392 | const headers = {}; 393 | const [name, value] = getUserAgentHeader(); 394 | headers[name] = value; 395 | logger.log(LogLevel.Trace, `(${transportName} transport) sending data. ${getDataDetail(content, options.logMessageContent)}.`); 396 | const responseType = isArrayBuffer(content) ? "arraybuffer" : "text"; 397 | const response = await httpClient.post(url, { 398 | content, 399 | headers: { ...headers, ...options.headers }, 400 | responseType, 401 | timeout: options.timeout, 402 | withCredentials: options.withCredentials, 403 | }); 404 | logger.log(LogLevel.Trace, `(${transportName} transport) request complete. Response status: ${response.statusCode}.`); 405 | } 406 | /** @private */ 407 | function createLogger(logger) { 408 | if (logger === undefined) { 409 | return new ConsoleLogger(LogLevel.Information); 410 | } 411 | if (logger === null) { 412 | return NullLogger.instance; 413 | } 414 | if (logger.log !== undefined) { 415 | return logger; 416 | } 417 | return new ConsoleLogger(logger); 418 | } 419 | /** @private */ 420 | class SubjectSubscription { 421 | constructor(subject, observer) { 422 | this._subject = subject; 423 | this._observer = observer; 424 | } 425 | dispose() { 426 | const index = this._subject.observers.indexOf(this._observer); 427 | if (index > -1) { 428 | this._subject.observers.splice(index, 1); 429 | } 430 | if (this._subject.observers.length === 0 && this._subject.cancelCallback) { 431 | this._subject.cancelCallback().catch((_) => { }); 432 | } 433 | } 434 | } 435 | /** @private */ 436 | class ConsoleLogger { 437 | constructor(minimumLogLevel) { 438 | this._minLevel = minimumLogLevel; 439 | this.out = console; 440 | } 441 | log(logLevel, message) { 442 | if (logLevel >= this._minLevel) { 443 | const msg = `[${new Date().toISOString()}] ${LogLevel[logLevel]}: ${message}`; 444 | switch (logLevel) { 445 | case LogLevel.Critical: 446 | case LogLevel.Error: 447 | this.out.error(msg); 448 | break; 449 | case LogLevel.Warning: 450 | this.out.warn(msg); 451 | break; 452 | case LogLevel.Information: 453 | this.out.info(msg); 454 | break; 455 | default: 456 | // console.debug only goes to attached debuggers in Node, so we use console.log for Trace and Debug 457 | this.out.log(msg); 458 | break; 459 | } 460 | } 461 | } 462 | } 463 | /** @private */ 464 | function getUserAgentHeader() { 465 | let userAgentHeaderName = "X-SignalR-User-Agent"; 466 | if (Platform.isNode) { 467 | userAgentHeaderName = "User-Agent"; 468 | } 469 | return [userAgentHeaderName, constructUserAgent(VERSION, getOsName(), getRuntime(), getRuntimeVersion())]; 470 | } 471 | /** @private */ 472 | function constructUserAgent(version, os, runtime, runtimeVersion) { 473 | // Microsoft SignalR/[Version] ([Detailed Version]; [Operating System]; [Runtime]; [Runtime Version]) 474 | let userAgent = "Microsoft SignalR/"; 475 | const majorAndMinor = version.split("."); 476 | userAgent += `${majorAndMinor[0]}.${majorAndMinor[1]}`; 477 | userAgent += ` (${version}; `; 478 | if (os && os !== "") { 479 | userAgent += `${os}; `; 480 | } 481 | else { 482 | userAgent += "Unknown OS; "; 483 | } 484 | userAgent += `${runtime}`; 485 | if (runtimeVersion) { 486 | userAgent += `; ${runtimeVersion}`; 487 | } 488 | else { 489 | userAgent += "; Unknown Runtime Version"; 490 | } 491 | userAgent += ")"; 492 | return userAgent; 493 | } 494 | // eslint-disable-next-line spaced-comment 495 | /*#__PURE__*/ function getOsName() { 496 | if (Platform.isNode) { 497 | switch (process.platform) { 498 | case "win32": 499 | return "Windows NT"; 500 | case "darwin": 501 | return "macOS"; 502 | case "linux": 503 | return "Linux"; 504 | default: 505 | return process.platform; 506 | } 507 | } 508 | else { 509 | return ""; 510 | } 511 | } 512 | // eslint-disable-next-line spaced-comment 513 | /*#__PURE__*/ function getRuntimeVersion() { 514 | if (Platform.isNode) { 515 | return process.versions.node; 516 | } 517 | return undefined; 518 | } 519 | function getRuntime() { 520 | if (Platform.isNode) { 521 | return "NodeJS"; 522 | } 523 | else { 524 | return "Browser"; 525 | } 526 | } 527 | /** @private */ 528 | function getErrorString(e) { 529 | if (e.stack) { 530 | return e.stack; 531 | } 532 | else if (e.message) { 533 | return e.message; 534 | } 535 | return `${e}`; 536 | } 537 | /** @private */ 538 | function getGlobalThis() { 539 | // globalThis is semi-new and not available in Node until v12 540 | if (typeof globalThis !== "undefined") { 541 | return globalThis; 542 | } 543 | if (typeof self !== "undefined") { 544 | return self; 545 | } 546 | if (typeof window !== "undefined") { 547 | return window; 548 | } 549 | if (typeof __webpack_require__.g !== "undefined") { 550 | return __webpack_require__.g; 551 | } 552 | throw new Error("could not find global"); 553 | } 554 | 555 | ;// CONCATENATED MODULE: ./src/FetchHttpClient.ts 556 | // Licensed to the .NET Foundation under one or more agreements. 557 | // The .NET Foundation licenses this file to you under the MIT license. 558 | 559 | 560 | 561 | 562 | class FetchHttpClient extends HttpClient { 563 | constructor(logger) { 564 | super(); 565 | this._logger = logger; 566 | if (typeof fetch === "undefined") { 567 | // In order to ignore the dynamic require in webpack builds we need to do this magic 568 | // @ts-ignore: TS doesn't know about these names 569 | const requireFunc = true ? require : 0; 570 | // Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests 571 | this._jar = new (requireFunc("tough-cookie")).CookieJar(); 572 | this._fetchType = requireFunc("node-fetch"); 573 | // node-fetch doesn't have a nice API for getting and setting cookies 574 | // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one 575 | this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar); 576 | } 577 | else { 578 | this._fetchType = fetch.bind(getGlobalThis()); 579 | } 580 | if (typeof AbortController === "undefined") { 581 | // In order to ignore the dynamic require in webpack builds we need to do this magic 582 | // @ts-ignore: TS doesn't know about these names 583 | const requireFunc = true ? require : 0; 584 | // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide 585 | this._abortControllerType = requireFunc("abort-controller"); 586 | } 587 | else { 588 | this._abortControllerType = AbortController; 589 | } 590 | } 591 | /** @inheritDoc */ 592 | async send(request) { 593 | // Check that abort was not signaled before calling send 594 | if (request.abortSignal && request.abortSignal.aborted) { 595 | throw new AbortError(); 596 | } 597 | if (!request.method) { 598 | throw new Error("No method defined."); 599 | } 600 | if (!request.url) { 601 | throw new Error("No url defined."); 602 | } 603 | const abortController = new this._abortControllerType(); 604 | let error; 605 | // Hook our abortSignal into the abort controller 606 | if (request.abortSignal) { 607 | request.abortSignal.onabort = () => { 608 | abortController.abort(); 609 | error = new AbortError(); 610 | }; 611 | } 612 | // If a timeout has been passed in, setup a timeout to call abort 613 | // Type needs to be any to fit window.setTimeout and NodeJS.setTimeout 614 | let timeoutId = null; 615 | if (request.timeout) { 616 | const msTimeout = request.timeout; 617 | timeoutId = setTimeout(() => { 618 | abortController.abort(); 619 | this._logger.log(LogLevel.Warning, `Timeout from HTTP request.`); 620 | error = new TimeoutError(); 621 | }, msTimeout); 622 | } 623 | if (request.content === "") { 624 | request.content = undefined; 625 | } 626 | if (request.content) { 627 | // Explicitly setting the Content-Type header for React Native on Android platform. 628 | request.headers = request.headers || {}; 629 | if (isArrayBuffer(request.content)) { 630 | request.headers["Content-Type"] = "application/octet-stream"; 631 | } 632 | else { 633 | request.headers["Content-Type"] = "text/plain;charset=UTF-8"; 634 | } 635 | } 636 | let response; 637 | try { 638 | response = await this._fetchType(request.url, { 639 | body: request.content, 640 | cache: "no-cache", 641 | credentials: request.withCredentials === true ? "include" : "same-origin", 642 | headers: { 643 | "X-Requested-With": "XMLHttpRequest", 644 | ...request.headers, 645 | }, 646 | method: request.method, 647 | mode: "cors", 648 | redirect: "follow", 649 | signal: abortController.signal, 650 | }); 651 | } 652 | catch (e) { 653 | if (error) { 654 | throw error; 655 | } 656 | this._logger.log(LogLevel.Warning, `Error from HTTP request. ${e}.`); 657 | throw e; 658 | } 659 | finally { 660 | if (timeoutId) { 661 | clearTimeout(timeoutId); 662 | } 663 | if (request.abortSignal) { 664 | request.abortSignal.onabort = null; 665 | } 666 | } 667 | if (!response.ok) { 668 | const errorMessage = await deserializeContent(response, "text"); 669 | throw new HttpError(errorMessage || response.statusText, response.status); 670 | } 671 | const content = deserializeContent(response, request.responseType); 672 | const payload = await content; 673 | return new HttpResponse(response.status, response.statusText, payload); 674 | } 675 | getCookieString(url) { 676 | let cookies = ""; 677 | if (Platform.isNode && this._jar) { 678 | // @ts-ignore: unused variable 679 | this._jar.getCookies(url, (e, c) => cookies = c.join("; ")); 680 | } 681 | return cookies; 682 | } 683 | } 684 | function deserializeContent(response, responseType) { 685 | let content; 686 | switch (responseType) { 687 | case "arraybuffer": 688 | content = response.arrayBuffer(); 689 | break; 690 | case "text": 691 | content = response.text(); 692 | break; 693 | case "blob": 694 | case "document": 695 | case "json": 696 | throw new Error(`${responseType} is not supported.`); 697 | default: 698 | content = response.text(); 699 | break; 700 | } 701 | return content; 702 | } 703 | 704 | ;// CONCATENATED MODULE: ./src/XhrHttpClient.ts 705 | // Licensed to the .NET Foundation under one or more agreements. 706 | // The .NET Foundation licenses this file to you under the MIT license. 707 | 708 | 709 | 710 | 711 | class XhrHttpClient extends HttpClient { 712 | constructor(logger) { 713 | super(); 714 | this._logger = logger; 715 | } 716 | /** @inheritDoc */ 717 | send(request) { 718 | // Check that abort was not signaled before calling send 719 | if (request.abortSignal && request.abortSignal.aborted) { 720 | return Promise.reject(new AbortError()); 721 | } 722 | if (!request.method) { 723 | return Promise.reject(new Error("No method defined.")); 724 | } 725 | if (!request.url) { 726 | return Promise.reject(new Error("No url defined.")); 727 | } 728 | return new Promise((resolve, reject) => { 729 | const xhr = new XMLHttpRequest(); 730 | xhr.open(request.method, request.url, true); 731 | xhr.withCredentials = request.withCredentials === undefined ? true : request.withCredentials; 732 | xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 733 | if (request.content === "") { 734 | request.content = undefined; 735 | } 736 | if (request.content) { 737 | // Explicitly setting the Content-Type header for React Native on Android platform. 738 | if (isArrayBuffer(request.content)) { 739 | xhr.setRequestHeader("Content-Type", "application/octet-stream"); 740 | } 741 | else { 742 | xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); 743 | } 744 | } 745 | const headers = request.headers; 746 | if (headers) { 747 | Object.keys(headers) 748 | .forEach((header) => { 749 | xhr.setRequestHeader(header, headers[header]); 750 | }); 751 | } 752 | if (request.responseType) { 753 | xhr.responseType = request.responseType; 754 | } 755 | if (request.abortSignal) { 756 | request.abortSignal.onabort = () => { 757 | xhr.abort(); 758 | reject(new AbortError()); 759 | }; 760 | } 761 | if (request.timeout) { 762 | xhr.timeout = request.timeout; 763 | } 764 | xhr.onload = () => { 765 | if (request.abortSignal) { 766 | request.abortSignal.onabort = null; 767 | } 768 | if (xhr.status >= 200 && xhr.status < 300) { 769 | resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText)); 770 | } 771 | else { 772 | reject(new HttpError(xhr.response || xhr.responseText || xhr.statusText, xhr.status)); 773 | } 774 | }; 775 | xhr.onerror = () => { 776 | this._logger.log(LogLevel.Warning, `Error from HTTP request. ${xhr.status}: ${xhr.statusText}.`); 777 | reject(new HttpError(xhr.statusText, xhr.status)); 778 | }; 779 | xhr.ontimeout = () => { 780 | this._logger.log(LogLevel.Warning, `Timeout from HTTP request.`); 781 | reject(new TimeoutError()); 782 | }; 783 | xhr.send(request.content); 784 | }); 785 | } 786 | } 787 | 788 | ;// CONCATENATED MODULE: ./src/DefaultHttpClient.ts 789 | // Licensed to the .NET Foundation under one or more agreements. 790 | // The .NET Foundation licenses this file to you under the MIT license. 791 | 792 | 793 | 794 | 795 | 796 | /** Default implementation of {@link @microsoft/signalr.HttpClient}. */ 797 | class DefaultHttpClient extends HttpClient { 798 | /** Creates a new instance of the {@link @microsoft/signalr.DefaultHttpClient}, using the provided {@link @microsoft/signalr.ILogger} to log messages. */ 799 | constructor(logger) { 800 | super(); 801 | if (typeof fetch !== "undefined" || Platform.isNode) { 802 | this._httpClient = new FetchHttpClient(logger); 803 | } 804 | else if (typeof XMLHttpRequest !== "undefined") { 805 | this._httpClient = new XhrHttpClient(logger); 806 | } 807 | else { 808 | throw new Error("No usable HttpClient found."); 809 | } 810 | } 811 | /** @inheritDoc */ 812 | send(request) { 813 | // Check that abort was not signaled before calling send 814 | if (request.abortSignal && request.abortSignal.aborted) { 815 | return Promise.reject(new AbortError()); 816 | } 817 | if (!request.method) { 818 | return Promise.reject(new Error("No method defined.")); 819 | } 820 | if (!request.url) { 821 | return Promise.reject(new Error("No url defined.")); 822 | } 823 | return this._httpClient.send(request); 824 | } 825 | getCookieString(url) { 826 | return this._httpClient.getCookieString(url); 827 | } 828 | } 829 | 830 | ;// CONCATENATED MODULE: ./src/TextMessageFormat.ts 831 | // Licensed to the .NET Foundation under one or more agreements. 832 | // The .NET Foundation licenses this file to you under the MIT license. 833 | // Not exported from index 834 | /** @private */ 835 | class TextMessageFormat { 836 | static write(output) { 837 | return `${output}${TextMessageFormat.RecordSeparator}`; 838 | } 839 | static parse(input) { 840 | if (input[input.length - 1] !== TextMessageFormat.RecordSeparator) { 841 | throw new Error("Message is incomplete."); 842 | } 843 | const messages = input.split(TextMessageFormat.RecordSeparator); 844 | messages.pop(); 845 | return messages; 846 | } 847 | } 848 | TextMessageFormat.RecordSeparatorCode = 0x1e; 849 | TextMessageFormat.RecordSeparator = String.fromCharCode(TextMessageFormat.RecordSeparatorCode); 850 | 851 | ;// CONCATENATED MODULE: ./src/HandshakeProtocol.ts 852 | // Licensed to the .NET Foundation under one or more agreements. 853 | // The .NET Foundation licenses this file to you under the MIT license. 854 | 855 | 856 | /** @private */ 857 | class HandshakeProtocol { 858 | // Handshake request is always JSON 859 | writeHandshakeRequest(handshakeRequest) { 860 | return TextMessageFormat.write(JSON.stringify(handshakeRequest)); 861 | } 862 | parseHandshakeResponse(data) { 863 | let messageData; 864 | let remainingData; 865 | if (isArrayBuffer(data)) { 866 | // Format is binary but still need to read JSON text from handshake response 867 | const binaryData = new Uint8Array(data); 868 | const separatorIndex = binaryData.indexOf(TextMessageFormat.RecordSeparatorCode); 869 | if (separatorIndex === -1) { 870 | throw new Error("Message is incomplete."); 871 | } 872 | // content before separator is handshake response 873 | // optional content after is additional messages 874 | const responseLength = separatorIndex + 1; 875 | messageData = String.fromCharCode.apply(null, Array.prototype.slice.call(binaryData.slice(0, responseLength))); 876 | remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null; 877 | } 878 | else { 879 | const textData = data; 880 | const separatorIndex = textData.indexOf(TextMessageFormat.RecordSeparator); 881 | if (separatorIndex === -1) { 882 | throw new Error("Message is incomplete."); 883 | } 884 | // content before separator is handshake response 885 | // optional content after is additional messages 886 | const responseLength = separatorIndex + 1; 887 | messageData = textData.substring(0, responseLength); 888 | remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null; 889 | } 890 | // At this point we should have just the single handshake message 891 | const messages = TextMessageFormat.parse(messageData); 892 | const response = JSON.parse(messages[0]); 893 | if (response.type) { 894 | throw new Error("Expected a handshake response from the server."); 895 | } 896 | const responseMessage = response; 897 | // multiple messages could have arrived with handshake 898 | // return additional data to be parsed as usual, or null if all parsed 899 | return [remainingData, responseMessage]; 900 | } 901 | } 902 | 903 | ;// CONCATENATED MODULE: ./src/IHubProtocol.ts 904 | // Licensed to the .NET Foundation under one or more agreements. 905 | // The .NET Foundation licenses this file to you under the MIT license. 906 | /** Defines the type of a Hub Message. */ 907 | var MessageType; 908 | (function (MessageType) { 909 | /** Indicates the message is an Invocation message and implements the {@link @microsoft/signalr.InvocationMessage} interface. */ 910 | MessageType[MessageType["Invocation"] = 1] = "Invocation"; 911 | /** Indicates the message is a StreamItem message and implements the {@link @microsoft/signalr.StreamItemMessage} interface. */ 912 | MessageType[MessageType["StreamItem"] = 2] = "StreamItem"; 913 | /** Indicates the message is a Completion message and implements the {@link @microsoft/signalr.CompletionMessage} interface. */ 914 | MessageType[MessageType["Completion"] = 3] = "Completion"; 915 | /** Indicates the message is a Stream Invocation message and implements the {@link @microsoft/signalr.StreamInvocationMessage} interface. */ 916 | MessageType[MessageType["StreamInvocation"] = 4] = "StreamInvocation"; 917 | /** Indicates the message is a Cancel Invocation message and implements the {@link @microsoft/signalr.CancelInvocationMessage} interface. */ 918 | MessageType[MessageType["CancelInvocation"] = 5] = "CancelInvocation"; 919 | /** Indicates the message is a Ping message and implements the {@link @microsoft/signalr.PingMessage} interface. */ 920 | MessageType[MessageType["Ping"] = 6] = "Ping"; 921 | /** Indicates the message is a Close message and implements the {@link @microsoft/signalr.CloseMessage} interface. */ 922 | MessageType[MessageType["Close"] = 7] = "Close"; 923 | })(MessageType || (MessageType = {})); 924 | 925 | ;// CONCATENATED MODULE: ./src/Subject.ts 926 | // Licensed to the .NET Foundation under one or more agreements. 927 | // The .NET Foundation licenses this file to you under the MIT license. 928 | 929 | /** Stream implementation to stream items to the server. */ 930 | class Subject { 931 | constructor() { 932 | this.observers = []; 933 | } 934 | next(item) { 935 | for (const observer of this.observers) { 936 | observer.next(item); 937 | } 938 | } 939 | error(err) { 940 | for (const observer of this.observers) { 941 | if (observer.error) { 942 | observer.error(err); 943 | } 944 | } 945 | } 946 | complete() { 947 | for (const observer of this.observers) { 948 | if (observer.complete) { 949 | observer.complete(); 950 | } 951 | } 952 | } 953 | subscribe(observer) { 954 | this.observers.push(observer); 955 | return new SubjectSubscription(this, observer); 956 | } 957 | } 958 | 959 | ;// CONCATENATED MODULE: ./src/HubConnection.ts 960 | // Licensed to the .NET Foundation under one or more agreements. 961 | // The .NET Foundation licenses this file to you under the MIT license. 962 | 963 | 964 | 965 | 966 | 967 | 968 | const DEFAULT_TIMEOUT_IN_MS = 30 * 1000; 969 | const DEFAULT_PING_INTERVAL_IN_MS = 15 * 1000; 970 | /** Describes the current state of the {@link HubConnection} to the server. */ 971 | var HubConnectionState; 972 | (function (HubConnectionState) { 973 | /** The hub connection is disconnected. */ 974 | HubConnectionState["Disconnected"] = "Disconnected"; 975 | /** The hub connection is connecting. */ 976 | HubConnectionState["Connecting"] = "Connecting"; 977 | /** The hub connection is connected. */ 978 | HubConnectionState["Connected"] = "Connected"; 979 | /** The hub connection is disconnecting. */ 980 | HubConnectionState["Disconnecting"] = "Disconnecting"; 981 | /** The hub connection is reconnecting. */ 982 | HubConnectionState["Reconnecting"] = "Reconnecting"; 983 | })(HubConnectionState || (HubConnectionState = {})); 984 | /** Represents a connection to a SignalR Hub. */ 985 | class HubConnection { 986 | constructor(connection, logger, protocol, reconnectPolicy) { 987 | this._nextKeepAlive = 0; 988 | this._freezeEventListener = () => { 989 | this._logger.log(LogLevel.Warning, "The page is being frozen, this will likely lead to the connection being closed and messages being lost. For more information see the docs at https://docs.microsoft.com/aspnet/core/signalr/javascript-client#bsleep"); 990 | }; 991 | Arg.isRequired(connection, "connection"); 992 | Arg.isRequired(logger, "logger"); 993 | Arg.isRequired(protocol, "protocol"); 994 | this.serverTimeoutInMilliseconds = DEFAULT_TIMEOUT_IN_MS; 995 | this.keepAliveIntervalInMilliseconds = DEFAULT_PING_INTERVAL_IN_MS; 996 | this._logger = logger; 997 | this._protocol = protocol; 998 | this.connection = connection; 999 | this._reconnectPolicy = reconnectPolicy; 1000 | this._handshakeProtocol = new HandshakeProtocol(); 1001 | this.connection.onreceive = (data) => this._processIncomingData(data); 1002 | this.connection.onclose = (error) => this._connectionClosed(error); 1003 | this._callbacks = {}; 1004 | this._methods = {}; 1005 | this._closedCallbacks = []; 1006 | this._reconnectingCallbacks = []; 1007 | this._reconnectedCallbacks = []; 1008 | this._invocationId = 0; 1009 | this._receivedHandshakeResponse = false; 1010 | this._connectionState = HubConnectionState.Disconnected; 1011 | this._connectionStarted = false; 1012 | this._cachedPingMessage = this._protocol.writeMessage({ type: MessageType.Ping }); 1013 | } 1014 | /** @internal */ 1015 | // Using a public static factory method means we can have a private constructor and an _internal_ 1016 | // create method that can be used by HubConnectionBuilder. An "internal" constructor would just 1017 | // be stripped away and the '.d.ts' file would have no constructor, which is interpreted as a 1018 | // public parameter-less constructor. 1019 | static create(connection, logger, protocol, reconnectPolicy) { 1020 | return new HubConnection(connection, logger, protocol, reconnectPolicy); 1021 | } 1022 | /** Indicates the state of the {@link HubConnection} to the server. */ 1023 | get state() { 1024 | return this._connectionState; 1025 | } 1026 | /** Represents the connection id of the {@link HubConnection} on the server. The connection id will be null when the connection is either 1027 | * in the disconnected state or if the negotiation step was skipped. 1028 | */ 1029 | get connectionId() { 1030 | return this.connection ? (this.connection.connectionId || null) : null; 1031 | } 1032 | /** Indicates the url of the {@link HubConnection} to the server. */ 1033 | get baseUrl() { 1034 | return this.connection.baseUrl || ""; 1035 | } 1036 | /** 1037 | * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or 1038 | * Reconnecting states. 1039 | * @param {string} url The url to connect to. 1040 | */ 1041 | set baseUrl(url) { 1042 | if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) { 1043 | throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url."); 1044 | } 1045 | if (!url) { 1046 | throw new Error("The HubConnection url must be a valid url."); 1047 | } 1048 | this.connection.baseUrl = url; 1049 | } 1050 | /** Starts the connection. 1051 | * 1052 | * @returns {Promise} A Promise that resolves when the connection has been successfully established, or rejects with an error. 1053 | */ 1054 | start() { 1055 | this._startPromise = this._startWithStateTransitions(); 1056 | return this._startPromise; 1057 | } 1058 | async _startWithStateTransitions() { 1059 | if (this._connectionState !== HubConnectionState.Disconnected) { 1060 | return Promise.reject(new Error("Cannot start a HubConnection that is not in the 'Disconnected' state.")); 1061 | } 1062 | this._connectionState = HubConnectionState.Connecting; 1063 | this._logger.log(LogLevel.Debug, "Starting HubConnection."); 1064 | try { 1065 | await this._startInternal(); 1066 | if (Platform.isBrowser) { 1067 | // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working 1068 | window.document.addEventListener("freeze", this._freezeEventListener); 1069 | } 1070 | this._connectionState = HubConnectionState.Connected; 1071 | this._connectionStarted = true; 1072 | this._logger.log(LogLevel.Debug, "HubConnection connected successfully."); 1073 | } 1074 | catch (e) { 1075 | this._connectionState = HubConnectionState.Disconnected; 1076 | this._logger.log(LogLevel.Debug, `HubConnection failed to start successfully because of error '${e}'.`); 1077 | return Promise.reject(e); 1078 | } 1079 | } 1080 | async _startInternal() { 1081 | this._stopDuringStartError = undefined; 1082 | this._receivedHandshakeResponse = false; 1083 | // Set up the promise before any connection is (re)started otherwise it could race with received messages 1084 | const handshakePromise = new Promise((resolve, reject) => { 1085 | this._handshakeResolver = resolve; 1086 | this._handshakeRejecter = reject; 1087 | }); 1088 | await this.connection.start(this._protocol.transferFormat); 1089 | try { 1090 | const handshakeRequest = { 1091 | protocol: this._protocol.name, 1092 | version: this._protocol.version, 1093 | }; 1094 | this._logger.log(LogLevel.Debug, "Sending handshake request."); 1095 | await this._sendMessage(this._handshakeProtocol.writeHandshakeRequest(handshakeRequest)); 1096 | this._logger.log(LogLevel.Information, `Using HubProtocol '${this._protocol.name}'.`); 1097 | // defensively cleanup timeout in case we receive a message from the server before we finish start 1098 | this._cleanupTimeout(); 1099 | this._resetTimeoutPeriod(); 1100 | this._resetKeepAliveInterval(); 1101 | await handshakePromise; 1102 | // It's important to check the stopDuringStartError instead of just relying on the handshakePromise 1103 | // being rejected on close, because this continuation can run after both the handshake completed successfully 1104 | // and the connection was closed. 1105 | if (this._stopDuringStartError) { 1106 | // It's important to throw instead of returning a rejected promise, because we don't want to allow any state 1107 | // transitions to occur between now and the calling code observing the exceptions. Returning a rejected promise 1108 | // will cause the calling continuation to get scheduled to run later. 1109 | // eslint-disable-next-line @typescript-eslint/no-throw-literal 1110 | throw this._stopDuringStartError; 1111 | } 1112 | if (!this.connection.features.inherentKeepAlive) { 1113 | await this._sendMessage(this._cachedPingMessage); 1114 | } 1115 | } 1116 | catch (e) { 1117 | this._logger.log(LogLevel.Debug, `Hub handshake failed with error '${e}' during start(). Stopping HubConnection.`); 1118 | this._cleanupTimeout(); 1119 | this._cleanupPingTimer(); 1120 | // HttpConnection.stop() should not complete until after the onclose callback is invoked. 1121 | // This will transition the HubConnection to the disconnected state before HttpConnection.stop() completes. 1122 | await this.connection.stop(e); 1123 | throw e; 1124 | } 1125 | } 1126 | /** Stops the connection. 1127 | * 1128 | * @returns {Promise} A Promise that resolves when the connection has been successfully terminated, or rejects with an error. 1129 | */ 1130 | async stop() { 1131 | // Capture the start promise before the connection might be restarted in an onclose callback. 1132 | const startPromise = this._startPromise; 1133 | this._stopPromise = this._stopInternal(); 1134 | await this._stopPromise; 1135 | try { 1136 | // Awaiting undefined continues immediately 1137 | await startPromise; 1138 | } 1139 | catch (e) { 1140 | // This exception is returned to the user as a rejected Promise from the start method. 1141 | } 1142 | } 1143 | _stopInternal(error) { 1144 | if (this._connectionState === HubConnectionState.Disconnected) { 1145 | this._logger.log(LogLevel.Debug, `Call to HubConnection.stop(${error}) ignored because it is already in the disconnected state.`); 1146 | return Promise.resolve(); 1147 | } 1148 | if (this._connectionState === HubConnectionState.Disconnecting) { 1149 | this._logger.log(LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnecting state.`); 1150 | return this._stopPromise; 1151 | } 1152 | this._connectionState = HubConnectionState.Disconnecting; 1153 | this._logger.log(LogLevel.Debug, "Stopping HubConnection."); 1154 | if (this._reconnectDelayHandle) { 1155 | // We're in a reconnect delay which means the underlying connection is currently already stopped. 1156 | // Just clear the handle to stop the reconnect loop (which no one is waiting on thankfully) and 1157 | // fire the onclose callbacks. 1158 | this._logger.log(LogLevel.Debug, "Connection stopped during reconnect delay. Done reconnecting."); 1159 | clearTimeout(this._reconnectDelayHandle); 1160 | this._reconnectDelayHandle = undefined; 1161 | this._completeClose(); 1162 | return Promise.resolve(); 1163 | } 1164 | this._cleanupTimeout(); 1165 | this._cleanupPingTimer(); 1166 | this._stopDuringStartError = error || new AbortError("The connection was stopped before the hub handshake could complete."); 1167 | // HttpConnection.stop() should not complete until after either HttpConnection.start() fails 1168 | // or the onclose callback is invoked. The onclose callback will transition the HubConnection 1169 | // to the disconnected state if need be before HttpConnection.stop() completes. 1170 | return this.connection.stop(error); 1171 | } 1172 | /** Invokes a streaming hub method on the server using the specified name and arguments. 1173 | * 1174 | * @typeparam T The type of the items returned by the server. 1175 | * @param {string} methodName The name of the server method to invoke. 1176 | * @param {any[]} args The arguments used to invoke the server method. 1177 | * @returns {IStreamResult} An object that yields results from the server as they are received. 1178 | */ 1179 | stream(methodName, ...args) { 1180 | const [streams, streamIds] = this._replaceStreamingParams(args); 1181 | const invocationDescriptor = this._createStreamInvocation(methodName, args, streamIds); 1182 | // eslint-disable-next-line prefer-const 1183 | let promiseQueue; 1184 | const subject = new Subject(); 1185 | subject.cancelCallback = () => { 1186 | const cancelInvocation = this._createCancelInvocation(invocationDescriptor.invocationId); 1187 | delete this._callbacks[invocationDescriptor.invocationId]; 1188 | return promiseQueue.then(() => { 1189 | return this._sendWithProtocol(cancelInvocation); 1190 | }); 1191 | }; 1192 | this._callbacks[invocationDescriptor.invocationId] = (invocationEvent, error) => { 1193 | if (error) { 1194 | subject.error(error); 1195 | return; 1196 | } 1197 | else if (invocationEvent) { 1198 | // invocationEvent will not be null when an error is not passed to the callback 1199 | if (invocationEvent.type === MessageType.Completion) { 1200 | if (invocationEvent.error) { 1201 | subject.error(new Error(invocationEvent.error)); 1202 | } 1203 | else { 1204 | subject.complete(); 1205 | } 1206 | } 1207 | else { 1208 | subject.next((invocationEvent.item)); 1209 | } 1210 | } 1211 | }; 1212 | promiseQueue = this._sendWithProtocol(invocationDescriptor) 1213 | .catch((e) => { 1214 | subject.error(e); 1215 | delete this._callbacks[invocationDescriptor.invocationId]; 1216 | }); 1217 | this._launchStreams(streams, promiseQueue); 1218 | return subject; 1219 | } 1220 | _sendMessage(message) { 1221 | this._resetKeepAliveInterval(); 1222 | return this.connection.send(message); 1223 | } 1224 | /** 1225 | * Sends a js object to the server. 1226 | * @param message The js object to serialize and send. 1227 | */ 1228 | _sendWithProtocol(message) { 1229 | return this._sendMessage(this._protocol.writeMessage(message)); 1230 | } 1231 | /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. 1232 | * 1233 | * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still 1234 | * be processing the invocation. 1235 | * 1236 | * @param {string} methodName The name of the server method to invoke. 1237 | * @param {any[]} args The arguments used to invoke the server method. 1238 | * @returns {Promise} A Promise that resolves when the invocation has been successfully sent, or rejects with an error. 1239 | */ 1240 | send(methodName, ...args) { 1241 | const [streams, streamIds] = this._replaceStreamingParams(args); 1242 | const sendPromise = this._sendWithProtocol(this._createInvocation(methodName, args, true, streamIds)); 1243 | this._launchStreams(streams, sendPromise); 1244 | return sendPromise; 1245 | } 1246 | /** Invokes a hub method on the server using the specified name and arguments. 1247 | * 1248 | * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise 1249 | * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of 1250 | * resolving the Promise. 1251 | * 1252 | * @typeparam T The expected return type. 1253 | * @param {string} methodName The name of the server method to invoke. 1254 | * @param {any[]} args The arguments used to invoke the server method. 1255 | * @returns {Promise} A Promise that resolves with the result of the server method (if any), or rejects with an error. 1256 | */ 1257 | invoke(methodName, ...args) { 1258 | const [streams, streamIds] = this._replaceStreamingParams(args); 1259 | const invocationDescriptor = this._createInvocation(methodName, args, false, streamIds); 1260 | const p = new Promise((resolve, reject) => { 1261 | // invocationId will always have a value for a non-blocking invocation 1262 | this._callbacks[invocationDescriptor.invocationId] = (invocationEvent, error) => { 1263 | if (error) { 1264 | reject(error); 1265 | return; 1266 | } 1267 | else if (invocationEvent) { 1268 | // invocationEvent will not be null when an error is not passed to the callback 1269 | if (invocationEvent.type === MessageType.Completion) { 1270 | if (invocationEvent.error) { 1271 | reject(new Error(invocationEvent.error)); 1272 | } 1273 | else { 1274 | resolve(invocationEvent.result); 1275 | } 1276 | } 1277 | else { 1278 | reject(new Error(`Unexpected message type: ${invocationEvent.type}`)); 1279 | } 1280 | } 1281 | }; 1282 | const promiseQueue = this._sendWithProtocol(invocationDescriptor) 1283 | .catch((e) => { 1284 | reject(e); 1285 | // invocationId will always have a value for a non-blocking invocation 1286 | delete this._callbacks[invocationDescriptor.invocationId]; 1287 | }); 1288 | this._launchStreams(streams, promiseQueue); 1289 | }); 1290 | return p; 1291 | } 1292 | on(methodName, newMethod) { 1293 | if (!methodName || !newMethod) { 1294 | return; 1295 | } 1296 | methodName = methodName.toLowerCase(); 1297 | if (!this._methods[methodName]) { 1298 | this._methods[methodName] = []; 1299 | } 1300 | // Preventing adding the same handler multiple times. 1301 | if (this._methods[methodName].indexOf(newMethod) !== -1) { 1302 | return; 1303 | } 1304 | this._methods[methodName].push(newMethod); 1305 | } 1306 | off(methodName, method) { 1307 | if (!methodName) { 1308 | return; 1309 | } 1310 | methodName = methodName.toLowerCase(); 1311 | const handlers = this._methods[methodName]; 1312 | if (!handlers) { 1313 | return; 1314 | } 1315 | if (method) { 1316 | const removeIdx = handlers.indexOf(method); 1317 | if (removeIdx !== -1) { 1318 | handlers.splice(removeIdx, 1); 1319 | if (handlers.length === 0) { 1320 | delete this._methods[methodName]; 1321 | } 1322 | } 1323 | } 1324 | else { 1325 | delete this._methods[methodName]; 1326 | } 1327 | } 1328 | /** Registers a handler that will be invoked when the connection is closed. 1329 | * 1330 | * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any). 1331 | */ 1332 | onclose(callback) { 1333 | if (callback) { 1334 | this._closedCallbacks.push(callback); 1335 | } 1336 | } 1337 | /** Registers a handler that will be invoked when the connection starts reconnecting. 1338 | * 1339 | * @param {Function} callback The handler that will be invoked when the connection starts reconnecting. Optionally receives a single argument containing the error that caused the connection to start reconnecting (if any). 1340 | */ 1341 | onreconnecting(callback) { 1342 | if (callback) { 1343 | this._reconnectingCallbacks.push(callback); 1344 | } 1345 | } 1346 | /** Registers a handler that will be invoked when the connection successfully reconnects. 1347 | * 1348 | * @param {Function} callback The handler that will be invoked when the connection successfully reconnects. 1349 | */ 1350 | onreconnected(callback) { 1351 | if (callback) { 1352 | this._reconnectedCallbacks.push(callback); 1353 | } 1354 | } 1355 | _processIncomingData(data) { 1356 | this._cleanupTimeout(); 1357 | if (!this._receivedHandshakeResponse) { 1358 | data = this._processHandshakeResponse(data); 1359 | this._receivedHandshakeResponse = true; 1360 | } 1361 | // Data may have all been read when processing handshake response 1362 | if (data) { 1363 | // Parse the messages 1364 | const messages = this._protocol.parseMessages(data, this._logger); 1365 | for (const message of messages) { 1366 | switch (message.type) { 1367 | case MessageType.Invocation: 1368 | // eslint-disable-next-line @typescript-eslint/no-floating-promises 1369 | this._invokeClientMethod(message); 1370 | break; 1371 | case MessageType.StreamItem: 1372 | case MessageType.Completion: { 1373 | const callback = this._callbacks[message.invocationId]; 1374 | if (callback) { 1375 | if (message.type === MessageType.Completion) { 1376 | delete this._callbacks[message.invocationId]; 1377 | } 1378 | try { 1379 | callback(message); 1380 | } 1381 | catch (e) { 1382 | this._logger.log(LogLevel.Error, `Stream callback threw error: ${getErrorString(e)}`); 1383 | } 1384 | } 1385 | break; 1386 | } 1387 | case MessageType.Ping: 1388 | // Don't care about pings 1389 | break; 1390 | case MessageType.Close: { 1391 | this._logger.log(LogLevel.Information, "Close message received from server."); 1392 | const error = message.error ? new Error("Server returned an error on close: " + message.error) : undefined; 1393 | if (message.allowReconnect === true) { 1394 | // It feels wrong not to await connection.stop() here, but processIncomingData is called as part of an onreceive callback which is not async, 1395 | // this is already the behavior for serverTimeout(), and HttpConnection.Stop() should catch and log all possible exceptions. 1396 | // eslint-disable-next-line @typescript-eslint/no-floating-promises 1397 | this.connection.stop(error); 1398 | } 1399 | else { 1400 | // We cannot await stopInternal() here, but subsequent calls to stop() will await this if stopInternal() is still ongoing. 1401 | this._stopPromise = this._stopInternal(error); 1402 | } 1403 | break; 1404 | } 1405 | default: 1406 | this._logger.log(LogLevel.Warning, `Invalid message type: ${message.type}.`); 1407 | break; 1408 | } 1409 | } 1410 | } 1411 | this._resetTimeoutPeriod(); 1412 | } 1413 | _processHandshakeResponse(data) { 1414 | let responseMessage; 1415 | let remainingData; 1416 | try { 1417 | [remainingData, responseMessage] = this._handshakeProtocol.parseHandshakeResponse(data); 1418 | } 1419 | catch (e) { 1420 | const message = "Error parsing handshake response: " + e; 1421 | this._logger.log(LogLevel.Error, message); 1422 | const error = new Error(message); 1423 | this._handshakeRejecter(error); 1424 | throw error; 1425 | } 1426 | if (responseMessage.error) { 1427 | const message = "Server returned handshake error: " + responseMessage.error; 1428 | this._logger.log(LogLevel.Error, message); 1429 | const error = new Error(message); 1430 | this._handshakeRejecter(error); 1431 | throw error; 1432 | } 1433 | else { 1434 | this._logger.log(LogLevel.Debug, "Server handshake complete."); 1435 | } 1436 | this._handshakeResolver(); 1437 | return remainingData; 1438 | } 1439 | _resetKeepAliveInterval() { 1440 | if (this.connection.features.inherentKeepAlive) { 1441 | return; 1442 | } 1443 | // Set the time we want the next keep alive to be sent 1444 | // Timer will be setup on next message receive 1445 | this._nextKeepAlive = new Date().getTime() + this.keepAliveIntervalInMilliseconds; 1446 | this._cleanupPingTimer(); 1447 | } 1448 | _resetTimeoutPeriod() { 1449 | if (!this.connection.features || !this.connection.features.inherentKeepAlive) { 1450 | // Set the timeout timer 1451 | this._timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds); 1452 | // Set keepAlive timer if there isn't one 1453 | if (this._pingServerHandle === undefined) { 1454 | let nextPing = this._nextKeepAlive - new Date().getTime(); 1455 | if (nextPing < 0) { 1456 | nextPing = 0; 1457 | } 1458 | // The timer needs to be set from a networking callback to avoid Chrome timer throttling from causing timers to run once a minute 1459 | this._pingServerHandle = setTimeout(async () => { 1460 | if (this._connectionState === HubConnectionState.Connected) { 1461 | try { 1462 | await this._sendMessage(this._cachedPingMessage); 1463 | } 1464 | catch { 1465 | // We don't care about the error. It should be seen elsewhere in the client. 1466 | // The connection is probably in a bad or closed state now, cleanup the timer so it stops triggering 1467 | this._cleanupPingTimer(); 1468 | } 1469 | } 1470 | }, nextPing); 1471 | } 1472 | } 1473 | } 1474 | // eslint-disable-next-line @typescript-eslint/naming-convention 1475 | serverTimeout() { 1476 | // The server hasn't talked to us in a while. It doesn't like us anymore ... :( 1477 | // Terminate the connection, but we don't need to wait on the promise. This could trigger reconnecting. 1478 | // eslint-disable-next-line @typescript-eslint/no-floating-promises 1479 | this.connection.stop(new Error("Server timeout elapsed without receiving a message from the server.")); 1480 | } 1481 | async _invokeClientMethod(invocationMessage) { 1482 | const methodName = invocationMessage.target.toLowerCase(); 1483 | const methods = this._methods[methodName]; 1484 | if (!methods) { 1485 | this._logger.log(LogLevel.Warning, `No client method with the name '${methodName}' found.`); 1486 | // No handlers provided by client but the server is expecting a response still, so we send an error 1487 | if (invocationMessage.invocationId) { 1488 | this._logger.log(LogLevel.Warning, `No result given for '${methodName}' method and invocation ID '${invocationMessage.invocationId}'.`); 1489 | await this._sendWithProtocol(this._createCompletionMessage(invocationMessage.invocationId, "Client didn't provide a result.", null)); 1490 | } 1491 | return; 1492 | } 1493 | // Avoid issues with handlers removing themselves thus modifying the list while iterating through it 1494 | const methodsCopy = methods.slice(); 1495 | // Server expects a response 1496 | const expectsResponse = invocationMessage.invocationId ? true : false; 1497 | // We preserve the last result or exception but still call all handlers 1498 | let res; 1499 | let exception; 1500 | let completionMessage; 1501 | for (const m of methodsCopy) { 1502 | try { 1503 | const prevRes = res; 1504 | res = await m.apply(this, invocationMessage.arguments); 1505 | if (expectsResponse && res && prevRes) { 1506 | this._logger.log(LogLevel.Error, `Multiple results provided for '${methodName}'. Sending error to server.`); 1507 | completionMessage = this._createCompletionMessage(invocationMessage.invocationId, `Client provided multiple results.`, null); 1508 | } 1509 | // Ignore exception if we got a result after, the exception will be logged 1510 | exception = undefined; 1511 | } 1512 | catch (e) { 1513 | exception = e; 1514 | this._logger.log(LogLevel.Error, `A callback for the method '${methodName}' threw error '${e}'.`); 1515 | } 1516 | } 1517 | if (completionMessage) { 1518 | await this._sendWithProtocol(completionMessage); 1519 | } 1520 | else if (expectsResponse) { 1521 | // If there is an exception that means either no result was given or a handler after a result threw 1522 | if (exception) { 1523 | completionMessage = this._createCompletionMessage(invocationMessage.invocationId, `${exception}`, null); 1524 | } 1525 | else if (res !== undefined) { 1526 | completionMessage = this._createCompletionMessage(invocationMessage.invocationId, null, res); 1527 | } 1528 | else { 1529 | this._logger.log(LogLevel.Warning, `No result given for '${methodName}' method and invocation ID '${invocationMessage.invocationId}'.`); 1530 | // Client didn't provide a result or throw from a handler, server expects a response so we send an error 1531 | completionMessage = this._createCompletionMessage(invocationMessage.invocationId, "Client didn't provide a result.", null); 1532 | } 1533 | await this._sendWithProtocol(completionMessage); 1534 | } 1535 | else { 1536 | if (res) { 1537 | this._logger.log(LogLevel.Error, `Result given for '${methodName}' method but server is not expecting a result.`); 1538 | } 1539 | } 1540 | } 1541 | _connectionClosed(error) { 1542 | this._logger.log(LogLevel.Debug, `HubConnection.connectionClosed(${error}) called while in state ${this._connectionState}.`); 1543 | // Triggering this.handshakeRejecter is insufficient because it could already be resolved without the continuation having run yet. 1544 | this._stopDuringStartError = this._stopDuringStartError || error || new AbortError("The underlying connection was closed before the hub handshake could complete."); 1545 | // If the handshake is in progress, start will be waiting for the handshake promise, so we complete it. 1546 | // If it has already completed, this should just noop. 1547 | if (this._handshakeResolver) { 1548 | this._handshakeResolver(); 1549 | } 1550 | this._cancelCallbacksWithError(error || new Error("Invocation canceled due to the underlying connection being closed.")); 1551 | this._cleanupTimeout(); 1552 | this._cleanupPingTimer(); 1553 | if (this._connectionState === HubConnectionState.Disconnecting) { 1554 | this._completeClose(error); 1555 | } 1556 | else if (this._connectionState === HubConnectionState.Connected && this._reconnectPolicy) { 1557 | // eslint-disable-next-line @typescript-eslint/no-floating-promises 1558 | this._reconnect(error); 1559 | } 1560 | else if (this._connectionState === HubConnectionState.Connected) { 1561 | this._completeClose(error); 1562 | } 1563 | // If none of the above if conditions were true were called the HubConnection must be in either: 1564 | // 1. The Connecting state in which case the handshakeResolver will complete it and stopDuringStartError will fail it. 1565 | // 2. The Reconnecting state in which case the handshakeResolver will complete it and stopDuringStartError will fail the current reconnect attempt 1566 | // and potentially continue the reconnect() loop. 1567 | // 3. The Disconnected state in which case we're already done. 1568 | } 1569 | _completeClose(error) { 1570 | if (this._connectionStarted) { 1571 | this._connectionState = HubConnectionState.Disconnected; 1572 | this._connectionStarted = false; 1573 | if (Platform.isBrowser) { 1574 | window.document.removeEventListener("freeze", this._freezeEventListener); 1575 | } 1576 | try { 1577 | this._closedCallbacks.forEach((c) => c.apply(this, [error])); 1578 | } 1579 | catch (e) { 1580 | this._logger.log(LogLevel.Error, `An onclose callback called with error '${error}' threw error '${e}'.`); 1581 | } 1582 | } 1583 | } 1584 | async _reconnect(error) { 1585 | const reconnectStartTime = Date.now(); 1586 | let previousReconnectAttempts = 0; 1587 | let retryError = error !== undefined ? error : new Error("Attempting to reconnect due to a unknown error."); 1588 | let nextRetryDelay = this._getNextRetryDelay(previousReconnectAttempts++, 0, retryError); 1589 | if (nextRetryDelay === null) { 1590 | this._logger.log(LogLevel.Debug, "Connection not reconnecting because the IRetryPolicy returned null on the first reconnect attempt."); 1591 | this._completeClose(error); 1592 | return; 1593 | } 1594 | this._connectionState = HubConnectionState.Reconnecting; 1595 | if (error) { 1596 | this._logger.log(LogLevel.Information, `Connection reconnecting because of error '${error}'.`); 1597 | } 1598 | else { 1599 | this._logger.log(LogLevel.Information, "Connection reconnecting."); 1600 | } 1601 | if (this._reconnectingCallbacks.length !== 0) { 1602 | try { 1603 | this._reconnectingCallbacks.forEach((c) => c.apply(this, [error])); 1604 | } 1605 | catch (e) { 1606 | this._logger.log(LogLevel.Error, `An onreconnecting callback called with error '${error}' threw error '${e}'.`); 1607 | } 1608 | // Exit early if an onreconnecting callback called connection.stop(). 1609 | if (this._connectionState !== HubConnectionState.Reconnecting) { 1610 | this._logger.log(LogLevel.Debug, "Connection left the reconnecting state in onreconnecting callback. Done reconnecting."); 1611 | return; 1612 | } 1613 | } 1614 | while (nextRetryDelay !== null) { 1615 | this._logger.log(LogLevel.Information, `Reconnect attempt number ${previousReconnectAttempts} will start in ${nextRetryDelay} ms.`); 1616 | await new Promise((resolve) => { 1617 | this._reconnectDelayHandle = setTimeout(resolve, nextRetryDelay); 1618 | }); 1619 | this._reconnectDelayHandle = undefined; 1620 | if (this._connectionState !== HubConnectionState.Reconnecting) { 1621 | this._logger.log(LogLevel.Debug, "Connection left the reconnecting state during reconnect delay. Done reconnecting."); 1622 | return; 1623 | } 1624 | try { 1625 | await this._startInternal(); 1626 | this._connectionState = HubConnectionState.Connected; 1627 | this._logger.log(LogLevel.Information, "HubConnection reconnected successfully."); 1628 | if (this._reconnectedCallbacks.length !== 0) { 1629 | try { 1630 | this._reconnectedCallbacks.forEach((c) => c.apply(this, [this.connection.connectionId])); 1631 | } 1632 | catch (e) { 1633 | this._logger.log(LogLevel.Error, `An onreconnected callback called with connectionId '${this.connection.connectionId}; threw error '${e}'.`); 1634 | } 1635 | } 1636 | return; 1637 | } 1638 | catch (e) { 1639 | this._logger.log(LogLevel.Information, `Reconnect attempt failed because of error '${e}'.`); 1640 | if (this._connectionState !== HubConnectionState.Reconnecting) { 1641 | this._logger.log(LogLevel.Debug, `Connection moved to the '${this._connectionState}' from the reconnecting state during reconnect attempt. Done reconnecting.`); 1642 | // The TypeScript compiler thinks that connectionState must be Connected here. The TypeScript compiler is wrong. 1643 | if (this._connectionState === HubConnectionState.Disconnecting) { 1644 | this._completeClose(); 1645 | } 1646 | return; 1647 | } 1648 | retryError = e instanceof Error ? e : new Error(e.toString()); 1649 | nextRetryDelay = this._getNextRetryDelay(previousReconnectAttempts++, Date.now() - reconnectStartTime, retryError); 1650 | } 1651 | } 1652 | this._logger.log(LogLevel.Information, `Reconnect retries have been exhausted after ${Date.now() - reconnectStartTime} ms and ${previousReconnectAttempts} failed attempts. Connection disconnecting.`); 1653 | this._completeClose(); 1654 | } 1655 | _getNextRetryDelay(previousRetryCount, elapsedMilliseconds, retryReason) { 1656 | try { 1657 | return this._reconnectPolicy.nextRetryDelayInMilliseconds({ 1658 | elapsedMilliseconds, 1659 | previousRetryCount, 1660 | retryReason, 1661 | }); 1662 | } 1663 | catch (e) { 1664 | this._logger.log(LogLevel.Error, `IRetryPolicy.nextRetryDelayInMilliseconds(${previousRetryCount}, ${elapsedMilliseconds}) threw error '${e}'.`); 1665 | return null; 1666 | } 1667 | } 1668 | _cancelCallbacksWithError(error) { 1669 | const callbacks = this._callbacks; 1670 | this._callbacks = {}; 1671 | Object.keys(callbacks) 1672 | .forEach((key) => { 1673 | const callback = callbacks[key]; 1674 | try { 1675 | callback(null, error); 1676 | } 1677 | catch (e) { 1678 | this._logger.log(LogLevel.Error, `Stream 'error' callback called with '${error}' threw error: ${getErrorString(e)}`); 1679 | } 1680 | }); 1681 | } 1682 | _cleanupPingTimer() { 1683 | if (this._pingServerHandle) { 1684 | clearTimeout(this._pingServerHandle); 1685 | this._pingServerHandle = undefined; 1686 | } 1687 | } 1688 | _cleanupTimeout() { 1689 | if (this._timeoutHandle) { 1690 | clearTimeout(this._timeoutHandle); 1691 | } 1692 | } 1693 | _createInvocation(methodName, args, nonblocking, streamIds) { 1694 | if (nonblocking) { 1695 | if (streamIds.length !== 0) { 1696 | return { 1697 | arguments: args, 1698 | streamIds, 1699 | target: methodName, 1700 | type: MessageType.Invocation, 1701 | }; 1702 | } 1703 | else { 1704 | return { 1705 | arguments: args, 1706 | target: methodName, 1707 | type: MessageType.Invocation, 1708 | }; 1709 | } 1710 | } 1711 | else { 1712 | const invocationId = this._invocationId; 1713 | this._invocationId++; 1714 | if (streamIds.length !== 0) { 1715 | return { 1716 | arguments: args, 1717 | invocationId: invocationId.toString(), 1718 | streamIds, 1719 | target: methodName, 1720 | type: MessageType.Invocation, 1721 | }; 1722 | } 1723 | else { 1724 | return { 1725 | arguments: args, 1726 | invocationId: invocationId.toString(), 1727 | target: methodName, 1728 | type: MessageType.Invocation, 1729 | }; 1730 | } 1731 | } 1732 | } 1733 | _launchStreams(streams, promiseQueue) { 1734 | if (streams.length === 0) { 1735 | return; 1736 | } 1737 | // Synchronize stream data so they arrive in-order on the server 1738 | if (!promiseQueue) { 1739 | promiseQueue = Promise.resolve(); 1740 | } 1741 | // We want to iterate over the keys, since the keys are the stream ids 1742 | // eslint-disable-next-line guard-for-in 1743 | for (const streamId in streams) { 1744 | streams[streamId].subscribe({ 1745 | complete: () => { 1746 | promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createCompletionMessage(streamId))); 1747 | }, 1748 | error: (err) => { 1749 | let message; 1750 | if (err instanceof Error) { 1751 | message = err.message; 1752 | } 1753 | else if (err && err.toString) { 1754 | message = err.toString(); 1755 | } 1756 | else { 1757 | message = "Unknown error"; 1758 | } 1759 | promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createCompletionMessage(streamId, message))); 1760 | }, 1761 | next: (item) => { 1762 | promiseQueue = promiseQueue.then(() => this._sendWithProtocol(this._createStreamItemMessage(streamId, item))); 1763 | }, 1764 | }); 1765 | } 1766 | } 1767 | _replaceStreamingParams(args) { 1768 | const streams = []; 1769 | const streamIds = []; 1770 | for (let i = 0; i < args.length; i++) { 1771 | const argument = args[i]; 1772 | if (this._isObservable(argument)) { 1773 | const streamId = this._invocationId; 1774 | this._invocationId++; 1775 | // Store the stream for later use 1776 | streams[streamId] = argument; 1777 | streamIds.push(streamId.toString()); 1778 | // remove stream from args 1779 | args.splice(i, 1); 1780 | } 1781 | } 1782 | return [streams, streamIds]; 1783 | } 1784 | _isObservable(arg) { 1785 | // This allows other stream implementations to just work (like rxjs) 1786 | return arg && arg.subscribe && typeof arg.subscribe === "function"; 1787 | } 1788 | _createStreamInvocation(methodName, args, streamIds) { 1789 | const invocationId = this._invocationId; 1790 | this._invocationId++; 1791 | if (streamIds.length !== 0) { 1792 | return { 1793 | arguments: args, 1794 | invocationId: invocationId.toString(), 1795 | streamIds, 1796 | target: methodName, 1797 | type: MessageType.StreamInvocation, 1798 | }; 1799 | } 1800 | else { 1801 | return { 1802 | arguments: args, 1803 | invocationId: invocationId.toString(), 1804 | target: methodName, 1805 | type: MessageType.StreamInvocation, 1806 | }; 1807 | } 1808 | } 1809 | _createCancelInvocation(id) { 1810 | return { 1811 | invocationId: id, 1812 | type: MessageType.CancelInvocation, 1813 | }; 1814 | } 1815 | _createStreamItemMessage(id, item) { 1816 | return { 1817 | invocationId: id, 1818 | item, 1819 | type: MessageType.StreamItem, 1820 | }; 1821 | } 1822 | _createCompletionMessage(id, error, result) { 1823 | if (error) { 1824 | return { 1825 | error, 1826 | invocationId: id, 1827 | type: MessageType.Completion, 1828 | }; 1829 | } 1830 | return { 1831 | invocationId: id, 1832 | result, 1833 | type: MessageType.Completion, 1834 | }; 1835 | } 1836 | } 1837 | 1838 | ;// CONCATENATED MODULE: ./src/DefaultReconnectPolicy.ts 1839 | // Licensed to the .NET Foundation under one or more agreements. 1840 | // The .NET Foundation licenses this file to you under the MIT license. 1841 | // 0, 2, 10, 30 second delays before reconnect attempts. 1842 | const DEFAULT_RETRY_DELAYS_IN_MILLISECONDS = [0, 2000, 10000, 30000, null]; 1843 | /** @private */ 1844 | class DefaultReconnectPolicy { 1845 | constructor(retryDelays) { 1846 | this._retryDelays = retryDelays !== undefined ? [...retryDelays, null] : DEFAULT_RETRY_DELAYS_IN_MILLISECONDS; 1847 | } 1848 | nextRetryDelayInMilliseconds(retryContext) { 1849 | return this._retryDelays[retryContext.previousRetryCount]; 1850 | } 1851 | } 1852 | 1853 | ;// CONCATENATED MODULE: ./src/HeaderNames.ts 1854 | // Licensed to the .NET Foundation under one or more agreements. 1855 | // The .NET Foundation licenses this file to you under the MIT license. 1856 | class HeaderNames { 1857 | } 1858 | HeaderNames.Authorization = "Authorization"; 1859 | HeaderNames.Cookie = "Cookie"; 1860 | 1861 | ;// CONCATENATED MODULE: ./src/AccessTokenHttpClient.ts 1862 | // Licensed to the .NET Foundation under one or more agreements. 1863 | // The .NET Foundation licenses this file to you under the MIT license. 1864 | 1865 | 1866 | /** @private */ 1867 | class AccessTokenHttpClient extends HttpClient { 1868 | constructor(innerClient, accessTokenFactory) { 1869 | super(); 1870 | this._innerClient = innerClient; 1871 | this._accessTokenFactory = accessTokenFactory; 1872 | } 1873 | async send(request) { 1874 | let allowRetry = true; 1875 | if (this._accessTokenFactory && (!this._accessToken || (request.url && request.url.indexOf("/negotiate?") > 0))) { 1876 | // don't retry if the request is a negotiate or if we just got a potentially new token from the access token factory 1877 | allowRetry = false; 1878 | this._accessToken = await this._accessTokenFactory(); 1879 | } 1880 | this._setAuthorizationHeader(request); 1881 | const response = await this._innerClient.send(request); 1882 | if (allowRetry && response.statusCode === 401 && this._accessTokenFactory) { 1883 | this._accessToken = await this._accessTokenFactory(); 1884 | this._setAuthorizationHeader(request); 1885 | return await this._innerClient.send(request); 1886 | } 1887 | return response; 1888 | } 1889 | _setAuthorizationHeader(request) { 1890 | if (!request.headers) { 1891 | request.headers = {}; 1892 | } 1893 | if (this._accessToken) { 1894 | request.headers[HeaderNames.Authorization] = `Bearer ${this._accessToken}`; 1895 | } 1896 | // don't remove the header if there isn't an access token factory, the user manually added the header in this case 1897 | else if (this._accessTokenFactory) { 1898 | if (request.headers[HeaderNames.Authorization]) { 1899 | delete request.headers[HeaderNames.Authorization]; 1900 | } 1901 | } 1902 | } 1903 | getCookieString(url) { 1904 | return this._innerClient.getCookieString(url); 1905 | } 1906 | } 1907 | 1908 | ;// CONCATENATED MODULE: ./src/ITransport.ts 1909 | // Licensed to the .NET Foundation under one or more agreements. 1910 | // The .NET Foundation licenses this file to you under the MIT license. 1911 | // This will be treated as a bit flag in the future, so we keep it using power-of-two values. 1912 | /** Specifies a specific HTTP transport type. */ 1913 | var HttpTransportType; 1914 | (function (HttpTransportType) { 1915 | /** Specifies no transport preference. */ 1916 | HttpTransportType[HttpTransportType["None"] = 0] = "None"; 1917 | /** Specifies the WebSockets transport. */ 1918 | HttpTransportType[HttpTransportType["WebSockets"] = 1] = "WebSockets"; 1919 | /** Specifies the Server-Sent Events transport. */ 1920 | HttpTransportType[HttpTransportType["ServerSentEvents"] = 2] = "ServerSentEvents"; 1921 | /** Specifies the Long Polling transport. */ 1922 | HttpTransportType[HttpTransportType["LongPolling"] = 4] = "LongPolling"; 1923 | })(HttpTransportType || (HttpTransportType = {})); 1924 | /** Specifies the transfer format for a connection. */ 1925 | var TransferFormat; 1926 | (function (TransferFormat) { 1927 | /** Specifies that only text data will be transmitted over the connection. */ 1928 | TransferFormat[TransferFormat["Text"] = 1] = "Text"; 1929 | /** Specifies that binary data will be transmitted over the connection. */ 1930 | TransferFormat[TransferFormat["Binary"] = 2] = "Binary"; 1931 | })(TransferFormat || (TransferFormat = {})); 1932 | 1933 | ;// CONCATENATED MODULE: ./src/AbortController.ts 1934 | // Licensed to the .NET Foundation under one or more agreements. 1935 | // The .NET Foundation licenses this file to you under the MIT license. 1936 | // Rough polyfill of https://developer.mozilla.org/en-US/docs/Web/API/AbortController 1937 | // We don't actually ever use the API being polyfilled, we always use the polyfill because 1938 | // it's a very new API right now. 1939 | // Not exported from index. 1940 | /** @private */ 1941 | class AbortController_AbortController { 1942 | constructor() { 1943 | this._isAborted = false; 1944 | this.onabort = null; 1945 | } 1946 | abort() { 1947 | if (!this._isAborted) { 1948 | this._isAborted = true; 1949 | if (this.onabort) { 1950 | this.onabort(); 1951 | } 1952 | } 1953 | } 1954 | get signal() { 1955 | return this; 1956 | } 1957 | get aborted() { 1958 | return this._isAborted; 1959 | } 1960 | } 1961 | 1962 | ;// CONCATENATED MODULE: ./src/LongPollingTransport.ts 1963 | // Licensed to the .NET Foundation under one or more agreements. 1964 | // The .NET Foundation licenses this file to you under the MIT license. 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | // Not exported from 'index', this type is internal. 1971 | /** @private */ 1972 | class LongPollingTransport { 1973 | constructor(httpClient, logger, options) { 1974 | this._httpClient = httpClient; 1975 | this._logger = logger; 1976 | this._pollAbort = new AbortController_AbortController(); 1977 | this._options = options; 1978 | this._running = false; 1979 | this.onreceive = null; 1980 | this.onclose = null; 1981 | } 1982 | // This is an internal type, not exported from 'index' so this is really just internal. 1983 | get pollAborted() { 1984 | return this._pollAbort.aborted; 1985 | } 1986 | async connect(url, transferFormat) { 1987 | Arg.isRequired(url, "url"); 1988 | Arg.isRequired(transferFormat, "transferFormat"); 1989 | Arg.isIn(transferFormat, TransferFormat, "transferFormat"); 1990 | this._url = url; 1991 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Connecting."); 1992 | // Allow binary format on Node and Browsers that support binary content (indicated by the presence of responseType property) 1993 | if (transferFormat === TransferFormat.Binary && 1994 | (typeof XMLHttpRequest !== "undefined" && typeof new XMLHttpRequest().responseType !== "string")) { 1995 | throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported."); 1996 | } 1997 | const [name, value] = getUserAgentHeader(); 1998 | const headers = { [name]: value, ...this._options.headers }; 1999 | const pollOptions = { 2000 | abortSignal: this._pollAbort.signal, 2001 | headers, 2002 | timeout: 100000, 2003 | withCredentials: this._options.withCredentials, 2004 | }; 2005 | if (transferFormat === TransferFormat.Binary) { 2006 | pollOptions.responseType = "arraybuffer"; 2007 | } 2008 | // Make initial long polling request 2009 | // Server uses first long polling request to finish initializing connection and it returns without data 2010 | const pollUrl = `${url}&_=${Date.now()}`; 2011 | this._logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}.`); 2012 | const response = await this._httpClient.get(pollUrl, pollOptions); 2013 | if (response.statusCode !== 200) { 2014 | this._logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}.`); 2015 | // Mark running as false so that the poll immediately ends and runs the close logic 2016 | this._closeError = new HttpError(response.statusText || "", response.statusCode); 2017 | this._running = false; 2018 | } 2019 | else { 2020 | this._running = true; 2021 | } 2022 | this._receiving = this._poll(this._url, pollOptions); 2023 | } 2024 | async _poll(url, pollOptions) { 2025 | try { 2026 | while (this._running) { 2027 | try { 2028 | const pollUrl = `${url}&_=${Date.now()}`; 2029 | this._logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}.`); 2030 | const response = await this._httpClient.get(pollUrl, pollOptions); 2031 | if (response.statusCode === 204) { 2032 | this._logger.log(LogLevel.Information, "(LongPolling transport) Poll terminated by server."); 2033 | this._running = false; 2034 | } 2035 | else if (response.statusCode !== 200) { 2036 | this._logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}.`); 2037 | // Unexpected status code 2038 | this._closeError = new HttpError(response.statusText || "", response.statusCode); 2039 | this._running = false; 2040 | } 2041 | else { 2042 | // Process the response 2043 | if (response.content) { 2044 | this._logger.log(LogLevel.Trace, `(LongPolling transport) data received. ${getDataDetail(response.content, this._options.logMessageContent)}.`); 2045 | if (this.onreceive) { 2046 | this.onreceive(response.content); 2047 | } 2048 | } 2049 | else { 2050 | // This is another way timeout manifest. 2051 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); 2052 | } 2053 | } 2054 | } 2055 | catch (e) { 2056 | if (!this._running) { 2057 | // Log but disregard errors that occur after stopping 2058 | this._logger.log(LogLevel.Trace, `(LongPolling transport) Poll errored after shutdown: ${e.message}`); 2059 | } 2060 | else { 2061 | if (e instanceof TimeoutError) { 2062 | // Ignore timeouts and reissue the poll. 2063 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); 2064 | } 2065 | else { 2066 | // Close the connection with the error as the result. 2067 | this._closeError = e; 2068 | this._running = false; 2069 | } 2070 | } 2071 | } 2072 | } 2073 | } 2074 | finally { 2075 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Polling complete."); 2076 | // We will reach here with pollAborted==false when the server returned a response causing the transport to stop. 2077 | // If pollAborted==true then client initiated the stop and the stop method will raise the close event after DELETE is sent. 2078 | if (!this.pollAborted) { 2079 | this._raiseOnClose(); 2080 | } 2081 | } 2082 | } 2083 | async send(data) { 2084 | if (!this._running) { 2085 | return Promise.reject(new Error("Cannot send until the transport is connected")); 2086 | } 2087 | return sendMessage(this._logger, "LongPolling", this._httpClient, this._url, data, this._options); 2088 | } 2089 | async stop() { 2090 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Stopping polling."); 2091 | // Tell receiving loop to stop, abort any current request, and then wait for it to finish 2092 | this._running = false; 2093 | this._pollAbort.abort(); 2094 | try { 2095 | await this._receiving; 2096 | // Send DELETE to clean up long polling on the server 2097 | this._logger.log(LogLevel.Trace, `(LongPolling transport) sending DELETE request to ${this._url}.`); 2098 | const headers = {}; 2099 | const [name, value] = getUserAgentHeader(); 2100 | headers[name] = value; 2101 | const deleteOptions = { 2102 | headers: { ...headers, ...this._options.headers }, 2103 | timeout: this._options.timeout, 2104 | withCredentials: this._options.withCredentials, 2105 | }; 2106 | await this._httpClient.delete(this._url, deleteOptions); 2107 | this._logger.log(LogLevel.Trace, "(LongPolling transport) DELETE request sent."); 2108 | } 2109 | finally { 2110 | this._logger.log(LogLevel.Trace, "(LongPolling transport) Stop finished."); 2111 | // Raise close event here instead of in polling 2112 | // It needs to happen after the DELETE request is sent 2113 | this._raiseOnClose(); 2114 | } 2115 | } 2116 | _raiseOnClose() { 2117 | if (this.onclose) { 2118 | let logMessage = "(LongPolling transport) Firing onclose event."; 2119 | if (this._closeError) { 2120 | logMessage += " Error: " + this._closeError; 2121 | } 2122 | this._logger.log(LogLevel.Trace, logMessage); 2123 | this.onclose(this._closeError); 2124 | } 2125 | } 2126 | } 2127 | 2128 | ;// CONCATENATED MODULE: ./src/ServerSentEventsTransport.ts 2129 | // Licensed to the .NET Foundation under one or more agreements. 2130 | // The .NET Foundation licenses this file to you under the MIT license. 2131 | 2132 | 2133 | 2134 | /** @private */ 2135 | class ServerSentEventsTransport { 2136 | constructor(httpClient, accessToken, logger, options) { 2137 | this._httpClient = httpClient; 2138 | this._accessToken = accessToken; 2139 | this._logger = logger; 2140 | this._options = options; 2141 | this.onreceive = null; 2142 | this.onclose = null; 2143 | } 2144 | async connect(url, transferFormat) { 2145 | Arg.isRequired(url, "url"); 2146 | Arg.isRequired(transferFormat, "transferFormat"); 2147 | Arg.isIn(transferFormat, TransferFormat, "transferFormat"); 2148 | this._logger.log(LogLevel.Trace, "(SSE transport) Connecting."); 2149 | // set url before accessTokenFactory because this._url is only for send and we set the auth header instead of the query string for send 2150 | this._url = url; 2151 | if (this._accessToken) { 2152 | url += (url.indexOf("?") < 0 ? "?" : "&") + `access_token=${encodeURIComponent(this._accessToken)}`; 2153 | } 2154 | return new Promise((resolve, reject) => { 2155 | let opened = false; 2156 | if (transferFormat !== TransferFormat.Text) { 2157 | reject(new Error("The Server-Sent Events transport only supports the 'Text' transfer format")); 2158 | return; 2159 | } 2160 | let eventSource; 2161 | if (Platform.isBrowser || Platform.isWebWorker) { 2162 | eventSource = new this._options.EventSource(url, { withCredentials: this._options.withCredentials }); 2163 | } 2164 | else { 2165 | // Non-browser passes cookies via the dictionary 2166 | const cookies = this._httpClient.getCookieString(url); 2167 | const headers = {}; 2168 | headers.Cookie = cookies; 2169 | const [name, value] = getUserAgentHeader(); 2170 | headers[name] = value; 2171 | eventSource = new this._options.EventSource(url, { withCredentials: this._options.withCredentials, headers: { ...headers, ...this._options.headers } }); 2172 | } 2173 | try { 2174 | eventSource.onmessage = (e) => { 2175 | if (this.onreceive) { 2176 | try { 2177 | this._logger.log(LogLevel.Trace, `(SSE transport) data received. ${getDataDetail(e.data, this._options.logMessageContent)}.`); 2178 | this.onreceive(e.data); 2179 | } 2180 | catch (error) { 2181 | this._close(error); 2182 | return; 2183 | } 2184 | } 2185 | }; 2186 | // @ts-ignore: not using event on purpose 2187 | eventSource.onerror = (e) => { 2188 | // EventSource doesn't give any useful information about server side closes. 2189 | if (opened) { 2190 | this._close(); 2191 | } 2192 | else { 2193 | reject(new Error("EventSource failed to connect. The connection could not be found on the server," 2194 | + " either the connection ID is not present on the server, or a proxy is refusing/buffering the connection." 2195 | + " If you have multiple servers check that sticky sessions are enabled.")); 2196 | } 2197 | }; 2198 | eventSource.onopen = () => { 2199 | this._logger.log(LogLevel.Information, `SSE connected to ${this._url}`); 2200 | this._eventSource = eventSource; 2201 | opened = true; 2202 | resolve(); 2203 | }; 2204 | } 2205 | catch (e) { 2206 | reject(e); 2207 | return; 2208 | } 2209 | }); 2210 | } 2211 | async send(data) { 2212 | if (!this._eventSource) { 2213 | return Promise.reject(new Error("Cannot send until the transport is connected")); 2214 | } 2215 | return sendMessage(this._logger, "SSE", this._httpClient, this._url, data, this._options); 2216 | } 2217 | stop() { 2218 | this._close(); 2219 | return Promise.resolve(); 2220 | } 2221 | _close(e) { 2222 | if (this._eventSource) { 2223 | this._eventSource.close(); 2224 | this._eventSource = undefined; 2225 | if (this.onclose) { 2226 | this.onclose(e); 2227 | } 2228 | } 2229 | } 2230 | } 2231 | 2232 | ;// CONCATENATED MODULE: ./src/WebSocketTransport.ts 2233 | // Licensed to the .NET Foundation under one or more agreements. 2234 | // The .NET Foundation licenses this file to you under the MIT license. 2235 | 2236 | 2237 | 2238 | 2239 | /** @private */ 2240 | class WebSocketTransport { 2241 | constructor(httpClient, accessTokenFactory, logger, logMessageContent, webSocketConstructor, headers) { 2242 | this._logger = logger; 2243 | this._accessTokenFactory = accessTokenFactory; 2244 | this._logMessageContent = logMessageContent; 2245 | this._webSocketConstructor = webSocketConstructor; 2246 | this._httpClient = httpClient; 2247 | this.onreceive = null; 2248 | this.onclose = null; 2249 | this._headers = headers; 2250 | } 2251 | async connect(url, transferFormat) { 2252 | Arg.isRequired(url, "url"); 2253 | Arg.isRequired(transferFormat, "transferFormat"); 2254 | Arg.isIn(transferFormat, TransferFormat, "transferFormat"); 2255 | this._logger.log(LogLevel.Trace, "(WebSockets transport) Connecting."); 2256 | let token; 2257 | if (this._accessTokenFactory) { 2258 | token = await this._accessTokenFactory(); 2259 | } 2260 | return new Promise((resolve, reject) => { 2261 | url = url.replace(/^http/, "ws"); 2262 | let webSocket; 2263 | const cookies = this._httpClient.getCookieString(url); 2264 | let opened = false; 2265 | if (Platform.isNode || Platform.isReactNative) { 2266 | const headers = {}; 2267 | const [name, value] = getUserAgentHeader(); 2268 | headers[name] = value; 2269 | if (token) { 2270 | headers[HeaderNames.Authorization] = `Bearer ${token}`; 2271 | } 2272 | if (cookies) { 2273 | headers[HeaderNames.Cookie] = cookies; 2274 | } 2275 | // Only pass headers when in non-browser environments 2276 | webSocket = new this._webSocketConstructor(url, undefined, { 2277 | headers: { ...headers, ...this._headers }, 2278 | }); 2279 | } 2280 | else { 2281 | if (token) { 2282 | url += (url.indexOf("?") < 0 ? "?" : "&") + `access_token=${encodeURIComponent(token)}`; 2283 | } 2284 | } 2285 | if (!webSocket) { 2286 | // Chrome is not happy with passing 'undefined' as protocol 2287 | webSocket = new this._webSocketConstructor(url); 2288 | } 2289 | if (transferFormat === TransferFormat.Binary) { 2290 | webSocket.binaryType = "arraybuffer"; 2291 | } 2292 | webSocket.onopen = (_event) => { 2293 | this._logger.log(LogLevel.Information, `WebSocket connected to ${url}.`); 2294 | this._webSocket = webSocket; 2295 | opened = true; 2296 | resolve(); 2297 | }; 2298 | webSocket.onerror = (event) => { 2299 | let error = null; 2300 | // ErrorEvent is a browser only type we need to check if the type exists before using it 2301 | if (typeof ErrorEvent !== "undefined" && event instanceof ErrorEvent) { 2302 | error = event.error; 2303 | } 2304 | else { 2305 | error = "There was an error with the transport"; 2306 | } 2307 | this._logger.log(LogLevel.Information, `(WebSockets transport) ${error}.`); 2308 | }; 2309 | webSocket.onmessage = (message) => { 2310 | this._logger.log(LogLevel.Trace, `(WebSockets transport) data received. ${getDataDetail(message.data, this._logMessageContent)}.`); 2311 | if (this.onreceive) { 2312 | try { 2313 | this.onreceive(message.data); 2314 | } 2315 | catch (error) { 2316 | this._close(error); 2317 | return; 2318 | } 2319 | } 2320 | }; 2321 | webSocket.onclose = (event) => { 2322 | // Don't call close handler if connection was never established 2323 | // We'll reject the connect call instead 2324 | if (opened) { 2325 | this._close(event); 2326 | } 2327 | else { 2328 | let error = null; 2329 | // ErrorEvent is a browser only type we need to check if the type exists before using it 2330 | if (typeof ErrorEvent !== "undefined" && event instanceof ErrorEvent) { 2331 | error = event.error; 2332 | } 2333 | else { 2334 | error = "WebSocket failed to connect. The connection could not be found on the server," 2335 | + " either the endpoint may not be a SignalR endpoint," 2336 | + " the connection ID is not present on the server, or there is a proxy blocking WebSockets." 2337 | + " If you have multiple servers check that sticky sessions are enabled."; 2338 | } 2339 | reject(new Error(error)); 2340 | } 2341 | }; 2342 | }); 2343 | } 2344 | send(data) { 2345 | if (this._webSocket && this._webSocket.readyState === this._webSocketConstructor.OPEN) { 2346 | this._logger.log(LogLevel.Trace, `(WebSockets transport) sending data. ${getDataDetail(data, this._logMessageContent)}.`); 2347 | this._webSocket.send(data); 2348 | return Promise.resolve(); 2349 | } 2350 | return Promise.reject("WebSocket is not in the OPEN state"); 2351 | } 2352 | stop() { 2353 | if (this._webSocket) { 2354 | // Manually invoke onclose callback inline so we know the HttpConnection was closed properly before returning 2355 | // This also solves an issue where websocket.onclose could take 18+ seconds to trigger during network disconnects 2356 | this._close(undefined); 2357 | } 2358 | return Promise.resolve(); 2359 | } 2360 | _close(event) { 2361 | // webSocket will be null if the transport did not start successfully 2362 | if (this._webSocket) { 2363 | // Clear websocket handlers because we are considering the socket closed now 2364 | this._webSocket.onclose = () => { }; 2365 | this._webSocket.onmessage = () => { }; 2366 | this._webSocket.onerror = () => { }; 2367 | this._webSocket.close(); 2368 | this._webSocket = undefined; 2369 | } 2370 | this._logger.log(LogLevel.Trace, "(WebSockets transport) socket closed."); 2371 | if (this.onclose) { 2372 | if (this._isCloseEvent(event) && (event.wasClean === false || event.code !== 1000)) { 2373 | this.onclose(new Error(`WebSocket closed with status code: ${event.code} (${event.reason || "no reason given"}).`)); 2374 | } 2375 | else if (event instanceof Error) { 2376 | this.onclose(event); 2377 | } 2378 | else { 2379 | this.onclose(); 2380 | } 2381 | } 2382 | } 2383 | _isCloseEvent(event) { 2384 | return event && typeof event.wasClean === "boolean" && typeof event.code === "number"; 2385 | } 2386 | } 2387 | 2388 | ;// CONCATENATED MODULE: ./src/HttpConnection.ts 2389 | // Licensed to the .NET Foundation under one or more agreements. 2390 | // The .NET Foundation licenses this file to you under the MIT license. 2391 | 2392 | 2393 | 2394 | 2395 | 2396 | 2397 | 2398 | 2399 | 2400 | const MAX_REDIRECTS = 100; 2401 | /** @private */ 2402 | class HttpConnection { 2403 | constructor(url, options = {}) { 2404 | this._stopPromiseResolver = () => { }; 2405 | this.features = {}; 2406 | this._negotiateVersion = 1; 2407 | Arg.isRequired(url, "url"); 2408 | this._logger = createLogger(options.logger); 2409 | this.baseUrl = this._resolveUrl(url); 2410 | options = options || {}; 2411 | options.logMessageContent = options.logMessageContent === undefined ? false : options.logMessageContent; 2412 | if (typeof options.withCredentials === "boolean" || options.withCredentials === undefined) { 2413 | options.withCredentials = options.withCredentials === undefined ? true : options.withCredentials; 2414 | } 2415 | else { 2416 | throw new Error("withCredentials option was not a 'boolean' or 'undefined' value"); 2417 | } 2418 | options.timeout = options.timeout === undefined ? 100 * 1000 : options.timeout; 2419 | let webSocketModule = null; 2420 | let eventSourceModule = null; 2421 | if (Platform.isNode && "function" !== "undefined") { 2422 | // In order to ignore the dynamic require in webpack builds we need to do this magic 2423 | // @ts-ignore: TS doesn't know about these names 2424 | const requireFunc = true ? require : 0; 2425 | webSocketModule = requireFunc("ws"); 2426 | eventSourceModule = requireFunc("eventsource"); 2427 | } 2428 | if (!Platform.isNode && typeof WebSocket !== "undefined" && !options.WebSocket) { 2429 | options.WebSocket = WebSocket; 2430 | } 2431 | else if (Platform.isNode && !options.WebSocket) { 2432 | if (webSocketModule) { 2433 | options.WebSocket = webSocketModule; 2434 | } 2435 | } 2436 | if (!Platform.isNode && typeof EventSource !== "undefined" && !options.EventSource) { 2437 | options.EventSource = EventSource; 2438 | } 2439 | else if (Platform.isNode && !options.EventSource) { 2440 | if (typeof eventSourceModule !== "undefined") { 2441 | options.EventSource = eventSourceModule; 2442 | } 2443 | } 2444 | this._httpClient = new AccessTokenHttpClient(options.httpClient || new DefaultHttpClient(this._logger), options.accessTokenFactory); 2445 | this._connectionState = "Disconnected" /* Disconnected */; 2446 | this._connectionStarted = false; 2447 | this._options = options; 2448 | this.onreceive = null; 2449 | this.onclose = null; 2450 | } 2451 | async start(transferFormat) { 2452 | transferFormat = transferFormat || TransferFormat.Binary; 2453 | Arg.isIn(transferFormat, TransferFormat, "transferFormat"); 2454 | this._logger.log(LogLevel.Debug, `Starting connection with transfer format '${TransferFormat[transferFormat]}'.`); 2455 | if (this._connectionState !== "Disconnected" /* Disconnected */) { 2456 | return Promise.reject(new Error("Cannot start an HttpConnection that is not in the 'Disconnected' state.")); 2457 | } 2458 | this._connectionState = "Connecting" /* Connecting */; 2459 | this._startInternalPromise = this._startInternal(transferFormat); 2460 | await this._startInternalPromise; 2461 | // The TypeScript compiler thinks that connectionState must be Connecting here. The TypeScript compiler is wrong. 2462 | if (this._connectionState === "Disconnecting" /* Disconnecting */) { 2463 | // stop() was called and transitioned the client into the Disconnecting state. 2464 | const message = "Failed to start the HttpConnection before stop() was called."; 2465 | this._logger.log(LogLevel.Error, message); 2466 | // We cannot await stopPromise inside startInternal since stopInternal awaits the startInternalPromise. 2467 | await this._stopPromise; 2468 | return Promise.reject(new AbortError(message)); 2469 | } 2470 | else if (this._connectionState !== "Connected" /* Connected */) { 2471 | // stop() was called and transitioned the client into the Disconnecting state. 2472 | const message = "HttpConnection.startInternal completed gracefully but didn't enter the connection into the connected state!"; 2473 | this._logger.log(LogLevel.Error, message); 2474 | return Promise.reject(new AbortError(message)); 2475 | } 2476 | this._connectionStarted = true; 2477 | } 2478 | send(data) { 2479 | if (this._connectionState !== "Connected" /* Connected */) { 2480 | return Promise.reject(new Error("Cannot send data if the connection is not in the 'Connected' State.")); 2481 | } 2482 | if (!this._sendQueue) { 2483 | this._sendQueue = new TransportSendQueue(this.transport); 2484 | } 2485 | // Transport will not be null if state is connected 2486 | return this._sendQueue.send(data); 2487 | } 2488 | async stop(error) { 2489 | if (this._connectionState === "Disconnected" /* Disconnected */) { 2490 | this._logger.log(LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnected state.`); 2491 | return Promise.resolve(); 2492 | } 2493 | if (this._connectionState === "Disconnecting" /* Disconnecting */) { 2494 | this._logger.log(LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnecting state.`); 2495 | return this._stopPromise; 2496 | } 2497 | this._connectionState = "Disconnecting" /* Disconnecting */; 2498 | this._stopPromise = new Promise((resolve) => { 2499 | // Don't complete stop() until stopConnection() completes. 2500 | this._stopPromiseResolver = resolve; 2501 | }); 2502 | // stopInternal should never throw so just observe it. 2503 | await this._stopInternal(error); 2504 | await this._stopPromise; 2505 | } 2506 | async _stopInternal(error) { 2507 | // Set error as soon as possible otherwise there is a race between 2508 | // the transport closing and providing an error and the error from a close message 2509 | // We would prefer the close message error. 2510 | this._stopError = error; 2511 | try { 2512 | await this._startInternalPromise; 2513 | } 2514 | catch (e) { 2515 | // This exception is returned to the user as a rejected Promise from the start method. 2516 | } 2517 | // The transport's onclose will trigger stopConnection which will run our onclose event. 2518 | // The transport should always be set if currently connected. If it wasn't set, it's likely because 2519 | // stop was called during start() and start() failed. 2520 | if (this.transport) { 2521 | try { 2522 | await this.transport.stop(); 2523 | } 2524 | catch (e) { 2525 | this._logger.log(LogLevel.Error, `HttpConnection.transport.stop() threw error '${e}'.`); 2526 | this._stopConnection(); 2527 | } 2528 | this.transport = undefined; 2529 | } 2530 | else { 2531 | this._logger.log(LogLevel.Debug, "HttpConnection.transport is undefined in HttpConnection.stop() because start() failed."); 2532 | } 2533 | } 2534 | async _startInternal(transferFormat) { 2535 | // Store the original base url and the access token factory since they may change 2536 | // as part of negotiating 2537 | let url = this.baseUrl; 2538 | this._accessTokenFactory = this._options.accessTokenFactory; 2539 | this._httpClient._accessTokenFactory = this._accessTokenFactory; 2540 | try { 2541 | if (this._options.skipNegotiation) { 2542 | if (this._options.transport === HttpTransportType.WebSockets) { 2543 | // No need to add a connection ID in this case 2544 | this.transport = this._constructTransport(HttpTransportType.WebSockets); 2545 | // We should just call connect directly in this case. 2546 | // No fallback or negotiate in this case. 2547 | await this._startTransport(url, transferFormat); 2548 | } 2549 | else { 2550 | throw new Error("Negotiation can only be skipped when using the WebSocket transport directly."); 2551 | } 2552 | } 2553 | else { 2554 | let negotiateResponse = null; 2555 | let redirects = 0; 2556 | do { 2557 | negotiateResponse = await this._getNegotiationResponse(url); 2558 | // the user tries to stop the connection when it is being started 2559 | if (this._connectionState === "Disconnecting" /* Disconnecting */ || this._connectionState === "Disconnected" /* Disconnected */) { 2560 | throw new AbortError("The connection was stopped during negotiation."); 2561 | } 2562 | if (negotiateResponse.error) { 2563 | throw new Error(negotiateResponse.error); 2564 | } 2565 | if (negotiateResponse.ProtocolVersion) { 2566 | throw new Error("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details."); 2567 | } 2568 | if (negotiateResponse.url) { 2569 | url = negotiateResponse.url; 2570 | } 2571 | if (negotiateResponse.accessToken) { 2572 | // Replace the current access token factory with one that uses 2573 | // the returned access token 2574 | const accessToken = negotiateResponse.accessToken; 2575 | this._accessTokenFactory = () => accessToken; 2576 | // set the factory to undefined so the AccessTokenHttpClient won't retry with the same token, since we know it won't change until a connection restart 2577 | this._httpClient._accessToken = accessToken; 2578 | this._httpClient._accessTokenFactory = undefined; 2579 | } 2580 | redirects++; 2581 | } while (negotiateResponse.url && redirects < MAX_REDIRECTS); 2582 | if (redirects === MAX_REDIRECTS && negotiateResponse.url) { 2583 | throw new Error("Negotiate redirection limit exceeded."); 2584 | } 2585 | await this._createTransport(url, this._options.transport, negotiateResponse, transferFormat); 2586 | } 2587 | if (this.transport instanceof LongPollingTransport) { 2588 | this.features.inherentKeepAlive = true; 2589 | } 2590 | if (this._connectionState === "Connecting" /* Connecting */) { 2591 | // Ensure the connection transitions to the connected state prior to completing this.startInternalPromise. 2592 | // start() will handle the case when stop was called and startInternal exits still in the disconnecting state. 2593 | this._logger.log(LogLevel.Debug, "The HttpConnection connected successfully."); 2594 | this._connectionState = "Connected" /* Connected */; 2595 | } 2596 | // stop() is waiting on us via this.startInternalPromise so keep this.transport around so it can clean up. 2597 | // This is the only case startInternal can exit in neither the connected nor disconnected state because stopConnection() 2598 | // will transition to the disconnected state. start() will wait for the transition using the stopPromise. 2599 | } 2600 | catch (e) { 2601 | this._logger.log(LogLevel.Error, "Failed to start the connection: " + e); 2602 | this._connectionState = "Disconnected" /* Disconnected */; 2603 | this.transport = undefined; 2604 | // if start fails, any active calls to stop assume that start will complete the stop promise 2605 | this._stopPromiseResolver(); 2606 | return Promise.reject(e); 2607 | } 2608 | } 2609 | async _getNegotiationResponse(url) { 2610 | const headers = {}; 2611 | const [name, value] = getUserAgentHeader(); 2612 | headers[name] = value; 2613 | const negotiateUrl = this._resolveNegotiateUrl(url); 2614 | this._logger.log(LogLevel.Debug, `Sending negotiation request: ${negotiateUrl}.`); 2615 | try { 2616 | const response = await this._httpClient.post(negotiateUrl, { 2617 | content: "", 2618 | headers: { ...headers, ...this._options.headers }, 2619 | timeout: this._options.timeout, 2620 | withCredentials: this._options.withCredentials, 2621 | }); 2622 | if (response.statusCode !== 200) { 2623 | return Promise.reject(new Error(`Unexpected status code returned from negotiate '${response.statusCode}'`)); 2624 | } 2625 | const negotiateResponse = JSON.parse(response.content); 2626 | if (!negotiateResponse.negotiateVersion || negotiateResponse.negotiateVersion < 1) { 2627 | // Negotiate version 0 doesn't use connectionToken 2628 | // So we set it equal to connectionId so all our logic can use connectionToken without being aware of the negotiate version 2629 | negotiateResponse.connectionToken = negotiateResponse.connectionId; 2630 | } 2631 | return negotiateResponse; 2632 | } 2633 | catch (e) { 2634 | let errorMessage = "Failed to complete negotiation with the server: " + e; 2635 | if (e instanceof HttpError) { 2636 | if (e.statusCode === 404) { 2637 | errorMessage = errorMessage + " Either this is not a SignalR endpoint or there is a proxy blocking the connection."; 2638 | } 2639 | } 2640 | this._logger.log(LogLevel.Error, errorMessage); 2641 | return Promise.reject(new FailedToNegotiateWithServerError(errorMessage)); 2642 | } 2643 | } 2644 | _createConnectUrl(url, connectionToken) { 2645 | if (!connectionToken) { 2646 | return url; 2647 | } 2648 | return url + (url.indexOf("?") === -1 ? "?" : "&") + `id=${connectionToken}`; 2649 | } 2650 | async _createTransport(url, requestedTransport, negotiateResponse, requestedTransferFormat) { 2651 | let connectUrl = this._createConnectUrl(url, negotiateResponse.connectionToken); 2652 | if (this._isITransport(requestedTransport)) { 2653 | this._logger.log(LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly."); 2654 | this.transport = requestedTransport; 2655 | await this._startTransport(connectUrl, requestedTransferFormat); 2656 | this.connectionId = negotiateResponse.connectionId; 2657 | return; 2658 | } 2659 | const transportExceptions = []; 2660 | const transports = negotiateResponse.availableTransports || []; 2661 | let negotiate = negotiateResponse; 2662 | for (const endpoint of transports) { 2663 | const transportOrError = this._resolveTransportOrError(endpoint, requestedTransport, requestedTransferFormat); 2664 | if (transportOrError instanceof Error) { 2665 | // Store the error and continue, we don't want to cause a re-negotiate in these cases 2666 | transportExceptions.push(`${endpoint.transport} failed:`); 2667 | transportExceptions.push(transportOrError); 2668 | } 2669 | else if (this._isITransport(transportOrError)) { 2670 | this.transport = transportOrError; 2671 | if (!negotiate) { 2672 | try { 2673 | negotiate = await this._getNegotiationResponse(url); 2674 | } 2675 | catch (ex) { 2676 | return Promise.reject(ex); 2677 | } 2678 | connectUrl = this._createConnectUrl(url, negotiate.connectionToken); 2679 | } 2680 | try { 2681 | await this._startTransport(connectUrl, requestedTransferFormat); 2682 | this.connectionId = negotiate.connectionId; 2683 | return; 2684 | } 2685 | catch (ex) { 2686 | this._logger.log(LogLevel.Error, `Failed to start the transport '${endpoint.transport}': ${ex}`); 2687 | negotiate = undefined; 2688 | transportExceptions.push(new FailedToStartTransportError(`${endpoint.transport} failed: ${ex}`, HttpTransportType[endpoint.transport])); 2689 | if (this._connectionState !== "Connecting" /* Connecting */) { 2690 | const message = "Failed to select transport before stop() was called."; 2691 | this._logger.log(LogLevel.Debug, message); 2692 | return Promise.reject(new AbortError(message)); 2693 | } 2694 | } 2695 | } 2696 | } 2697 | if (transportExceptions.length > 0) { 2698 | return Promise.reject(new AggregateErrors(`Unable to connect to the server with any of the available transports. ${transportExceptions.join(" ")}`, transportExceptions)); 2699 | } 2700 | return Promise.reject(new Error("None of the transports supported by the client are supported by the server.")); 2701 | } 2702 | _constructTransport(transport) { 2703 | switch (transport) { 2704 | case HttpTransportType.WebSockets: 2705 | if (!this._options.WebSocket) { 2706 | throw new Error("'WebSocket' is not supported in your environment."); 2707 | } 2708 | return new WebSocketTransport(this._httpClient, this._accessTokenFactory, this._logger, this._options.logMessageContent, this._options.WebSocket, this._options.headers || {}); 2709 | case HttpTransportType.ServerSentEvents: 2710 | if (!this._options.EventSource) { 2711 | throw new Error("'EventSource' is not supported in your environment."); 2712 | } 2713 | return new ServerSentEventsTransport(this._httpClient, this._httpClient._accessToken, this._logger, this._options); 2714 | case HttpTransportType.LongPolling: 2715 | return new LongPollingTransport(this._httpClient, this._logger, this._options); 2716 | default: 2717 | throw new Error(`Unknown transport: ${transport}.`); 2718 | } 2719 | } 2720 | _startTransport(url, transferFormat) { 2721 | this.transport.onreceive = this.onreceive; 2722 | this.transport.onclose = (e) => this._stopConnection(e); 2723 | return this.transport.connect(url, transferFormat); 2724 | } 2725 | _resolveTransportOrError(endpoint, requestedTransport, requestedTransferFormat) { 2726 | const transport = HttpTransportType[endpoint.transport]; 2727 | if (transport === null || transport === undefined) { 2728 | this._logger.log(LogLevel.Debug, `Skipping transport '${endpoint.transport}' because it is not supported by this client.`); 2729 | return new Error(`Skipping transport '${endpoint.transport}' because it is not supported by this client.`); 2730 | } 2731 | else { 2732 | if (transportMatches(requestedTransport, transport)) { 2733 | const transferFormats = endpoint.transferFormats.map((s) => TransferFormat[s]); 2734 | if (transferFormats.indexOf(requestedTransferFormat) >= 0) { 2735 | if ((transport === HttpTransportType.WebSockets && !this._options.WebSocket) || 2736 | (transport === HttpTransportType.ServerSentEvents && !this._options.EventSource)) { 2737 | this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it is not supported in your environment.'`); 2738 | return new UnsupportedTransportError(`'${HttpTransportType[transport]}' is not supported in your environment.`, transport); 2739 | } 2740 | else { 2741 | this._logger.log(LogLevel.Debug, `Selecting transport '${HttpTransportType[transport]}'.`); 2742 | try { 2743 | return this._constructTransport(transport); 2744 | } 2745 | catch (ex) { 2746 | return ex; 2747 | } 2748 | } 2749 | } 2750 | else { 2751 | this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it does not support the requested transfer format '${TransferFormat[requestedTransferFormat]}'.`); 2752 | return new Error(`'${HttpTransportType[transport]}' does not support ${TransferFormat[requestedTransferFormat]}.`); 2753 | } 2754 | } 2755 | else { 2756 | this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it was disabled by the client.`); 2757 | return new DisabledTransportError(`'${HttpTransportType[transport]}' is disabled by the client.`, transport); 2758 | } 2759 | } 2760 | } 2761 | _isITransport(transport) { 2762 | return transport && typeof (transport) === "object" && "connect" in transport; 2763 | } 2764 | _stopConnection(error) { 2765 | this._logger.log(LogLevel.Debug, `HttpConnection.stopConnection(${error}) called while in state ${this._connectionState}.`); 2766 | this.transport = undefined; 2767 | // If we have a stopError, it takes precedence over the error from the transport 2768 | error = this._stopError || error; 2769 | this._stopError = undefined; 2770 | if (this._connectionState === "Disconnected" /* Disconnected */) { 2771 | this._logger.log(LogLevel.Debug, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is already in the disconnected state.`); 2772 | return; 2773 | } 2774 | if (this._connectionState === "Connecting" /* Connecting */) { 2775 | this._logger.log(LogLevel.Warning, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is still in the connecting state.`); 2776 | throw new Error(`HttpConnection.stopConnection(${error}) was called while the connection is still in the connecting state.`); 2777 | } 2778 | if (this._connectionState === "Disconnecting" /* Disconnecting */) { 2779 | // A call to stop() induced this call to stopConnection and needs to be completed. 2780 | // Any stop() awaiters will be scheduled to continue after the onclose callback fires. 2781 | this._stopPromiseResolver(); 2782 | } 2783 | if (error) { 2784 | this._logger.log(LogLevel.Error, `Connection disconnected with error '${error}'.`); 2785 | } 2786 | else { 2787 | this._logger.log(LogLevel.Information, "Connection disconnected."); 2788 | } 2789 | if (this._sendQueue) { 2790 | this._sendQueue.stop().catch((e) => { 2791 | this._logger.log(LogLevel.Error, `TransportSendQueue.stop() threw error '${e}'.`); 2792 | }); 2793 | this._sendQueue = undefined; 2794 | } 2795 | this.connectionId = undefined; 2796 | this._connectionState = "Disconnected" /* Disconnected */; 2797 | if (this._connectionStarted) { 2798 | this._connectionStarted = false; 2799 | try { 2800 | if (this.onclose) { 2801 | this.onclose(error); 2802 | } 2803 | } 2804 | catch (e) { 2805 | this._logger.log(LogLevel.Error, `HttpConnection.onclose(${error}) threw error '${e}'.`); 2806 | } 2807 | } 2808 | } 2809 | _resolveUrl(url) { 2810 | // startsWith is not supported in IE 2811 | if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) { 2812 | return url; 2813 | } 2814 | if (!Platform.isBrowser) { 2815 | throw new Error(`Cannot resolve '${url}'.`); 2816 | } 2817 | // Setting the url to the href propery of an anchor tag handles normalization 2818 | // for us. There are 3 main cases. 2819 | // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b" 2820 | // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b" 2821 | // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b" 2822 | const aTag = window.document.createElement("a"); 2823 | aTag.href = url; 2824 | this._logger.log(LogLevel.Information, `Normalizing '${url}' to '${aTag.href}'.`); 2825 | return aTag.href; 2826 | } 2827 | _resolveNegotiateUrl(url) { 2828 | const index = url.indexOf("?"); 2829 | let negotiateUrl = url.substring(0, index === -1 ? url.length : index); 2830 | if (negotiateUrl[negotiateUrl.length - 1] !== "/") { 2831 | negotiateUrl += "/"; 2832 | } 2833 | negotiateUrl += "negotiate"; 2834 | negotiateUrl += index === -1 ? "" : url.substring(index); 2835 | if (negotiateUrl.indexOf("negotiateVersion") === -1) { 2836 | negotiateUrl += index === -1 ? "?" : "&"; 2837 | negotiateUrl += "negotiateVersion=" + this._negotiateVersion; 2838 | } 2839 | return negotiateUrl; 2840 | } 2841 | } 2842 | function transportMatches(requestedTransport, actualTransport) { 2843 | return !requestedTransport || ((actualTransport & requestedTransport) !== 0); 2844 | } 2845 | /** @private */ 2846 | class TransportSendQueue { 2847 | constructor(_transport) { 2848 | this._transport = _transport; 2849 | this._buffer = []; 2850 | this._executing = true; 2851 | this._sendBufferedData = new PromiseSource(); 2852 | this._transportResult = new PromiseSource(); 2853 | this._sendLoopPromise = this._sendLoop(); 2854 | } 2855 | send(data) { 2856 | this._bufferData(data); 2857 | if (!this._transportResult) { 2858 | this._transportResult = new PromiseSource(); 2859 | } 2860 | return this._transportResult.promise; 2861 | } 2862 | stop() { 2863 | this._executing = false; 2864 | this._sendBufferedData.resolve(); 2865 | return this._sendLoopPromise; 2866 | } 2867 | _bufferData(data) { 2868 | if (this._buffer.length && typeof (this._buffer[0]) !== typeof (data)) { 2869 | throw new Error(`Expected data to be of type ${typeof (this._buffer)} but was of type ${typeof (data)}`); 2870 | } 2871 | this._buffer.push(data); 2872 | this._sendBufferedData.resolve(); 2873 | } 2874 | async _sendLoop() { 2875 | while (true) { 2876 | await this._sendBufferedData.promise; 2877 | if (!this._executing) { 2878 | if (this._transportResult) { 2879 | this._transportResult.reject("Connection stopped."); 2880 | } 2881 | break; 2882 | } 2883 | this._sendBufferedData = new PromiseSource(); 2884 | const transportResult = this._transportResult; 2885 | this._transportResult = undefined; 2886 | const data = typeof (this._buffer[0]) === "string" ? 2887 | this._buffer.join("") : 2888 | TransportSendQueue._concatBuffers(this._buffer); 2889 | this._buffer.length = 0; 2890 | try { 2891 | await this._transport.send(data); 2892 | transportResult.resolve(); 2893 | } 2894 | catch (error) { 2895 | transportResult.reject(error); 2896 | } 2897 | } 2898 | } 2899 | static _concatBuffers(arrayBuffers) { 2900 | const totalLength = arrayBuffers.map((b) => b.byteLength).reduce((a, b) => a + b); 2901 | const result = new Uint8Array(totalLength); 2902 | let offset = 0; 2903 | for (const item of arrayBuffers) { 2904 | result.set(new Uint8Array(item), offset); 2905 | offset += item.byteLength; 2906 | } 2907 | return result.buffer; 2908 | } 2909 | } 2910 | class PromiseSource { 2911 | constructor() { 2912 | this.promise = new Promise((resolve, reject) => [this._resolver, this._rejecter] = [resolve, reject]); 2913 | } 2914 | resolve() { 2915 | this._resolver(); 2916 | } 2917 | reject(reason) { 2918 | this._rejecter(reason); 2919 | } 2920 | } 2921 | 2922 | ;// CONCATENATED MODULE: ./src/JsonHubProtocol.ts 2923 | // Licensed to the .NET Foundation under one or more agreements. 2924 | // The .NET Foundation licenses this file to you under the MIT license. 2925 | 2926 | 2927 | 2928 | 2929 | 2930 | const JSON_HUB_PROTOCOL_NAME = "json"; 2931 | /** Implements the JSON Hub Protocol. */ 2932 | class JsonHubProtocol { 2933 | constructor() { 2934 | /** @inheritDoc */ 2935 | this.name = JSON_HUB_PROTOCOL_NAME; 2936 | /** @inheritDoc */ 2937 | this.version = 1; 2938 | /** @inheritDoc */ 2939 | this.transferFormat = TransferFormat.Text; 2940 | } 2941 | /** Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation. 2942 | * 2943 | * @param {string} input A string containing the serialized representation. 2944 | * @param {ILogger} logger A logger that will be used to log messages that occur during parsing. 2945 | */ 2946 | parseMessages(input, logger) { 2947 | // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error. 2948 | if (typeof input !== "string") { 2949 | throw new Error("Invalid input for JSON hub protocol. Expected a string."); 2950 | } 2951 | if (!input) { 2952 | return []; 2953 | } 2954 | if (logger === null) { 2955 | logger = NullLogger.instance; 2956 | } 2957 | // Parse the messages 2958 | const messages = TextMessageFormat.parse(input); 2959 | const hubMessages = []; 2960 | for (const message of messages) { 2961 | const parsedMessage = JSON.parse(message); 2962 | if (typeof parsedMessage.type !== "number") { 2963 | throw new Error("Invalid payload."); 2964 | } 2965 | switch (parsedMessage.type) { 2966 | case MessageType.Invocation: 2967 | this._isInvocationMessage(parsedMessage); 2968 | break; 2969 | case MessageType.StreamItem: 2970 | this._isStreamItemMessage(parsedMessage); 2971 | break; 2972 | case MessageType.Completion: 2973 | this._isCompletionMessage(parsedMessage); 2974 | break; 2975 | case MessageType.Ping: 2976 | // Single value, no need to validate 2977 | break; 2978 | case MessageType.Close: 2979 | // All optional values, no need to validate 2980 | break; 2981 | default: 2982 | // Future protocol changes can add message types, old clients can ignore them 2983 | logger.log(LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored."); 2984 | continue; 2985 | } 2986 | hubMessages.push(parsedMessage); 2987 | } 2988 | return hubMessages; 2989 | } 2990 | /** Writes the specified {@link @microsoft/signalr.HubMessage} to a string and returns it. 2991 | * 2992 | * @param {HubMessage} message The message to write. 2993 | * @returns {string} A string containing the serialized representation of the message. 2994 | */ 2995 | writeMessage(message) { 2996 | return TextMessageFormat.write(JSON.stringify(message)); 2997 | } 2998 | _isInvocationMessage(message) { 2999 | this._assertNotEmptyString(message.target, "Invalid payload for Invocation message."); 3000 | if (message.invocationId !== undefined) { 3001 | this._assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message."); 3002 | } 3003 | } 3004 | _isStreamItemMessage(message) { 3005 | this._assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message."); 3006 | if (message.item === undefined) { 3007 | throw new Error("Invalid payload for StreamItem message."); 3008 | } 3009 | } 3010 | _isCompletionMessage(message) { 3011 | if (message.result && message.error) { 3012 | throw new Error("Invalid payload for Completion message."); 3013 | } 3014 | if (!message.result && message.error) { 3015 | this._assertNotEmptyString(message.error, "Invalid payload for Completion message."); 3016 | } 3017 | this._assertNotEmptyString(message.invocationId, "Invalid payload for Completion message."); 3018 | } 3019 | _assertNotEmptyString(value, errorMessage) { 3020 | if (typeof value !== "string" || value === "") { 3021 | throw new Error(errorMessage); 3022 | } 3023 | } 3024 | } 3025 | 3026 | ;// CONCATENATED MODULE: ./src/HubConnectionBuilder.ts 3027 | // Licensed to the .NET Foundation under one or more agreements. 3028 | // The .NET Foundation licenses this file to you under the MIT license. 3029 | 3030 | 3031 | 3032 | 3033 | 3034 | 3035 | 3036 | const LogLevelNameMapping = { 3037 | trace: LogLevel.Trace, 3038 | debug: LogLevel.Debug, 3039 | info: LogLevel.Information, 3040 | information: LogLevel.Information, 3041 | warn: LogLevel.Warning, 3042 | warning: LogLevel.Warning, 3043 | error: LogLevel.Error, 3044 | critical: LogLevel.Critical, 3045 | none: LogLevel.None, 3046 | }; 3047 | function parseLogLevel(name) { 3048 | // Case-insensitive matching via lower-casing 3049 | // Yes, I know case-folding is a complicated problem in Unicode, but we only support 3050 | // the ASCII strings defined in LogLevelNameMapping anyway, so it's fine -anurse. 3051 | const mapping = LogLevelNameMapping[name.toLowerCase()]; 3052 | if (typeof mapping !== "undefined") { 3053 | return mapping; 3054 | } 3055 | else { 3056 | throw new Error(`Unknown log level: ${name}`); 3057 | } 3058 | } 3059 | /** A builder for configuring {@link @microsoft/signalr.HubConnection} instances. */ 3060 | class HubConnectionBuilder { 3061 | configureLogging(logging) { 3062 | Arg.isRequired(logging, "logging"); 3063 | if (isLogger(logging)) { 3064 | this.logger = logging; 3065 | } 3066 | else if (typeof logging === "string") { 3067 | const logLevel = parseLogLevel(logging); 3068 | this.logger = new ConsoleLogger(logLevel); 3069 | } 3070 | else { 3071 | this.logger = new ConsoleLogger(logging); 3072 | } 3073 | return this; 3074 | } 3075 | withUrl(url, transportTypeOrOptions) { 3076 | Arg.isRequired(url, "url"); 3077 | Arg.isNotEmpty(url, "url"); 3078 | this.url = url; 3079 | // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed 3080 | // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called. 3081 | if (typeof transportTypeOrOptions === "object") { 3082 | this.httpConnectionOptions = { ...this.httpConnectionOptions, ...transportTypeOrOptions }; 3083 | } 3084 | else { 3085 | this.httpConnectionOptions = { 3086 | ...this.httpConnectionOptions, 3087 | transport: transportTypeOrOptions, 3088 | }; 3089 | } 3090 | return this; 3091 | } 3092 | /** Configures the {@link @microsoft/signalr.HubConnection} to use the specified Hub Protocol. 3093 | * 3094 | * @param {IHubProtocol} protocol The {@link @microsoft/signalr.IHubProtocol} implementation to use. 3095 | */ 3096 | withHubProtocol(protocol) { 3097 | Arg.isRequired(protocol, "protocol"); 3098 | this.protocol = protocol; 3099 | return this; 3100 | } 3101 | withAutomaticReconnect(retryDelaysOrReconnectPolicy) { 3102 | if (this.reconnectPolicy) { 3103 | throw new Error("A reconnectPolicy has already been set."); 3104 | } 3105 | if (!retryDelaysOrReconnectPolicy) { 3106 | this.reconnectPolicy = new DefaultReconnectPolicy(); 3107 | } 3108 | else if (Array.isArray(retryDelaysOrReconnectPolicy)) { 3109 | this.reconnectPolicy = new DefaultReconnectPolicy(retryDelaysOrReconnectPolicy); 3110 | } 3111 | else { 3112 | this.reconnectPolicy = retryDelaysOrReconnectPolicy; 3113 | } 3114 | return this; 3115 | } 3116 | /** Creates a {@link @microsoft/signalr.HubConnection} from the configuration options specified in this builder. 3117 | * 3118 | * @returns {HubConnection} The configured {@link @microsoft/signalr.HubConnection}. 3119 | */ 3120 | build() { 3121 | // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one 3122 | // provided to configureLogger 3123 | const httpConnectionOptions = this.httpConnectionOptions || {}; 3124 | // If it's 'null', the user **explicitly** asked for null, don't mess with it. 3125 | if (httpConnectionOptions.logger === undefined) { 3126 | // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it. 3127 | httpConnectionOptions.logger = this.logger; 3128 | } 3129 | // Now create the connection 3130 | if (!this.url) { 3131 | throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection."); 3132 | } 3133 | const connection = new HttpConnection(this.url, httpConnectionOptions); 3134 | return HubConnection.create(connection, this.logger || NullLogger.instance, this.protocol || new JsonHubProtocol(), this.reconnectPolicy); 3135 | } 3136 | } 3137 | function isLogger(logger) { 3138 | return logger.log !== undefined; 3139 | } 3140 | 3141 | ;// CONCATENATED MODULE: ./src/index.ts 3142 | // Licensed to the .NET Foundation under one or more agreements. 3143 | // The .NET Foundation licenses this file to you under the MIT license. 3144 | 3145 | 3146 | 3147 | 3148 | 3149 | 3150 | 3151 | 3152 | 3153 | 3154 | 3155 | 3156 | 3157 | ;// CONCATENATED MODULE: ./src/browser-index.ts 3158 | // Licensed to the .NET Foundation under one or more agreements. 3159 | // The .NET Foundation licenses this file to you under the MIT license. 3160 | // This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds. 3161 | // Copy from Array.prototype into Uint8Array to polyfill on IE. It's OK because the implementations of indexOf and slice use properties 3162 | // that exist on Uint8Array with the same name, and JavaScript is magic. 3163 | // We make them 'writable' because the Buffer polyfill messes with it as well. 3164 | if (!Uint8Array.prototype.indexOf) { 3165 | Object.defineProperty(Uint8Array.prototype, "indexOf", { 3166 | value: Array.prototype.indexOf, 3167 | writable: true, 3168 | }); 3169 | } 3170 | if (!Uint8Array.prototype.slice) { 3171 | Object.defineProperty(Uint8Array.prototype, "slice", { 3172 | // wrap the slice in Uint8Array so it looks like a Uint8Array.slice call 3173 | // eslint-disable-next-line object-shorthand 3174 | value: function (start, end) { return new Uint8Array(Array.prototype.slice.call(this, start, end)); }, 3175 | writable: true, 3176 | }); 3177 | } 3178 | if (!Uint8Array.prototype.forEach) { 3179 | Object.defineProperty(Uint8Array.prototype, "forEach", { 3180 | value: Array.prototype.forEach, 3181 | writable: true, 3182 | }); 3183 | } 3184 | 3185 | 3186 | /******/ return __webpack_exports__; 3187 | /******/ })() 3188 | ; 3189 | }); 3190 | //# sourceMappingURL=signalr.js.map -------------------------------------------------------------------------------- /Sample/wwwroot/microsoft-signalr/signalr.min.js: -------------------------------------------------------------------------------- 1 | var t,e;t=self,e=()=>(()=>{var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})}};t.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),t.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),t.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"t",{value:!0})};var e,s={};t.r(s),t.d(s,{AbortError:()=>r,DefaultHttpClient:()=>H,HttpClient:()=>d,HttpError:()=>i,HttpResponse:()=>u,HttpTransportType:()=>W,HubConnection:()=>N,HubConnectionBuilder:()=>Y,HubConnectionState:()=>A,JsonHubProtocol:()=>K,LogLevel:()=>e,MessageType:()=>R,NullLogger:()=>p,Subject:()=>U,TimeoutError:()=>n,TransferFormat:()=>O,VERSION:()=>f});class i extends Error{constructor(t,e){const s=new.target.prototype;super(`${t}: Status code '${e}'`),this.statusCode=e,this.__proto__=s}}class n extends Error{constructor(t="A timeout occurred."){const e=new.target.prototype;super(t),this.__proto__=e}}class r extends Error{constructor(t="An abort occurred."){const e=new.target.prototype;super(t),this.__proto__=e}}class o extends Error{constructor(t,e){const s=new.target.prototype;super(t),this.transport=e,this.errorType="UnsupportedTransportError",this.__proto__=s}}class h extends Error{constructor(t,e){const s=new.target.prototype;super(t),this.transport=e,this.errorType="DisabledTransportError",this.__proto__=s}}class c extends Error{constructor(t,e){const s=new.target.prototype;super(t),this.transport=e,this.errorType="FailedToStartTransportError",this.__proto__=s}}class a extends Error{constructor(t){const e=new.target.prototype;super(t),this.errorType="FailedToNegotiateWithServerError",this.__proto__=e}}class l extends Error{constructor(t,e){const s=new.target.prototype;super(t),this.innerErrors=e,this.__proto__=s}}class u{constructor(t,e,s){this.statusCode=t,this.statusText=e,this.content=s}}class d{get(t,e){return this.send({...e,method:"GET",url:t})}post(t,e){return this.send({...e,method:"POST",url:t})}delete(t,e){return this.send({...e,method:"DELETE",url:t})}getCookieString(t){return""}}!function(t){t[t.Trace=0]="Trace",t[t.Debug=1]="Debug",t[t.Information=2]="Information",t[t.Warning=3]="Warning",t[t.Error=4]="Error",t[t.Critical=5]="Critical",t[t.None=6]="None"}(e||(e={}));class p{constructor(){}log(t,e){}}p.instance=new p;const f="7.0.0";class w{static isRequired(t,e){if(null==t)throw new Error(`The '${e}' argument is required.`)}static isNotEmpty(t,e){if(!t||t.match(/^\s*$/))throw new Error(`The '${e}' argument should not be empty.`)}static isIn(t,e,s){if(!(t in e))throw new Error(`Unknown ${s} value: ${t}.`)}}class g{static get isBrowser(){return"object"==typeof window&&"object"==typeof window.document}static get isWebWorker(){return"object"==typeof self&&"importScripts"in self}static get isReactNative(){return"object"==typeof window&&void 0===window.document}static get isNode(){return!this.isBrowser&&!this.isWebWorker&&!this.isReactNative}}function m(t,e){let s="";return y(t)?(s=`Binary data of length ${t.byteLength}`,e&&(s+=`. Content: '${function(t){const e=new Uint8Array(t);let s="";return e.forEach((t=>{s+=`0x${t<16?"0":""}${t.toString(16)} `})),s.substr(0,s.length-1)}(t)}'`)):"string"==typeof t&&(s=`String data of length ${t.length}`,e&&(s+=`. Content: '${t}'`)),s}function y(t){return t&&"undefined"!=typeof ArrayBuffer&&(t instanceof ArrayBuffer||t.constructor&&"ArrayBuffer"===t.constructor.name)}async function b(t,s,i,n,r,o){const h={},[c,a]=E();h[c]=a,t.log(e.Trace,`(${s} transport) sending data. ${m(r,o.logMessageContent)}.`);const l=y(r)?"arraybuffer":"text",u=await i.post(n,{content:r,headers:{...h,...o.headers},responseType:l,timeout:o.timeout,withCredentials:o.withCredentials});t.log(e.Trace,`(${s} transport) request complete. Response status: ${u.statusCode}.`)}class v{constructor(t,e){this.i=t,this.h=e}dispose(){const t=this.i.observers.indexOf(this.h);t>-1&&this.i.observers.splice(t,1),0===this.i.observers.length&&this.i.cancelCallback&&this.i.cancelCallback().catch((t=>{}))}}class ${constructor(t){this.l=t,this.out=console}log(t,s){if(t>=this.l){const i=`[${(new Date).toISOString()}] ${e[t]}: ${s}`;switch(t){case e.Critical:case e.Error:this.out.error(i);break;case e.Warning:this.out.warn(i);break;case e.Information:this.out.info(i);break;default:this.out.log(i)}}}}function E(){let t="X-SignalR-User-Agent";return g.isNode&&(t="User-Agent"),[t,C(f,S(),g.isNode?"NodeJS":"Browser",k())]}function C(t,e,s,i){let n="Microsoft SignalR/";const r=t.split(".");return n+=`${r[0]}.${r[1]}`,n+=` (${t}; `,n+=e&&""!==e?`${e}; `:"Unknown OS; ",n+=`${s}`,n+=i?`; ${i}`:"; Unknown Runtime Version",n+=")",n}function S(){if(!g.isNode)return"";switch(process.platform){case"win32":return"Windows NT";case"darwin":return"macOS";case"linux":return"Linux";default:return process.platform}}function k(){if(g.isNode)return process.versions.node}function P(t){return t.stack?t.stack:t.message?t.message:`${t}`}class T extends d{constructor(e){if(super(),this.u=e,"undefined"==typeof fetch){const t=require;this.p=new(t("tough-cookie").CookieJar),this.m=t("node-fetch"),this.m=t("fetch-cookie")(this.m,this.p)}else this.m=fetch.bind(function(){if("undefined"!=typeof globalThis)return globalThis;if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if(void 0!==t.g)return t.g;throw new Error("could not find global")}());if("undefined"==typeof AbortController){const t=require;this.v=t("abort-controller")}else this.v=AbortController}async send(t){if(t.abortSignal&&t.abortSignal.aborted)throw new r;if(!t.method)throw new Error("No method defined.");if(!t.url)throw new Error("No url defined.");const s=new this.v;let o;t.abortSignal&&(t.abortSignal.onabort=()=>{s.abort(),o=new r});let h,c=null;if(t.timeout){const i=t.timeout;c=setTimeout((()=>{s.abort(),this.u.log(e.Warning,"Timeout from HTTP request."),o=new n}),i)}""===t.content&&(t.content=void 0),t.content&&(t.headers=t.headers||{},y(t.content)?t.headers["Content-Type"]="application/octet-stream":t.headers["Content-Type"]="text/plain;charset=UTF-8");try{h=await this.m(t.url,{body:t.content,cache:"no-cache",credentials:!0===t.withCredentials?"include":"same-origin",headers:{"X-Requested-With":"XMLHttpRequest",...t.headers},method:t.method,mode:"cors",redirect:"follow",signal:s.signal})}catch(t){if(o)throw o;throw this.u.log(e.Warning,`Error from HTTP request. ${t}.`),t}finally{c&&clearTimeout(c),t.abortSignal&&(t.abortSignal.onabort=null)}if(!h.ok){const t=await I(h,"text");throw new i(t||h.statusText,h.status)}const a=I(h,t.responseType),l=await a;return new u(h.status,h.statusText,l)}getCookieString(t){let e="";return g.isNode&&this.p&&this.p.getCookies(t,((t,s)=>e=s.join("; "))),e}}function I(t,e){let s;switch(e){case"arraybuffer":s=t.arrayBuffer();break;case"text":default:s=t.text();break;case"blob":case"document":case"json":throw new Error(`${e} is not supported.`)}return s}class _ extends d{constructor(t){super(),this.u=t}send(t){return t.abortSignal&&t.abortSignal.aborted?Promise.reject(new r):t.method?t.url?new Promise(((s,o)=>{const h=new XMLHttpRequest;h.open(t.method,t.url,!0),h.withCredentials=void 0===t.withCredentials||t.withCredentials,h.setRequestHeader("X-Requested-With","XMLHttpRequest"),""===t.content&&(t.content=void 0),t.content&&(y(t.content)?h.setRequestHeader("Content-Type","application/octet-stream"):h.setRequestHeader("Content-Type","text/plain;charset=UTF-8"));const c=t.headers;c&&Object.keys(c).forEach((t=>{h.setRequestHeader(t,c[t])})),t.responseType&&(h.responseType=t.responseType),t.abortSignal&&(t.abortSignal.onabort=()=>{h.abort(),o(new r)}),t.timeout&&(h.timeout=t.timeout),h.onload=()=>{t.abortSignal&&(t.abortSignal.onabort=null),h.status>=200&&h.status<300?s(new u(h.status,h.statusText,h.response||h.responseText)):o(new i(h.response||h.responseText||h.statusText,h.status))},h.onerror=()=>{this.u.log(e.Warning,`Error from HTTP request. ${h.status}: ${h.statusText}.`),o(new i(h.statusText,h.status))},h.ontimeout=()=>{this.u.log(e.Warning,"Timeout from HTTP request."),o(new n)},h.send(t.content)})):Promise.reject(new Error("No url defined.")):Promise.reject(new Error("No method defined."))}}class H extends d{constructor(t){if(super(),"undefined"!=typeof fetch||g.isNode)this.$=new T(t);else{if("undefined"==typeof XMLHttpRequest)throw new Error("No usable HttpClient found.");this.$=new _(t)}}send(t){return t.abortSignal&&t.abortSignal.aborted?Promise.reject(new r):t.method?t.url?this.$.send(t):Promise.reject(new Error("No url defined.")):Promise.reject(new Error("No method defined."))}getCookieString(t){return this.$.getCookieString(t)}}class D{static write(t){return`${t}${D.RecordSeparator}`}static parse(t){if(t[t.length-1]!==D.RecordSeparator)throw new Error("Message is incomplete.");const e=t.split(D.RecordSeparator);return e.pop(),e}}D.RecordSeparatorCode=30,D.RecordSeparator=String.fromCharCode(D.RecordSeparatorCode);class x{writeHandshakeRequest(t){return D.write(JSON.stringify(t))}parseHandshakeResponse(t){let e,s;if(y(t)){const i=new Uint8Array(t),n=i.indexOf(D.RecordSeparatorCode);if(-1===n)throw new Error("Message is incomplete.");const r=n+1;e=String.fromCharCode.apply(null,Array.prototype.slice.call(i.slice(0,r))),s=i.byteLength>r?i.slice(r).buffer:null}else{const i=t,n=i.indexOf(D.RecordSeparator);if(-1===n)throw new Error("Message is incomplete.");const r=n+1;e=i.substring(0,r),s=i.length>r?i.substring(r):null}const i=D.parse(e),n=JSON.parse(i[0]);if(n.type)throw new Error("Expected a handshake response from the server.");return[s,n]}}var R,A;!function(t){t[t.Invocation=1]="Invocation",t[t.StreamItem=2]="StreamItem",t[t.Completion=3]="Completion",t[t.StreamInvocation=4]="StreamInvocation",t[t.CancelInvocation=5]="CancelInvocation",t[t.Ping=6]="Ping",t[t.Close=7]="Close"}(R||(R={}));class U{constructor(){this.observers=[]}next(t){for(const e of this.observers)e.next(t)}error(t){for(const e of this.observers)e.error&&e.error(t)}complete(){for(const t of this.observers)t.complete&&t.complete()}subscribe(t){return this.observers.push(t),new v(this,t)}}!function(t){t.Disconnected="Disconnected",t.Connecting="Connecting",t.Connected="Connected",t.Disconnecting="Disconnecting",t.Reconnecting="Reconnecting"}(A||(A={}));class N{constructor(t,s,i,n){this.C=0,this.S=()=>{this.u.log(e.Warning,"The page is being frozen, this will likely lead to the connection being closed and messages being lost. For more information see the docs at https://docs.microsoft.com/aspnet/core/signalr/javascript-client#bsleep")},w.isRequired(t,"connection"),w.isRequired(s,"logger"),w.isRequired(i,"protocol"),this.serverTimeoutInMilliseconds=3e4,this.keepAliveIntervalInMilliseconds=15e3,this.u=s,this.k=i,this.connection=t,this.P=n,this.T=new x,this.connection.onreceive=t=>this.I(t),this.connection.onclose=t=>this._(t),this.H={},this.D={},this.R=[],this.A=[],this.U=[],this.N=0,this.L=!1,this.M=A.Disconnected,this.j=!1,this.q=this.k.writeMessage({type:R.Ping})}static create(t,e,s,i){return new N(t,e,s,i)}get state(){return this.M}get connectionId(){return this.connection&&this.connection.connectionId||null}get baseUrl(){return this.connection.baseUrl||""}set baseUrl(t){if(this.M!==A.Disconnected&&this.M!==A.Reconnecting)throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");if(!t)throw new Error("The HubConnection url must be a valid url.");this.connection.baseUrl=t}start(){return this.W=this.O(),this.W}async O(){if(this.M!==A.Disconnected)return Promise.reject(new Error("Cannot start a HubConnection that is not in the 'Disconnected' state."));this.M=A.Connecting,this.u.log(e.Debug,"Starting HubConnection.");try{await this.F(),g.isBrowser&&window.document.addEventListener("freeze",this.S),this.M=A.Connected,this.j=!0,this.u.log(e.Debug,"HubConnection connected successfully.")}catch(t){return this.M=A.Disconnected,this.u.log(e.Debug,`HubConnection failed to start successfully because of error '${t}'.`),Promise.reject(t)}}async F(){this.B=void 0,this.L=!1;const t=new Promise(((t,e)=>{this.X=t,this.J=e}));await this.connection.start(this.k.transferFormat);try{const s={protocol:this.k.name,version:this.k.version};if(this.u.log(e.Debug,"Sending handshake request."),await this.V(this.T.writeHandshakeRequest(s)),this.u.log(e.Information,`Using HubProtocol '${this.k.name}'.`),this.G(),this.K(),this.Y(),await t,this.B)throw this.B;this.connection.features.inherentKeepAlive||await this.V(this.q)}catch(t){throw this.u.log(e.Debug,`Hub handshake failed with error '${t}' during start(). Stopping HubConnection.`),this.G(),this.Z(),await this.connection.stop(t),t}}async stop(){const t=this.W;this.tt=this.et(),await this.tt;try{await t}catch(t){}}et(t){return this.M===A.Disconnected?(this.u.log(e.Debug,`Call to HubConnection.stop(${t}) ignored because it is already in the disconnected state.`),Promise.resolve()):this.M===A.Disconnecting?(this.u.log(e.Debug,`Call to HttpConnection.stop(${t}) ignored because the connection is already in the disconnecting state.`),this.tt):(this.M=A.Disconnecting,this.u.log(e.Debug,"Stopping HubConnection."),this.st?(this.u.log(e.Debug,"Connection stopped during reconnect delay. Done reconnecting."),clearTimeout(this.st),this.st=void 0,this.it(),Promise.resolve()):(this.G(),this.Z(),this.B=t||new r("The connection was stopped before the hub handshake could complete."),this.connection.stop(t)))}stream(t,...e){const[s,i]=this.nt(e),n=this.rt(t,e,i);let r;const o=new U;return o.cancelCallback=()=>{const t=this.ot(n.invocationId);return delete this.H[n.invocationId],r.then((()=>this.ht(t)))},this.H[n.invocationId]=(t,e)=>{e?o.error(e):t&&(t.type===R.Completion?t.error?o.error(new Error(t.error)):o.complete():o.next(t.item))},r=this.ht(n).catch((t=>{o.error(t),delete this.H[n.invocationId]})),this.ct(s,r),o}V(t){return this.Y(),this.connection.send(t)}ht(t){return this.V(this.k.writeMessage(t))}send(t,...e){const[s,i]=this.nt(e),n=this.ht(this.lt(t,e,!0,i));return this.ct(s,n),n}invoke(t,...e){const[s,i]=this.nt(e),n=this.lt(t,e,!1,i);return new Promise(((t,e)=>{this.H[n.invocationId]=(s,i)=>{i?e(i):s&&(s.type===R.Completion?s.error?e(new Error(s.error)):t(s.result):e(new Error(`Unexpected message type: ${s.type}`)))};const i=this.ht(n).catch((t=>{e(t),delete this.H[n.invocationId]}));this.ct(s,i)}))}on(t,e){t&&e&&(t=t.toLowerCase(),this.D[t]||(this.D[t]=[]),-1===this.D[t].indexOf(e)&&this.D[t].push(e))}off(t,e){if(!t)return;t=t.toLowerCase();const s=this.D[t];if(s)if(e){const i=s.indexOf(e);-1!==i&&(s.splice(i,1),0===s.length&&delete this.D[t])}else delete this.D[t]}onclose(t){t&&this.R.push(t)}onreconnecting(t){t&&this.A.push(t)}onreconnected(t){t&&this.U.push(t)}I(t){if(this.G(),this.L||(t=this.ut(t),this.L=!0),t){const s=this.k.parseMessages(t,this.u);for(const t of s)switch(t.type){case R.Invocation:this.dt(t);break;case R.StreamItem:case R.Completion:{const s=this.H[t.invocationId];if(s){t.type===R.Completion&&delete this.H[t.invocationId];try{s(t)}catch(t){this.u.log(e.Error,`Stream callback threw error: ${P(t)}`)}}break}case R.Ping:break;case R.Close:{this.u.log(e.Information,"Close message received from server.");const s=t.error?new Error("Server returned an error on close: "+t.error):void 0;!0===t.allowReconnect?this.connection.stop(s):this.tt=this.et(s);break}default:this.u.log(e.Warning,`Invalid message type: ${t.type}.`)}}this.K()}ut(t){let s,i;try{[i,s]=this.T.parseHandshakeResponse(t)}catch(t){const s="Error parsing handshake response: "+t;this.u.log(e.Error,s);const i=new Error(s);throw this.J(i),i}if(s.error){const t="Server returned handshake error: "+s.error;this.u.log(e.Error,t);const i=new Error(t);throw this.J(i),i}return this.u.log(e.Debug,"Server handshake complete."),this.X(),i}Y(){this.connection.features.inherentKeepAlive||(this.C=(new Date).getTime()+this.keepAliveIntervalInMilliseconds,this.Z())}K(){if(!(this.connection.features&&this.connection.features.inherentKeepAlive||(this.ft=setTimeout((()=>this.serverTimeout()),this.serverTimeoutInMilliseconds),void 0!==this.wt))){let t=this.C-(new Date).getTime();t<0&&(t=0),this.wt=setTimeout((async()=>{if(this.M===A.Connected)try{await this.V(this.q)}catch{this.Z()}}),t)}}serverTimeout(){this.connection.stop(new Error("Server timeout elapsed without receiving a message from the server."))}async dt(t){const s=t.target.toLowerCase(),i=this.D[s];if(!i)return this.u.log(e.Warning,`No client method with the name '${s}' found.`),void(t.invocationId&&(this.u.log(e.Warning,`No result given for '${s}' method and invocation ID '${t.invocationId}'.`),await this.ht(this.gt(t.invocationId,"Client didn't provide a result.",null))));const n=i.slice(),r=!!t.invocationId;let o,h,c;for(const i of n)try{const n=o;o=await i.apply(this,t.arguments),r&&o&&n&&(this.u.log(e.Error,`Multiple results provided for '${s}'. Sending error to server.`),c=this.gt(t.invocationId,"Client provided multiple results.",null)),h=void 0}catch(t){h=t,this.u.log(e.Error,`A callback for the method '${s}' threw error '${t}'.`)}c?await this.ht(c):r?(h?c=this.gt(t.invocationId,`${h}`,null):void 0!==o?c=this.gt(t.invocationId,null,o):(this.u.log(e.Warning,`No result given for '${s}' method and invocation ID '${t.invocationId}'.`),c=this.gt(t.invocationId,"Client didn't provide a result.",null)),await this.ht(c)):o&&this.u.log(e.Error,`Result given for '${s}' method but server is not expecting a result.`)}_(t){this.u.log(e.Debug,`HubConnection.connectionClosed(${t}) called while in state ${this.M}.`),this.B=this.B||t||new r("The underlying connection was closed before the hub handshake could complete."),this.X&&this.X(),this.yt(t||new Error("Invocation canceled due to the underlying connection being closed.")),this.G(),this.Z(),this.M===A.Disconnecting?this.it(t):this.M===A.Connected&&this.P?this.bt(t):this.M===A.Connected&&this.it(t)}it(t){if(this.j){this.M=A.Disconnected,this.j=!1,g.isBrowser&&window.document.removeEventListener("freeze",this.S);try{this.R.forEach((e=>e.apply(this,[t])))}catch(s){this.u.log(e.Error,`An onclose callback called with error '${t}' threw error '${s}'.`)}}}async bt(t){const s=Date.now();let i=0,n=void 0!==t?t:new Error("Attempting to reconnect due to a unknown error."),r=this.vt(i++,0,n);if(null===r)return this.u.log(e.Debug,"Connection not reconnecting because the IRetryPolicy returned null on the first reconnect attempt."),void this.it(t);if(this.M=A.Reconnecting,t?this.u.log(e.Information,`Connection reconnecting because of error '${t}'.`):this.u.log(e.Information,"Connection reconnecting."),0!==this.A.length){try{this.A.forEach((e=>e.apply(this,[t])))}catch(s){this.u.log(e.Error,`An onreconnecting callback called with error '${t}' threw error '${s}'.`)}if(this.M!==A.Reconnecting)return void this.u.log(e.Debug,"Connection left the reconnecting state in onreconnecting callback. Done reconnecting.")}for(;null!==r;){if(this.u.log(e.Information,`Reconnect attempt number ${i} will start in ${r} ms.`),await new Promise((t=>{this.st=setTimeout(t,r)})),this.st=void 0,this.M!==A.Reconnecting)return void this.u.log(e.Debug,"Connection left the reconnecting state during reconnect delay. Done reconnecting.");try{if(await this.F(),this.M=A.Connected,this.u.log(e.Information,"HubConnection reconnected successfully."),0!==this.U.length)try{this.U.forEach((t=>t.apply(this,[this.connection.connectionId])))}catch(t){this.u.log(e.Error,`An onreconnected callback called with connectionId '${this.connection.connectionId}; threw error '${t}'.`)}return}catch(t){if(this.u.log(e.Information,`Reconnect attempt failed because of error '${t}'.`),this.M!==A.Reconnecting)return this.u.log(e.Debug,`Connection moved to the '${this.M}' from the reconnecting state during reconnect attempt. Done reconnecting.`),void(this.M===A.Disconnecting&&this.it());n=t instanceof Error?t:new Error(t.toString()),r=this.vt(i++,Date.now()-s,n)}}this.u.log(e.Information,`Reconnect retries have been exhausted after ${Date.now()-s} ms and ${i} failed attempts. Connection disconnecting.`),this.it()}vt(t,s,i){try{return this.P.nextRetryDelayInMilliseconds({elapsedMilliseconds:s,previousRetryCount:t,retryReason:i})}catch(i){return this.u.log(e.Error,`IRetryPolicy.nextRetryDelayInMilliseconds(${t}, ${s}) threw error '${i}'.`),null}}yt(t){const s=this.H;this.H={},Object.keys(s).forEach((i=>{const n=s[i];try{n(null,t)}catch(s){this.u.log(e.Error,`Stream 'error' callback called with '${t}' threw error: ${P(s)}`)}}))}Z(){this.wt&&(clearTimeout(this.wt),this.wt=void 0)}G(){this.ft&&clearTimeout(this.ft)}lt(t,e,s,i){if(s)return 0!==i.length?{arguments:e,streamIds:i,target:t,type:R.Invocation}:{arguments:e,target:t,type:R.Invocation};{const s=this.N;return this.N++,0!==i.length?{arguments:e,invocationId:s.toString(),streamIds:i,target:t,type:R.Invocation}:{arguments:e,invocationId:s.toString(),target:t,type:R.Invocation}}}ct(t,e){if(0!==t.length){e||(e=Promise.resolve());for(const s in t)t[s].subscribe({complete:()=>{e=e.then((()=>this.ht(this.gt(s))))},error:t=>{let i;i=t instanceof Error?t.message:t&&t.toString?t.toString():"Unknown error",e=e.then((()=>this.ht(this.gt(s,i))))},next:t=>{e=e.then((()=>this.ht(this.$t(s,t))))}})}}nt(t){const e=[],s=[];for(let i=0;i0)&&(e=!1,this.Pt=await this.kt()),this.Tt(t);const s=await this.St.send(t);return e&&401===s.statusCode&&this.kt?(this.Pt=await this.kt(),this.Tt(t),await this.St.send(t)):s}Tt(t){t.headers||(t.headers={}),this.Pt?t.headers[j.Authorization]=`Bearer ${this.Pt}`:this.kt&&t.headers[j.Authorization]&&delete t.headers[j.Authorization]}getCookieString(t){return this.St.getCookieString(t)}}var W,O;!function(t){t[t.None=0]="None",t[t.WebSockets=1]="WebSockets",t[t.ServerSentEvents=2]="ServerSentEvents",t[t.LongPolling=4]="LongPolling"}(W||(W={})),function(t){t[t.Text=1]="Text",t[t.Binary=2]="Binary"}(O||(O={}));class F{constructor(){this.It=!1,this.onabort=null}abort(){this.It||(this.It=!0,this.onabort&&this.onabort())}get signal(){return this}get aborted(){return this.It}}class B{constructor(t,e,s){this.$=t,this.u=e,this._t=new F,this.Ht=s,this.Dt=!1,this.onreceive=null,this.onclose=null}get pollAborted(){return this._t.aborted}async connect(t,s){if(w.isRequired(t,"url"),w.isRequired(s,"transferFormat"),w.isIn(s,O,"transferFormat"),this.xt=t,this.u.log(e.Trace,"(LongPolling transport) Connecting."),s===O.Binary&&"undefined"!=typeof XMLHttpRequest&&"string"!=typeof(new XMLHttpRequest).responseType)throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported.");const[n,r]=E(),o={[n]:r,...this.Ht.headers},h={abortSignal:this._t.signal,headers:o,timeout:1e5,withCredentials:this.Ht.withCredentials};s===O.Binary&&(h.responseType="arraybuffer");const c=`${t}&_=${Date.now()}`;this.u.log(e.Trace,`(LongPolling transport) polling: ${c}.`);const a=await this.$.get(c,h);200!==a.statusCode?(this.u.log(e.Error,`(LongPolling transport) Unexpected response code: ${a.statusCode}.`),this.Rt=new i(a.statusText||"",a.statusCode),this.Dt=!1):this.Dt=!0,this.At=this.Ut(this.xt,h)}async Ut(t,s){try{for(;this.Dt;)try{const n=`${t}&_=${Date.now()}`;this.u.log(e.Trace,`(LongPolling transport) polling: ${n}.`);const r=await this.$.get(n,s);204===r.statusCode?(this.u.log(e.Information,"(LongPolling transport) Poll terminated by server."),this.Dt=!1):200!==r.statusCode?(this.u.log(e.Error,`(LongPolling transport) Unexpected response code: ${r.statusCode}.`),this.Rt=new i(r.statusText||"",r.statusCode),this.Dt=!1):r.content?(this.u.log(e.Trace,`(LongPolling transport) data received. ${m(r.content,this.Ht.logMessageContent)}.`),this.onreceive&&this.onreceive(r.content)):this.u.log(e.Trace,"(LongPolling transport) Poll timed out, reissuing.")}catch(t){this.Dt?t instanceof n?this.u.log(e.Trace,"(LongPolling transport) Poll timed out, reissuing."):(this.Rt=t,this.Dt=!1):this.u.log(e.Trace,`(LongPolling transport) Poll errored after shutdown: ${t.message}`)}}finally{this.u.log(e.Trace,"(LongPolling transport) Polling complete."),this.pollAborted||this.Nt()}}async send(t){return this.Dt?b(this.u,"LongPolling",this.$,this.xt,t,this.Ht):Promise.reject(new Error("Cannot send until the transport is connected"))}async stop(){this.u.log(e.Trace,"(LongPolling transport) Stopping polling."),this.Dt=!1,this._t.abort();try{await this.At,this.u.log(e.Trace,`(LongPolling transport) sending DELETE request to ${this.xt}.`);const t={},[s,i]=E();t[s]=i;const n={headers:{...t,...this.Ht.headers},timeout:this.Ht.timeout,withCredentials:this.Ht.withCredentials};await this.$.delete(this.xt,n),this.u.log(e.Trace,"(LongPolling transport) DELETE request sent.")}finally{this.u.log(e.Trace,"(LongPolling transport) Stop finished."),this.Nt()}}Nt(){if(this.onclose){let t="(LongPolling transport) Firing onclose event.";this.Rt&&(t+=" Error: "+this.Rt),this.u.log(e.Trace,t),this.onclose(this.Rt)}}}class X{constructor(t,e,s,i){this.$=t,this.Pt=e,this.u=s,this.Ht=i,this.onreceive=null,this.onclose=null}async connect(t,s){return w.isRequired(t,"url"),w.isRequired(s,"transferFormat"),w.isIn(s,O,"transferFormat"),this.u.log(e.Trace,"(SSE transport) Connecting."),this.xt=t,this.Pt&&(t+=(t.indexOf("?")<0?"?":"&")+`access_token=${encodeURIComponent(this.Pt)}`),new Promise(((i,n)=>{let r,o=!1;if(s===O.Text){if(g.isBrowser||g.isWebWorker)r=new this.Ht.EventSource(t,{withCredentials:this.Ht.withCredentials});else{const e=this.$.getCookieString(t),s={};s.Cookie=e;const[i,n]=E();s[i]=n,r=new this.Ht.EventSource(t,{withCredentials:this.Ht.withCredentials,headers:{...s,...this.Ht.headers}})}try{r.onmessage=t=>{if(this.onreceive)try{this.u.log(e.Trace,`(SSE transport) data received. ${m(t.data,this.Ht.logMessageContent)}.`),this.onreceive(t.data)}catch(t){return void this.Lt(t)}},r.onerror=t=>{o?this.Lt():n(new Error("EventSource failed to connect. The connection could not be found on the server, either the connection ID is not present on the server, or a proxy is refusing/buffering the connection. If you have multiple servers check that sticky sessions are enabled."))},r.onopen=()=>{this.u.log(e.Information,`SSE connected to ${this.xt}`),this.Mt=r,o=!0,i()}}catch(t){return void n(t)}}else n(new Error("The Server-Sent Events transport only supports the 'Text' transfer format"))}))}async send(t){return this.Mt?b(this.u,"SSE",this.$,this.xt,t,this.Ht):Promise.reject(new Error("Cannot send until the transport is connected"))}stop(){return this.Lt(),Promise.resolve()}Lt(t){this.Mt&&(this.Mt.close(),this.Mt=void 0,this.onclose&&this.onclose(t))}}class J{constructor(t,e,s,i,n,r){this.u=s,this.kt=e,this.jt=i,this.qt=n,this.$=t,this.onreceive=null,this.onclose=null,this.Wt=r}async connect(t,s){let i;return w.isRequired(t,"url"),w.isRequired(s,"transferFormat"),w.isIn(s,O,"transferFormat"),this.u.log(e.Trace,"(WebSockets transport) Connecting."),this.kt&&(i=await this.kt()),new Promise(((n,r)=>{let o;t=t.replace(/^http/,"ws");const h=this.$.getCookieString(t);let c=!1;if(g.isNode||g.isReactNative){const e={},[s,n]=E();e[s]=n,i&&(e[j.Authorization]=`Bearer ${i}`),h&&(e[j.Cookie]=h),o=new this.qt(t,void 0,{headers:{...e,...this.Wt}})}else i&&(t+=(t.indexOf("?")<0?"?":"&")+`access_token=${encodeURIComponent(i)}`);o||(o=new this.qt(t)),s===O.Binary&&(o.binaryType="arraybuffer"),o.onopen=s=>{this.u.log(e.Information,`WebSocket connected to ${t}.`),this.Ot=o,c=!0,n()},o.onerror=t=>{let s=null;s="undefined"!=typeof ErrorEvent&&t instanceof ErrorEvent?t.error:"There was an error with the transport",this.u.log(e.Information,`(WebSockets transport) ${s}.`)},o.onmessage=t=>{if(this.u.log(e.Trace,`(WebSockets transport) data received. ${m(t.data,this.jt)}.`),this.onreceive)try{this.onreceive(t.data)}catch(t){return void this.Lt(t)}},o.onclose=t=>{if(c)this.Lt(t);else{let e=null;e="undefined"!=typeof ErrorEvent&&t instanceof ErrorEvent?t.error:"WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.",r(new Error(e))}}}))}send(t){return this.Ot&&this.Ot.readyState===this.qt.OPEN?(this.u.log(e.Trace,`(WebSockets transport) sending data. ${m(t,this.jt)}.`),this.Ot.send(t),Promise.resolve()):Promise.reject("WebSocket is not in the OPEN state")}stop(){return this.Ot&&this.Lt(void 0),Promise.resolve()}Lt(t){this.Ot&&(this.Ot.onclose=()=>{},this.Ot.onmessage=()=>{},this.Ot.onerror=()=>{},this.Ot.close(),this.Ot=void 0),this.u.log(e.Trace,"(WebSockets transport) socket closed."),this.onclose&&(!this.Ft(t)||!1!==t.wasClean&&1e3===t.code?t instanceof Error?this.onclose(t):this.onclose():this.onclose(new Error(`WebSocket closed with status code: ${t.code} (${t.reason||"no reason given"}).`)))}Ft(t){return t&&"boolean"==typeof t.wasClean&&"number"==typeof t.code}}class z{constructor(t,s={}){var i;if(this.Bt=()=>{},this.features={},this.Xt=1,w.isRequired(t,"url"),this.u=void 0===(i=s.logger)?new $(e.Information):null===i?p.instance:void 0!==i.log?i:new $(i),this.baseUrl=this.Jt(t),(s=s||{}).logMessageContent=void 0!==s.logMessageContent&&s.logMessageContent,"boolean"!=typeof s.withCredentials&&void 0!==s.withCredentials)throw new Error("withCredentials option was not a 'boolean' or 'undefined' value");s.withCredentials=void 0===s.withCredentials||s.withCredentials,s.timeout=void 0===s.timeout?1e5:s.timeout;let n=null,r=null;if(g.isNode){const t=require;n=t("ws"),r=t("eventsource")}g.isNode||"undefined"==typeof WebSocket||s.WebSocket?g.isNode&&!s.WebSocket&&n&&(s.WebSocket=n):s.WebSocket=WebSocket,g.isNode||"undefined"==typeof EventSource||s.EventSource?g.isNode&&!s.EventSource&&void 0!==r&&(s.EventSource=r):s.EventSource=EventSource,this.$=new q(s.httpClient||new H(this.u),s.accessTokenFactory),this.M="Disconnected",this.j=!1,this.Ht=s,this.onreceive=null,this.onclose=null}async start(t){if(t=t||O.Binary,w.isIn(t,O,"transferFormat"),this.u.log(e.Debug,`Starting connection with transfer format '${O[t]}'.`),"Disconnected"!==this.M)return Promise.reject(new Error("Cannot start an HttpConnection that is not in the 'Disconnected' state."));if(this.M="Connecting",this.zt=this.F(t),await this.zt,"Disconnecting"===this.M){const t="Failed to start the HttpConnection before stop() was called.";return this.u.log(e.Error,t),await this.tt,Promise.reject(new r(t))}if("Connected"!==this.M){const t="HttpConnection.startInternal completed gracefully but didn't enter the connection into the connected state!";return this.u.log(e.Error,t),Promise.reject(new r(t))}this.j=!0}send(t){return"Connected"!==this.M?Promise.reject(new Error("Cannot send data if the connection is not in the 'Connected' State.")):(this.Vt||(this.Vt=new V(this.transport)),this.Vt.send(t))}async stop(t){return"Disconnected"===this.M?(this.u.log(e.Debug,`Call to HttpConnection.stop(${t}) ignored because the connection is already in the disconnected state.`),Promise.resolve()):"Disconnecting"===this.M?(this.u.log(e.Debug,`Call to HttpConnection.stop(${t}) ignored because the connection is already in the disconnecting state.`),this.tt):(this.M="Disconnecting",this.tt=new Promise((t=>{this.Bt=t})),await this.et(t),void await this.tt)}async et(t){this.Gt=t;try{await this.zt}catch(t){}if(this.transport){try{await this.transport.stop()}catch(t){this.u.log(e.Error,`HttpConnection.transport.stop() threw error '${t}'.`),this.Kt()}this.transport=void 0}else this.u.log(e.Debug,"HttpConnection.transport is undefined in HttpConnection.stop() because start() failed.")}async F(t){let s=this.baseUrl;this.kt=this.Ht.accessTokenFactory,this.$.kt=this.kt;try{if(this.Ht.skipNegotiation){if(this.Ht.transport!==W.WebSockets)throw new Error("Negotiation can only be skipped when using the WebSocket transport directly.");this.transport=this.Qt(W.WebSockets),await this.Yt(s,t)}else{let e=null,i=0;do{if(e=await this.Zt(s),"Disconnecting"===this.M||"Disconnected"===this.M)throw new r("The connection was stopped during negotiation.");if(e.error)throw new Error(e.error);if(e.ProtocolVersion)throw new Error("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.");if(e.url&&(s=e.url),e.accessToken){const t=e.accessToken;this.kt=()=>t,this.$.Pt=t,this.$.kt=void 0}i++}while(e.url&&i<100);if(100===i&&e.url)throw new Error("Negotiate redirection limit exceeded.");await this.te(s,this.Ht.transport,e,t)}this.transport instanceof B&&(this.features.inherentKeepAlive=!0),"Connecting"===this.M&&(this.u.log(e.Debug,"The HttpConnection connected successfully."),this.M="Connected")}catch(t){return this.u.log(e.Error,"Failed to start the connection: "+t),this.M="Disconnected",this.transport=void 0,this.Bt(),Promise.reject(t)}}async Zt(t){const s={},[n,r]=E();s[n]=r;const o=this.ee(t);this.u.log(e.Debug,`Sending negotiation request: ${o}.`);try{const t=await this.$.post(o,{content:"",headers:{...s,...this.Ht.headers},timeout:this.Ht.timeout,withCredentials:this.Ht.withCredentials});if(200!==t.statusCode)return Promise.reject(new Error(`Unexpected status code returned from negotiate '${t.statusCode}'`));const e=JSON.parse(t.content);return(!e.negotiateVersion||e.negotiateVersion<1)&&(e.connectionToken=e.connectionId),e}catch(t){let s="Failed to complete negotiation with the server: "+t;return t instanceof i&&404===t.statusCode&&(s+=" Either this is not a SignalR endpoint or there is a proxy blocking the connection."),this.u.log(e.Error,s),Promise.reject(new a(s))}}se(t,e){return e?t+(-1===t.indexOf("?")?"?":"&")+`id=${e}`:t}async te(t,s,i,n){let o=this.se(t,i.connectionToken);if(this.ie(s))return this.u.log(e.Debug,"Connection was provided an instance of ITransport, using that directly."),this.transport=s,await this.Yt(o,n),void(this.connectionId=i.connectionId);const h=[],a=i.availableTransports||[];let u=i;for(const i of a){const a=this.ne(i,s,n);if(a instanceof Error)h.push(`${i.transport} failed:`),h.push(a);else if(this.ie(a)){if(this.transport=a,!u){try{u=await this.Zt(t)}catch(t){return Promise.reject(t)}o=this.se(t,u.connectionToken)}try{return await this.Yt(o,n),void(this.connectionId=u.connectionId)}catch(t){if(this.u.log(e.Error,`Failed to start the transport '${i.transport}': ${t}`),u=void 0,h.push(new c(`${i.transport} failed: ${t}`,W[i.transport])),"Connecting"!==this.M){const t="Failed to select transport before stop() was called.";return this.u.log(e.Debug,t),Promise.reject(new r(t))}}}}return h.length>0?Promise.reject(new l(`Unable to connect to the server with any of the available transports. ${h.join(" ")}`,h)):Promise.reject(new Error("None of the transports supported by the client are supported by the server."))}Qt(t){switch(t){case W.WebSockets:if(!this.Ht.WebSocket)throw new Error("'WebSocket' is not supported in your environment.");return new J(this.$,this.kt,this.u,this.Ht.logMessageContent,this.Ht.WebSocket,this.Ht.headers||{});case W.ServerSentEvents:if(!this.Ht.EventSource)throw new Error("'EventSource' is not supported in your environment.");return new X(this.$,this.$.Pt,this.u,this.Ht);case W.LongPolling:return new B(this.$,this.u,this.Ht);default:throw new Error(`Unknown transport: ${t}.`)}}Yt(t,e){return this.transport.onreceive=this.onreceive,this.transport.onclose=t=>this.Kt(t),this.transport.connect(t,e)}ne(t,s,i){const n=W[t.transport];if(null==n)return this.u.log(e.Debug,`Skipping transport '${t.transport}' because it is not supported by this client.`),new Error(`Skipping transport '${t.transport}' because it is not supported by this client.`);if(!function(t,e){return!t||0!=(e&t)}(s,n))return this.u.log(e.Debug,`Skipping transport '${W[n]}' because it was disabled by the client.`),new h(`'${W[n]}' is disabled by the client.`,n);if(!(t.transferFormats.map((t=>O[t])).indexOf(i)>=0))return this.u.log(e.Debug,`Skipping transport '${W[n]}' because it does not support the requested transfer format '${O[i]}'.`),new Error(`'${W[n]}' does not support ${O[i]}.`);if(n===W.WebSockets&&!this.Ht.WebSocket||n===W.ServerSentEvents&&!this.Ht.EventSource)return this.u.log(e.Debug,`Skipping transport '${W[n]}' because it is not supported in your environment.'`),new o(`'${W[n]}' is not supported in your environment.`,n);this.u.log(e.Debug,`Selecting transport '${W[n]}'.`);try{return this.Qt(n)}catch(t){return t}}ie(t){return t&&"object"==typeof t&&"connect"in t}Kt(t){if(this.u.log(e.Debug,`HttpConnection.stopConnection(${t}) called while in state ${this.M}.`),this.transport=void 0,t=this.Gt||t,this.Gt=void 0,"Disconnected"!==this.M){if("Connecting"===this.M)throw this.u.log(e.Warning,`Call to HttpConnection.stopConnection(${t}) was ignored because the connection is still in the connecting state.`),new Error(`HttpConnection.stopConnection(${t}) was called while the connection is still in the connecting state.`);if("Disconnecting"===this.M&&this.Bt(),t?this.u.log(e.Error,`Connection disconnected with error '${t}'.`):this.u.log(e.Information,"Connection disconnected."),this.Vt&&(this.Vt.stop().catch((t=>{this.u.log(e.Error,`TransportSendQueue.stop() threw error '${t}'.`)})),this.Vt=void 0),this.connectionId=void 0,this.M="Disconnected",this.j){this.j=!1;try{this.onclose&&this.onclose(t)}catch(s){this.u.log(e.Error,`HttpConnection.onclose(${t}) threw error '${s}'.`)}}}else this.u.log(e.Debug,`Call to HttpConnection.stopConnection(${t}) was ignored because the connection is already in the disconnected state.`)}Jt(t){if(0===t.lastIndexOf("https://",0)||0===t.lastIndexOf("http://",0))return t;if(!g.isBrowser)throw new Error(`Cannot resolve '${t}'.`);const s=window.document.createElement("a");return s.href=t,this.u.log(e.Information,`Normalizing '${t}' to '${s.href}'.`),s.href}ee(t){const e=t.indexOf("?");let s=t.substring(0,-1===e?t.length:e);return"/"!==s[s.length-1]&&(s+="/"),s+="negotiate",s+=-1===e?"":t.substring(e),-1===s.indexOf("negotiateVersion")&&(s+=-1===e?"?":"&",s+="negotiateVersion="+this.Xt),s}}class V{constructor(t){this.re=t,this.oe=[],this.he=!0,this.ce=new G,this.ae=new G,this.le=this.ue()}send(t){return this.de(t),this.ae||(this.ae=new G),this.ae.promise}stop(){return this.he=!1,this.ce.resolve(),this.le}de(t){if(this.oe.length&&typeof this.oe[0]!=typeof t)throw new Error(`Expected data to be of type ${typeof this.oe} but was of type ${typeof t}`);this.oe.push(t),this.ce.resolve()}async ue(){for(;;){if(await this.ce.promise,!this.he){this.ae&&this.ae.reject("Connection stopped.");break}this.ce=new G;const t=this.ae;this.ae=void 0;const e="string"==typeof this.oe[0]?this.oe.join(""):V.pe(this.oe);this.oe.length=0;try{await this.re.send(e),t.resolve()}catch(e){t.reject(e)}}}static pe(t){const e=t.map((t=>t.byteLength)).reduce(((t,e)=>t+e)),s=new Uint8Array(e);let i=0;for(const e of t)s.set(new Uint8Array(e),i),i+=e.byteLength;return s.buffer}}class G{constructor(){this.promise=new Promise(((t,e)=>[this.fe,this.we]=[t,e]))}resolve(){this.fe()}reject(t){this.we(t)}}class K{constructor(){this.name="json",this.version=1,this.transferFormat=O.Text}parseMessages(t,s){if("string"!=typeof t)throw new Error("Invalid input for JSON hub protocol. Expected a string.");if(!t)return[];null===s&&(s=p.instance);const i=D.parse(t),n=[];for(const t of i){const i=JSON.parse(t);if("number"!=typeof i.type)throw new Error("Invalid payload.");switch(i.type){case R.Invocation:this.ge(i);break;case R.StreamItem:this.me(i);break;case R.Completion:this.ye(i);break;case R.Ping:case R.Close:break;default:s.log(e.Information,"Unknown message type '"+i.type+"' ignored.");continue}n.push(i)}return n}writeMessage(t){return D.write(JSON.stringify(t))}ge(t){this.be(t.target,"Invalid payload for Invocation message."),void 0!==t.invocationId&&this.be(t.invocationId,"Invalid payload for Invocation message.")}me(t){if(this.be(t.invocationId,"Invalid payload for StreamItem message."),void 0===t.item)throw new Error("Invalid payload for StreamItem message.")}ye(t){if(t.result&&t.error)throw new Error("Invalid payload for Completion message.");!t.result&&t.error&&this.be(t.error,"Invalid payload for Completion message."),this.be(t.invocationId,"Invalid payload for Completion message.")}be(t,e){if("string"!=typeof t||""===t)throw new Error(e)}}const Q={trace:e.Trace,debug:e.Debug,info:e.Information,information:e.Information,warn:e.Warning,warning:e.Warning,error:e.Error,critical:e.Critical,none:e.None};class Y{configureLogging(t){if(w.isRequired(t,"logging"),void 0!==t.log)this.logger=t;else if("string"==typeof t){const e=function(t){const e=Q[t.toLowerCase()];if(void 0!==e)return e;throw new Error(`Unknown log level: ${t}`)}(t);this.logger=new $(e)}else this.logger=new $(t);return this}withUrl(t,e){return w.isRequired(t,"url"),w.isNotEmpty(t,"url"),this.url=t,this.httpConnectionOptions="object"==typeof e?{...this.httpConnectionOptions,...e}:{...this.httpConnectionOptions,transport:e},this}withHubProtocol(t){return w.isRequired(t,"protocol"),this.protocol=t,this}withAutomaticReconnect(t){if(this.reconnectPolicy)throw new Error("A reconnectPolicy has already been set.");return t?Array.isArray(t)?this.reconnectPolicy=new M(t):this.reconnectPolicy=t:this.reconnectPolicy=new M,this}build(){const t=this.httpConnectionOptions||{};if(void 0===t.logger&&(t.logger=this.logger),!this.url)throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");const e=new z(this.url,t);return N.create(e,this.logger||p.instance,this.protocol||new K,this.reconnectPolicy)}}return Uint8Array.prototype.indexOf||Object.defineProperty(Uint8Array.prototype,"indexOf",{value:Array.prototype.indexOf,writable:!0}),Uint8Array.prototype.slice||Object.defineProperty(Uint8Array.prototype,"slice",{value:function(t,e){return new Uint8Array(Array.prototype.slice.call(this,t,e))},writable:!0}),Uint8Array.prototype.forEach||Object.defineProperty(Uint8Array.prototype,"forEach",{value:Array.prototype.forEach,writable:!0}),s})(),"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.signalR=e():t.signalR=e(); 2 | //# sourceMappingURL=signalr.min.js.map -------------------------------------------------------------------------------- /SignalRClient/Program.cs: -------------------------------------------------------------------------------- 1 |  2 | using Microsoft.AspNetCore.SignalR.Client; 3 | 4 | var connection = new HubConnectionBuilder() 5 | .WithUrl("http://localhost:8080/chat") 6 | .Build(); 7 | 8 | connection.On("Send", (string message) => 9 | { 10 | Console.WriteLine($"R: {message}"); 11 | }); 12 | 13 | await connection.StartAsync(); 14 | 15 | while (true) 16 | { 17 | Console.Write("S: "); 18 | var line = Console.ReadLine(); 19 | Console.WriteLine(); 20 | await connection.InvokeAsync("Send", line); 21 | } -------------------------------------------------------------------------------- /SignalRClient/SignalRClient.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SmartRouter.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.5.33129.541 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2778501B-5D30-42FE-B579-381BF25C49A9}" 7 | ProjectSection(SolutionItems) = preProject 8 | README.md = README.md 9 | tye.yaml = tye.yaml 10 | EndProjectSection 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{91DA43C0-63A3-4F07-AD01-3C7AEBB97EB6}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yarp.Ingress", "Yarp.Ingress\Yarp.Ingress.csproj", "{6BD1EBA1-ABF8-458D-9342-1C80D56158B8}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignalRClient", "SignalRClient\SignalRClient.csproj", "{B8BCC8EE-EDB3-43B2-83DE-3C7CE13CA201}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.ServiceDiscovery", "Microsoft.Extensions.ServiceDiscovery\Microsoft.Extensions.ServiceDiscovery.csproj", "{1C21890A-FB0E-4F8D-A958-8D8897D9E7B7}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.LoadBalancing", "Microsoft.AspNetCore.SignalR.LoadBalancing\Microsoft.AspNetCore.SignalR.LoadBalancing.csproj", "{63611FBD-9A21-4450-8A7C-60406BDF53C7}" 21 | EndProject 22 | Global 23 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 24 | Debug|Any CPU = Debug|Any CPU 25 | Release|Any CPU = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 28 | {91DA43C0-63A3-4F07-AD01-3C7AEBB97EB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {91DA43C0-63A3-4F07-AD01-3C7AEBB97EB6}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {91DA43C0-63A3-4F07-AD01-3C7AEBB97EB6}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {91DA43C0-63A3-4F07-AD01-3C7AEBB97EB6}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {6BD1EBA1-ABF8-458D-9342-1C80D56158B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {6BD1EBA1-ABF8-458D-9342-1C80D56158B8}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {6BD1EBA1-ABF8-458D-9342-1C80D56158B8}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {6BD1EBA1-ABF8-458D-9342-1C80D56158B8}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {B8BCC8EE-EDB3-43B2-83DE-3C7CE13CA201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {B8BCC8EE-EDB3-43B2-83DE-3C7CE13CA201}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {B8BCC8EE-EDB3-43B2-83DE-3C7CE13CA201}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {B8BCC8EE-EDB3-43B2-83DE-3C7CE13CA201}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {1C21890A-FB0E-4F8D-A958-8D8897D9E7B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {1C21890A-FB0E-4F8D-A958-8D8897D9E7B7}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {1C21890A-FB0E-4F8D-A958-8D8897D9E7B7}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {1C21890A-FB0E-4F8D-A958-8D8897D9E7B7}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {63611FBD-9A21-4450-8A7C-60406BDF53C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {63611FBD-9A21-4450-8A7C-60406BDF53C7}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {63611FBD-9A21-4450-8A7C-60406BDF53C7}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {63611FBD-9A21-4450-8A7C-60406BDF53C7}.Release|Any CPU.Build.0 = Release|Any CPU 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | GlobalSection(ExtensibilityGlobals) = postSolution 53 | SolutionGuid = {73742BAC-6657-4224-8273-81640805FED6} 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /Yarp.Ingress/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.ServiceDiscovery; 2 | using Yarp.ReverseProxy.Configuration; 3 | 4 | var builder = WebApplication.CreateBuilder(args); 5 | 6 | var routes = new[] 7 | { 8 | new RouteConfig() 9 | { 10 | RouteId = "route1", 11 | ClusterId = "cluster1", 12 | Match = new RouteMatch 13 | { 14 | Path = "{**catch-all}" 15 | } 16 | }, 17 | // Mark the negotiate route as a signalr route so we can affinitize the connection id 18 | new RouteConfig() 19 | { 20 | RouteId = "route2", 21 | ClusterId = "cluster1", 22 | Match = new RouteMatch 23 | { 24 | Path = "/chat/negotiate" 25 | }, 26 | Metadata = new Dictionary { ["hub"] = "true" } 27 | }, 28 | }; 29 | 30 | var destinations = new Dictionary(StringComparer.OrdinalIgnoreCase); 31 | 32 | var discovery = new TyeServiceDiscovery(builder.Configuration); 33 | 34 | foreach (var (key, address) in await discovery.GetAddressesAsync("sample")) 35 | { 36 | destinations[key] = new DestinationConfig() 37 | { 38 | Address = address.ToString(), 39 | }; 40 | Console.WriteLine($"{key} => {address}"); 41 | } 42 | 43 | var clusters = new[] 44 | { 45 | new ClusterConfig() 46 | { 47 | ClusterId = "cluster1", 48 | SessionAffinity = new SessionAffinityConfig() 49 | { 50 | AffinityKeyName = "yarp.affinity", 51 | Policy = "SignalR", 52 | Enabled = true 53 | }, 54 | Destinations = destinations 55 | } 56 | }; 57 | 58 | builder.Services.AddReverseProxy() 59 | .AddSignalRSessionAffinity() 60 | .LoadFromMemory(routes, clusters); 61 | 62 | var app = builder.Build(); 63 | 64 | app.MapReverseProxy(); 65 | 66 | app.Run(); 67 | -------------------------------------------------------------------------------- /Yarp.Ingress/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:32246", 7 | "sslPort": 44344 8 | } 9 | }, 10 | "profiles": { 11 | "http": { 12 | "commandName": "Project", 13 | "dotnetRunMessages": true, 14 | "launchBrowser": true, 15 | "applicationUrl": "http://localhost:5016", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "https": { 21 | "commandName": "Project", 22 | "dotnetRunMessages": true, 23 | "launchBrowser": true, 24 | "applicationUrl": "https://localhost:7221;http://localhost:5016", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | }, 29 | "IIS Express": { 30 | "commandName": "IISExpress", 31 | "launchBrowser": true, 32 | "environmentVariables": { 33 | "ASPNETCORE_ENVIRONMENT": "Development" 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Yarp.Ingress/Yarp.Ingress.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Yarp.Ingress/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Yarp.Ingress/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*" 9 | } 10 | -------------------------------------------------------------------------------- /tye.yaml: -------------------------------------------------------------------------------- 1 | # This topology is testing SignalR in front of a load balancer 2 | name: smartrouter-yarp 3 | services: 4 | - name: ingress 5 | project: Yarp.Ingress/Yarp.Ingress.csproj 6 | bindings: 7 | - protocol: http 8 | port: 8080 9 | - name: sample 10 | project: Sample/Sample.csproj 11 | replicas: 3 12 | - name: redis 13 | image: redis 14 | bindings: 15 | - port: 6379 16 | connectionString: "${host}:${port}" 17 | --------------------------------------------------------------------------------