Skip to main content

WebSocket

WebSocket 类代表页面中的 websocket 连接。

¥The WebSocket class represents websocket connections in the page.


方法

¥Methods

isClosed

Added before v1.9 webSocket.isClosed

表示 Web 套接字已关闭。

¥Indicates that the web socket has been closed.

用法

¥Usage

webSocket.isClosed();

返回

¥Returns


url

Added before v1.9 webSocket.url

包含 WebSocket 的 URL。

¥Contains the URL of the WebSocket.

用法

¥Usage

webSocket.url();

返回

¥Returns


waitForEvent

Added before v1.9 webSocket.waitForEvent

等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果在事件触发之前关闭 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

await webSocket.waitForEvent(event);
await webSocket.waitForEvent(event, optionsOrPredicate, options);

参数

¥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.

等待的最长时间(以毫秒为单位)。默认为 0 - 没有超时。可以通过配置中的 actionTimeout 选项或使用 browserContext.setDefaultTimeout()page.setDefaultTimeout() 方法更改默认值。

¥Maximum time to wait for in milliseconds. Defaults to 0 - no timeout. The default value can be changed via actionTimeout option in the config, or by using the browserContext.setDefaultTimeout() or page.setDefaultTimeout() methods.

接收事件的谓词或选项对象。可选的。

¥Either a predicate that receives an event or an options object. Optional.

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

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

返回

¥Returns


事件

¥Events

on('close')

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

当 websocket 关闭时触发。

¥Fired when the websocket closes.

用法

¥Usage

webSocket.on('close', data => {});

事件数据

¥Event data


on('framereceived')

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

当 websocket 收到帧时触发。

¥Fired when the websocket receives a frame.

用法

¥Usage

webSocket.on('framereceived', data => {});

事件数据

¥Event data

帧有效负载

¥frame payload


on('framesent')

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

当 websocket 发送帧时触发。

¥Fired when the websocket sends a frame.

用法

¥Usage

webSocket.on('framesent', data => {});

事件数据

¥Event data

帧有效负载

¥frame payload


on('socketerror')

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

当 websocket 出现错误时触发。

¥Fired when the websocket has an error.

用法

¥Usage

webSocket.on('socketerror', data => {});

事件数据

¥Event data