WebSocket
WebSocket 类表示页面内的 WebSocket 连接。它提供了检查和操作正在发送和接收的数据的能力。
🌐 The WebSocket class represents WebSocket connections within a page. It provides the ability to inspect and manipulate the data being transmitted and received.
如果你想拦截或修改 WebSocket 帧,可以考虑使用 WebSocketRoute。
🌐 If you want to intercept or modify WebSocket frames, consider using WebSocketRoute.
方法
🌐 Methods
isClosed
Added before v1.9表示 Web 套接字已关闭。
🌐 Indicates that the web socket has been closed.
用法
webSocket.isClosed();
返回
url
Added before v1.9包含 WebSocket 的 URL。
🌐 Contains the URL of the WebSocket.
用法
webSocket.url();
返回
waitForEvent
Added before v1.9等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果在事件触发前 WebSocket 被关闭,将抛出错误。返回事件数据值。
🌐 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.
用法
await webSocket.waitForEvent(event);
await webSocket.waitForEvent(event, optionsOrPredicate, options);
参数
-
事件名称,相同的会传入
webSocket.on(event)。 -
optionsOrPredicatefunction | Object (optional)#-
predicatefunction接收事件数据并在等待解决时解析为真值。
-
timeoutnumber (optional)等待的最长时间,以毫秒为单位。默认值为
0——无超时。默认值可以通过配置中的actionTimeout选项更改,或使用 browserContext.setDefaultTimeout() 或 page.setDefaultTimeout() 方法更改。
可以是接收事件的谓词,也可以是一个选项对象。可选。
-
-
optionsObject (optional)
返回
事件
🌐 Events
on('close')
Added before v1.9当 websocket 关闭时触发。
🌐 Fired when the websocket closes.
用法
webSocket.on('close', data => {});
事件数据
on('framereceived')
Added in: v1.9当 websocket 收到帧时触发。
🌐 Fired when the websocket receives a frame.
用法
webSocket.on('framereceived', data => {});
事件数据
on('framesent')
Added in: v1.9当 websocket 发送帧时触发。
🌐 Fired when the websocket sends a frame.
用法
webSocket.on('framesent', data => {});
事件数据
on('socketerror')
Added in: v1.9当 websocket 出现错误时触发。
🌐 Fired when the websocket has an error.
用法
webSocket.on('socketerror', data => {});
事件数据