跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

WebSocket ​

Added in: v1.8

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

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

Methods ​

webSocket.isClosed() ​

Added in: v1.8

Indicates that the web socket has been closed.

Returns

  • boolean

webSocket.url() ​

Added in: v1.8

Contains the URL of the WebSocket.

Returns

  • string

webSocket.waitForFrameReceived() ​

Added in: v1.10

Performs action and waits for a frame to be sent. If predicate is provided, it passes WebSocketFrame value into the predicate function and waits for predicate(webSocketFrame) to return a truthy value. Will throw an error if the WebSocket or Page is closed before the frame is received.

Arguments

  • options Object (optional)

    • predicate function(WebSocketFrame): boolean (optional) Added in: v1.9

      Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.

    • timeout float (optional) Added in: v1.9

      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 browserContext.setDefaultTimeout().

  • callback Runnable Added in: v1.9

    Callback that performs the action triggering the event.

Returns

  • WebSocketFrame

webSocket.waitForFrameSent() ​

Added in: v1.10

Performs action and waits for a frame to be sent. If predicate is provided, it passes WebSocketFrame value into the predicate function and waits for predicate(webSocketFrame) to return a truthy value. Will throw an error if the WebSocket or Page is closed before the frame is sent.

Arguments

  • options Object (optional)

    • predicate function(WebSocketFrame): boolean (optional) Added in: v1.9

      Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.

    • timeout float (optional) Added in: v1.9

      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 browserContext.setDefaultTimeout().

  • callback Runnable Added in: v1.9

    Callback that performs the action triggering the event.

Returns

  • WebSocketFrame

Events ​

webSocket.onClose() ​

Added in: v1.8

Fired when the websocket closes.

Event data

  • WebSocket

webSocket.onFrameReceived() ​

Added in: v1.9

Fired when the websocket receives a frame.

Event data

  • WebSocketFrame

webSocket.onFrameSent() ​

Added in: v1.9

Fired when the websocket sends a frame.

Event data

  • WebSocketFrame

webSocket.onSocketError() ​

Added in: v1.9

Fired when the websocket has an error.

Event data

  • string

基于 Apache 2.0 许可证发布。