Skip to main content

WebSocket

WebSocket 类表示页面内的 WebSocket 连接。它提供了检查和操作正在传输和接收的数据的能力。

¥The WebSocket class represents WebSocket connections within a page. It provides the ability to inspect and manipulate the data being transmitted and received.

如果你想拦截或修改 WebSocket 帧,请考虑使用 WebSocketRoute

¥If you want to intercept or modify WebSocket frames, consider using WebSocketRoute.


方法

¥Methods

expect_event

Added before v1.9 webSocket.expect_event

等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果在事件触发之前关闭 webSocket,将会抛出错误。返回事件数据值。

¥Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the webSocket is closed before the event is fired. Returns the event data value.

用法

¥Usage

web_socket.expect_event(event)
web_socket.expect_event(event, **kwargs)

参数

¥Arguments

事件名称,相同的将传递到 webSocket.on(event)

¥Event name, same one would pass into webSocket.on(event).

接收事件数据并在等待解决时解析为真值。

¥Receives the event data and resolves to truthy value when the waiting should resolve.

等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 browser_context.set_default_timeout() 更改默认值。

¥Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().

返回

¥Returns


wait_for_event

Added before v1.9 webSocket.wait_for_event
注意

在大多数情况下,你应该使用 web_socket.expect_event()

¥In most cases, you should use web_socket.expect_event().

等待给定的 event 触发。如果提供了谓词,它会将事件的值传递给 predicate 函数,并等待 predicate(event) 返回真值。如果在 event 事件触发之前关闭套接字,则会抛出错误。

¥Waits for given event to fire. If predicate is provided, it passes event's value into the predicate function and waits for predicate(event) to return a truthy value. Will throw an error if the socket is closed before the event is fired.

用法

¥Usage

web_socket.wait_for_event(event)
web_socket.wait_for_event(event, **kwargs)

参数

¥Arguments

事件名称,通常传递到 *.on(event) 中。

¥Event name, same one typically passed into *.on(event).

接收事件数据并在等待解决时解析为真值。

¥Receives the event data and resolves to truthy value when the waiting should resolve.

等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 browser_context.set_default_timeout() 更改默认值。

¥Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().

返回

¥Returns


属性

¥Properties

is_closed

Added before v1.9 webSocket.is_closed

表示 Web 套接字已关闭。

¥Indicates that the web socket has been closed.

用法

¥Usage

web_socket.is_closed()

返回

¥Returns


url

Added before v1.9 webSocket.url

包含 WebSocket 的 URL。

¥Contains the URL of the WebSocket.

用法

¥Usage

web_socket.url

返回

¥Returns


事件

¥Events

on("close")

Added before v1.9 webSocket.on("close")

当 websocket 关闭时触发。

¥Fired when the websocket closes.

用法

¥Usage

web_socket.on("close", handler)

事件数据

¥Event data


on("framereceived")

Added in: v1.9 webSocket.on("framereceived")

当 websocket 收到帧时触发。

¥Fired when the websocket receives a frame.

用法

¥Usage

web_socket.on("framereceived", handler)

事件数据

¥Event data


on("framesent")

Added in: v1.9 webSocket.on("framesent")

当 websocket 发送帧时触发。

¥Fired when the websocket sends a frame.

用法

¥Usage

web_socket.on("framesent", handler)

事件数据

¥Event data


on("socketerror")

Added in: v1.9 webSocket.on("socketerror")

当 websocket 出现错误时触发。

¥Fired when the websocket has an error.

用法

¥Usage

web_socket.on("socketerror", handler)

事件数据

¥Event data