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
isClosed
Added before v1.9
webSocket.isClosedIndicates that the web socket has been closed.
Usage
Returns
url
Added before v1.9
webSocket.urlContains the URL of the WebSocket.
Usage
Returns
waitForFrameReceived
Added in: v1.10
webSocket.waitForFrameReceivedPerforms 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.
Usage
WebSocket.waitForFrameReceived(callback);
WebSocket.waitForFrameReceived(callback, options);
Arguments
-
optionsWebSocket.WaitForFrameReceivedOptions(optional)-
setPredicatePredicate<WebSocketFrame> (optional) Added in: v1.9#Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.
-
setTimeoutdouble (optional) Added in: v1.9#Maximum time to wait for in milliseconds. Defaults to
30000(30 seconds). Pass0to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout().
-
-
callbackRunnable Added in: v1.9#Callback that performs the action triggering the event.
Returns
waitForFrameSent
Added in: v1.10
webSocket.waitForFrameSentPerforms 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.
Usage
WebSocket.waitForFrameSent(callback);
WebSocket.waitForFrameSent(callback, options);
Arguments
-
optionsWebSocket.WaitForFrameSentOptions(optional)-
setPredicatePredicate<WebSocketFrame> (optional) Added in: v1.9#Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.
-
setTimeoutdouble (optional) Added in: v1.9#Maximum time to wait for in milliseconds. Defaults to
30000(30 seconds). Pass0to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout().
-
-
callbackRunnable Added in: v1.9#Callback that performs the action triggering the event.
Returns
Events
onClose(handler)
Added before v1.9
webSocket.onClose(handler)Fired when the websocket closes.
Usage
WebSocket.onClose(handler)
Event data
onFrameReceived(handler)
Added in: v1.9
webSocket.onFrameReceived(handler)Fired when the websocket receives a frame.
Usage
WebSocket.onFrameReceived(handler)
Event data
onFrameSent(handler)
Added in: v1.9
webSocket.onFrameSent(handler)Fired when the websocket sends a frame.
Usage
WebSocket.onFrameSent(handler)
Event data
onSocketError(handler)
Added in: v1.9
webSocket.onSocketError(handler)Fired when the websocket has an error.
Usage
WebSocket.onSocketError(handler)
Event data