Skip to main content

Module web_socket

Module web_socket 

Source
Expand description

WebSocket protocol object — represents a WebSocket connection in the page.

§Example

use playwright_rs::protocol::Playwright;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let playwright = Playwright::launch().await?;
    let browser = playwright.chromium().launch().await?;
    let page = browser.new_page().await?;

    // Register the handler BEFORE the action that opens the WebSocket
    page.on_websocket(|ws| async move {
        println!("WebSocket URL: {}", ws.url());

        // Wait for the connection to close
        let close_waiter = ws.expect_close(Some(5000.0)).await?;
        close_waiter.wait().await?;
        assert!(ws.is_closed());
        Ok(())
    }).await?;

    // Navigate to a page that opens a WebSocket
    page.goto("https://example.com/ws-demo", None).await?;

    browser.close().await?;
    Ok(())
}

Structs§

WebSocket
Represents a WebSocket connection initiated by a page.