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

isClosed

Added before v1.9 webSocket.isClosed

表示 Web 套接字已关闭。

🌐 Indicates that the web socket has been closed.

用法

webSocket.isClosed();

返回


url

Added before v1.9 webSocket.url

包含 WebSocket 的 URL。

🌐 Contains the URL of the WebSocket.

用法

webSocket.url();

返回


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.

用法

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

参数

  • event string#

    事件名称,相同的会传入 webSocket.on(event)

  • optionsOrPredicate function | Object (optional)#

    可以是接收事件的谓词,也可以是一个选项对象。可选。

  • options Object (optional)

    • predicate function (optional)#

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

返回


事件

🌐 Events

on('close')

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

当 websocket 关闭时触发。

🌐 Fired when the websocket closes.

用法

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

事件数据


on('framereceived')

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

当 websocket 收到帧时触发。

🌐 Fired when the websocket receives a frame.

用法

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

事件数据


on('framesent')

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

当 websocket 发送帧时触发。

🌐 Fired when the websocket sends a frame.

用法

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

事件数据


on('socketerror')

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

当 websocket 出现错误时触发。

🌐 Fired when the websocket has an error.

用法

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

事件数据