pub struct Connection<W, R>{ /* private fields */ }Expand description
JSON-RPC connection to Playwright server
Manages request/response correlation and event dispatch. Uses sequential request IDs and oneshot channels for correlation.
§Thread Safety
Connection is thread-safe and can be shared across async tasks using Arc.
Multiple concurrent requests are supported.
§Architecture
This follows the pattern from official Playwright bindings:
- Python: Direct callback on message receive
- Java: Callback map with synchronized access
- .NET: ConcurrentDictionary with TaskCompletionSource
Rust implementation uses:
AtomicU32for thread-safe ID generationArc<Mutex<HashMap>>for callback storagetokio::sync::oneshotfor request/response correlation
Implementations§
Source§impl<W, R> Connection<W, R>
impl<W, R> Connection<W, R>
Sourcepub fn new(
transport: PipeTransport<W, R>,
message_rx: UnboundedReceiver<Value>,
) -> Self
pub fn new( transport: PipeTransport<W, R>, message_rx: UnboundedReceiver<Value>, ) -> Self
Create a new Connection with the given transport
§Arguments
transport- Transport connected to Playwright servermessage_rx- Receiver for incoming messages from transport
§Example
let (stdin_read, stdin_write) = duplex(1024);
let (stdout_read, stdout_write) = duplex(1024);
let (transport, message_rx) = PipeTransport::new(stdin_write, stdout_read);
let connection = Connection::new(transport, message_rx);Sourcepub async fn send_message(
&self,
guid: &str,
method: &str,
params: Value,
) -> Result<Value>
pub async fn send_message( &self, guid: &str, method: &str, params: Value, ) -> Result<Value>
Send a message to the Playwright server and await response
This method:
- Generates a unique request ID
- Creates a oneshot channel for the response
- Stores the channel sender in the callbacks map
- Serializes and sends the request via transport
- Awaits the response on the receiver
§Arguments
guid- GUID of the target object (e.g., “page@abc123”)method- Method name to invoke (e.g., “goto”)params- Method parameters as JSON value
§Returns
The result value from the server, or an error if:
- Transport send fails
- Server returns an error
- Connection is closed before response arrives
See module-level documentation for usage examples.
Sourcepub async fn initialize_playwright(
self: &Arc<Self>,
) -> Result<Arc<dyn ChannelOwner>>
pub async fn initialize_playwright( self: &Arc<Self>, ) -> Result<Arc<dyn ChannelOwner>>
Initialize the Playwright connection and return the root Playwright object
This method implements the initialization handshake with the Playwright server:
- Creates a temporary Root object
- Sends “initialize” message with sdkLanguage=“rust”
- Server creates BrowserType objects (sends
__create__messages) - Server responds with Playwright GUID
- Looks up Playwright object from registry (guaranteed to exist)
The initialize message is synchronous - by the time the response arrives,
all protocol objects have been created and registered.
§Returns
An Arc<dyn ChannelOwner> that is the Playwright object. Callers should downcast
to Playwright type.
§Errors
Returns error if:
- Initialize message fails to send
- Server returns protocol error
- Response is missing Playwright GUID
- Playwright object not found in registry
- Timeout after 30 seconds
See module-level documentation for usage examples.
See also:
Sourcepub async fn run(self: &Arc<Self>)
pub async fn run(self: &Arc<Self>)
Run the message dispatch loop
This method continuously reads messages from the transport and dispatches them:
- Responses (with
id) are correlated with pending requests - Events (without
id) are dispatched to protocol objects (TODO: Future phase - event handling)
The loop runs until the transport channel is closed.
§Usage
This method should be spawned in a background task:
let conn = Arc::clone(&connection);
tokio::spawn(async move {
conn.run().await;
});