|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
| 2 | +using System.Net.WebSockets; |
| 3 | +using System.Threading; |
5 | 4 | using System.Threading.Tasks; |
6 | 5 | using System.Web.Hosting; |
7 | | -using Microsoft.Extensions.Logging; |
8 | 6 | using PuppeteerSharp.Transport; |
9 | 7 |
|
10 | 8 | namespace PuppeteerSharp.AspNetFramework |
11 | 9 | { |
12 | 10 | public class AspNetWebSocketTransport : WebSocketTransport |
13 | 11 | { |
| 12 | + #region Static fields |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Gets a <see cref="TransportFactory"/> that creates <see cref="AspNetWebSocketTransport"/> instances. |
| 16 | + /// </summary> |
| 17 | + public static readonly TransportFactory AspNetTransportFactory = CreateAspNetTransport; |
| 18 | + |
14 | 19 | /// <summary> |
15 | | - /// Initializes a new instance of the <see cref="PuppeteerSharp.AspNet.AspNetWebSocketTransport"/> class. |
| 20 | + /// Gets a <see cref="TransportTaskScheduler"/> that uses ASP.NET <see cref="HostingEnvironment.QueueBackgroundWorkItem(Func{CancellationToken,Task})"/> |
| 21 | + /// for scheduling of tasks. |
16 | 22 | /// </summary> |
17 | | - public AspNetWebSocketTransport() : base(false) |
| 23 | + public static readonly TransportTaskScheduler AspNetTransportScheduler = ScheduleBackgroundTask; |
| 24 | + |
| 25 | + #endregion |
| 26 | + |
| 27 | + #region Static methods |
| 28 | + |
| 29 | + private static async Task<IConnectionTransport> CreateAspNetTransport(Uri url, IConnectionOptions connectionOptions, CancellationToken cancellationToken) |
18 | 30 | { |
| 31 | + var webSocketFactory = connectionOptions.WebSocketFactory ?? DefaultWebSocketFactory; |
| 32 | + var webSocket = await webSocketFactory(url, connectionOptions, cancellationToken); |
| 33 | + return new AspNetWebSocketTransport(webSocket, connectionOptions.EnqueueTransportMessages); |
19 | 34 | } |
20 | 35 |
|
21 | | - public override async Task InitializeAsync(string url, IConnectionOptions connectionOptions, ILoggerFactory loggerFactory = null) |
| 36 | + private static void ScheduleBackgroundTask(Func<CancellationToken, Task> taskFactory, CancellationToken cancellationToken) |
22 | 37 | { |
23 | | - await base.InitializeAsync(url, connectionOptions, loggerFactory).ConfigureAwait(false); |
24 | | - HostingEnvironment.QueueBackgroundWorkItem((cts) => GetResponseAsync()); |
| 38 | + Task ExecuteAsync(CancellationToken hostingCancellationToken) |
| 39 | + => taskFactory(CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, hostingCancellationToken).Token); |
| 40 | + HostingEnvironment.QueueBackgroundWorkItem(ExecuteAsync); |
25 | 41 | } |
| 42 | + |
| 43 | + #endregion |
| 44 | + |
| 45 | + #region Constructor(s) |
| 46 | + |
| 47 | + /// <inheritdoc /> |
| 48 | + public AspNetWebSocketTransport(WebSocket client, bool queueRequests) |
| 49 | + : base(client, AspNetTransportScheduler, queueRequests) |
| 50 | + { } |
| 51 | + |
| 52 | + #endregion |
26 | 53 | } |
27 | 54 | } |
0 commit comments