PlaywrightServer Class
A Playwright server (as npx playwright run-server) whose browser is the managed browser engine of Meziantou.Framework.WebPlatform: the Playwright clients connect to its WebSocket endpoint with BrowserType.ConnectAsync(wsEndpoint) and automate the pages it hosts in the process.
public sealed class PlaywrightServer : IAsyncDisposableRemarks
Each connection gets a browser; each browser context of the clients runs its pages on its own thread, with its own script engine, cookies and storage. The protocol of Playwright changes with each minor version: the clients must use the version of PlaywrightServer.PlaywrightVersion (see PlaywrightServerOptions.AllowAnyClientVersion).
The scripts of the pages run in the process (see the threat model of the script engine), and the documents are fetched by PlaywrightServerOptions.DocumentLoader: expose the server to trusted clients only.
- Implements
Constructors
| Name | Description |
|---|---|
| PlaywrightServer(PlaywrightServerOptions?) | Creates a server. Call PlaywrightServer.StartAsync to listen. |
PlaywrightServer(PlaywrightServerOptions?)
PlaywrightServer.csCreates a server. Call PlaywrightServer.StartAsync to listen.
public PlaywrightServer(PlaywrightServerOptions? options = null)Parameters
optionsPlaywrightServerOptions?- The options, or null for the default options.
Properties
| Name | Description |
|---|---|
| Port | Gets the TCP port the server listens on, once it is started. |
| WsEndpoint | Gets the WebSocket endpoint of the server, once it is started (e.g. ws://127.0.0.1:3000/), to give to BrowserType.ConnectAsync. |
WsEndpoint
PlaywrightServer.csMethods
| Name | Description |
|---|---|
| DisposeAsync() | Stops listening, and closes the connections (their browser contexts are closed). |
| HandleConnectionAsync(WebSocket, CancellationToken) | Runs the connection of a client on a WebSocket accepted by the host (e.g. by ASP.NET Core), until the client disconnects or cancellationToken is canceled. The host checks the version of the client first (see PlaywrightServer.IsCompatibleClient). |
| IsCompatibleClient(string?) | Returns whether a client can connect, given the User-Agent header of its WebSocket request (e.g. Playwright/x.y.z (arm64; macOS 26.0) csharp/10.0): its major and minor versions must be the ones of PlaywrightServer.PlaywrightVersion. A user agent that is not the one of a Playwright client is accepted. |
| StartAsync(CancellationToken) | Starts listening for the connections of the clients. |
DisposeAsync()
PlaywrightServer.csHandleConnectionAsync(WebSocket, CancellationToken)
PlaywrightServer.csRuns the connection of a client on a WebSocket accepted by the host (e.g. by ASP.NET Core), until the client disconnects or cancellationToken is canceled. The host checks the version of the client first (see PlaywrightServer.IsCompatibleClient).
public Task HandleConnectionAsync(WebSocket webSocket, CancellationToken cancellationToken = default)Parameters
webSocketWebSocket- The WebSocket of the client.
cancellationTokenCancellationToken- Closes the connection.
Returns
IsCompatibleClient(string?)
PlaywrightServer.csReturns whether a client can connect, given the User-Agent header of its WebSocket request (e.g. Playwright/x.y.z (arm64; macOS 26.0) csharp/10.0): its major and minor versions must be the ones of PlaywrightServer.PlaywrightVersion. A user agent that is not the one of a Playwright client is accepted.
StartAsync(CancellationToken)
PlaywrightServer.csStarts listening for the connections of the clients.
public Task StartAsync(CancellationToken cancellationToken = default)Constants
| Name | Description |
|---|---|
| PlaywrightVersion | The version of Playwright whose protocol the server implements (the clients must have the same major and minor versions). |
PlaywrightVersion
The version of Playwright whose protocol the server implements (the clients must have the same major and minor versions).
public const string PlaywrightVersion = "1.64.1"