Skip to main content

WebSocket

The WebSocket class represents websocket connections in the page.


Methods

isClosed

Added in: v1.8webSocket.isClosed

Indicates that the web socket has been closed.

Usage

webSocket.isClosed();

Returns


url

Added in: v1.8webSocket.url

Contains the URL of the WebSocket.

Usage

webSocket.url();

Returns


waitForEvent

Added in: v1.8webSocket.waitForEvent

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

  • event string#

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

  • optionsOrPredicate function|Object (optional)#

    • predicate function

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

    • timeout number (optional)

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

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

  • options Object (optional)

    • predicate function (optional)#

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

Returns


Events

on('close')

Added in: v1.8webSocket.on('close')

Fired when the websocket closes.

Usage

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

Event data


on('framereceived')

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

Fired when the websocket receives a frame.

Usage

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

Event data


on('framesent')

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

Fired when the websocket sends a frame.

Usage

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

Event data


on('socketerror')

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

Fired when the websocket has an error.

Usage

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

Event data