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 : IAsyncDisposable

Remarks

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

NameDescription
PlaywrightServer(PlaywrightServerOptions?)Creates a server. Call PlaywrightServer.StartAsync to listen.

PlaywrightServer(PlaywrightServerOptions?)

PlaywrightServer.cs

Creates a server. Call PlaywrightServer.StartAsync to listen.

public PlaywrightServer(PlaywrightServerOptions? options = null)

Parameters

options PlaywrightServerOptions?
The options, or null for the default options.

Properties

NameDescription
PortGets the TCP port the server listens on, once it is started.
WsEndpointGets the WebSocket endpoint of the server, once it is started (e.g. ws://127.0.0.1:3000/), to give to BrowserType.ConnectAsync.

Gets the TCP port the server listens on, once it is started.

public int Port { get; }

Property value

int

Gets the WebSocket endpoint of the server, once it is started (e.g. ws://127.0.0.1:3000/), to give to BrowserType.ConnectAsync.

public Uri WsEndpoint { get; }

Property value

Uri

Methods

NameDescription
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.cs

Stops listening, and closes the connections (their browser contexts are closed).

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask

HandleConnectionAsync(WebSocket, CancellationToken)

PlaywrightServer.cs

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).

public Task HandleConnectionAsync(WebSocket webSocket, CancellationToken cancellationToken = default)

Parameters

webSocket WebSocket
The WebSocket of the client.
cancellationToken CancellationToken
Closes the connection.

Returns

Task

IsCompatibleClient(string?)

PlaywrightServer.cs

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.

public static bool IsCompatibleClient(string? userAgent)

Parameters

userAgent string?
The value of the User-Agent header, or null.

Returns

bool

StartAsync(CancellationToken)

PlaywrightServer.cs

Starts listening for the connections of the clients.

public Task StartAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

Constants

NameDescription
PlaywrightVersionThe 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"