WebSocket
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.9Indicates that the web socket has been closed.
使用方式
WebSocket.isClosed();
傳回值
url
Added before v1.9Contains the URL of the WebSocket.
使用方式
WebSocket.url();
傳回值
waitForFrameReceived
Added in: v1.10Performs 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.
使用方式
WebSocket.waitForFrameReceived(callback);
WebSocket.waitForFrameReceived(callback, options);
參數
-
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.
傳回值
waitForFrameSent
Added in: v1.10Performs 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.
使用方式
WebSocket.waitForFrameSent(callback);
WebSocket.waitForFrameSent(callback, options);
參數
-
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.
傳回值
事件 (Events)
onClose(handler)
Added before v1.9Fired when the websocket closes.
使用方式
WebSocket.onClose(handler)
事件資料
onFrameReceived(handler)
Added in: v1.9Fired when the websocket receives a frame.
使用方式
WebSocket.onFrameReceived(handler)
事件資料
onFrameSent(handler)
Added in: v1.9Fired when the websocket sends a frame.
使用方式
WebSocket.onFrameSent(handler)
事件資料
onSocketError(handler)
Added in: v1.9Fired when the websocket has an error.
使用方式
WebSocket.onSocketError(handler)
事件資料