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();
傳回值
waitForEvent
Added before v1.9Waits 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 name, same one would pass into
webSocket.on(event)
. -
optionsOrPredicate
function | Object (optional)#-
predicate
functionReceives 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
0
- no timeout. The default value can be changed viaactionTimeout
option in the config, or by using the browserContext.setDefaultTimeout() or page.setDefaultTimeout() methods.
Either a predicate that receives an event or an options object. Optional.
-
-
options
Object (optional)
傳回值
事件 (Events)
on('close')
Added before v1.9Fired when the websocket closes.
使用方式
webSocket.on('close', data => {});
事件資料
on('framereceived')
Added in: v1.9Fired when the websocket receives a frame.
使用方式
webSocket.on('framereceived', data => {});
事件資料
on('framesent')
Added in: v1.9Fired when the websocket sends a frame.
使用方式
webSocket.on('framesent', data => {});
事件資料
on('socketerror')
Added in: v1.9Fired when the websocket has an error.
使用方式
webSocket.on('socketerror', data => {});
事件資料