1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Example: WebSocket Interception
//
// This example demonstrates how to intercept and inspect WebSocket connections
// and messages.
//
// To run this example:
// cargo run --example websocket
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 context = browser.new_context().await?;
let page = context.new_page().await?;
// Subscribe to the "websocket" event
// This handler triggers whenever the page opens a new WebSocket connection
page.on_websocket(|ws| {
println!("WebSocket opened: {}", ws.url());
// Clone ws to move into async block
let ws_clone = ws.clone();
Box::pin(async move {
// Subscribe to "framereceived" events (messages from server)
ws_clone
.on_frame_received(|payload| {
Box::pin(async move {
println!("Create Frame Received: {}", payload);
Ok(())
})
})
.await?;
// Subscribe to "framesent" events (messages from client)
ws_clone
.on_frame_sent(|payload| {
Box::pin(async move {
println!("Client Frame Sent: {}", payload);
Ok(())
})
})
.await?;
// Subscribe to "close" event
ws_clone
.on_close(|_| {
Box::pin(async move {
println!("WebSocket closed");
Ok(())
})
})
.await?;
Ok(())
})
})
.await?;
println!("Navigating to websocket.org echo test...");
// Using a public echo test page
page.goto("https://websocket.org/echo.html", None).await?;
// In a real scenario, you might interact with the page to trigger WS messages
// For this example, we just wait a bit to let the page load and potentially connect
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// You can also inspect the page to trigger actions
// page.click("button:has-text('Connect')").await?;
// page.click("button:has-text('Send')").await?;
println!("Closing browser...");
browser.close().await?;
Ok(())
}