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?;

    // Set up the waiter BEFORE the action that opens the WebSocket
    let ws_waiter = page.expect_websocket(None).await?;

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

    let ws = ws_waiter.wait().await?;
    println!("WebSocket URL: {}", ws.url());

    // Wait for the connection to close
    let close_waiter = ws.expect_close(Some(5000.0)).await?;
    // ... trigger close ...
    close_waiter.wait().await?;

    assert!(ws.is_closed());

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

Structs§

WebSocket
Represents a WebSocket connection initiated by a page.