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.
用法
¥Usage
WebSocket.isClosed();
返回
¥Returns
url
Added before v1.9包含 WebSocket 的 URL。
¥Contains the URL of the WebSocket.
用法
¥Usage
WebSocket.url();
返回
¥Returns
waitForFrameReceived
Added in: v1.10执行操作并等待帧发送。如果提供了谓词,它会将 WebSocketFrame 值传递给 predicate
函数,并等待 predicate(webSocketFrame)
返回真值。如果在接收帧之前关闭 WebSocket 或页面,则会抛出错误。
¥Performs 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
-
options
WebSocket.WaitForFrameReceivedOptions
(可选)¥
options
WebSocket.WaitForFrameReceivedOptions
(optional)setPredicate
Predicate<WebSocketFrame> (optional) Added in: v1.9#
接收 WebSocketFrame 对象,并在等待应该解决时解析为真值。
¥Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.
等待的最长时间(以毫秒为单位)。默认为 30000
(30 秒)。通过 0
禁用超时。可以使用 BrowserContext.setDefaultTimeout() 更改默认值。
¥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().
执行触发事件的操作的回调。
¥Callback that performs the action triggering the event.
返回
¥Returns
waitForFrameSent
Added in: v1.10执行操作并等待帧发送。如果提供了谓词,它会将 WebSocketFrame 值传递给 predicate
函数,并等待 predicate(webSocketFrame)
返回真值。如果在发送帧之前关闭 WebSocket 或页面,则会抛出错误。
¥Performs 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
-
options
WebSocket.WaitForFrameSentOptions
(可选)¥
options
WebSocket.WaitForFrameSentOptions
(optional)setPredicate
Predicate<WebSocketFrame> (optional) Added in: v1.9#
接收 WebSocketFrame 对象,并在等待应该解决时解析为真值。
¥Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.
等待的最长时间(以毫秒为单位)。默认为 30000
(30 秒)。通过 0
禁用超时。可以使用 BrowserContext.setDefaultTimeout() 更改默认值。
¥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().
执行触发事件的操作的回调。
¥Callback that performs the action triggering the event.
返回
¥Returns
事件
¥Events
onClose(handler)
Added before v1.9当 websocket 关闭时触发。
¥Fired when the websocket closes.
用法
¥Usage
WebSocket.onClose(handler)
事件数据
¥Event data
onFrameReceived(handler)
Added in: v1.9当 websocket 收到帧时触发。
¥Fired when the websocket receives a frame.
用法
¥Usage
WebSocket.onFrameReceived(handler)
事件数据
¥Event data
onFrameSent(handler)
Added in: v1.9当 websocket 发送帧时触发。
¥Fired when the websocket sends a frame.
用法
¥Usage
WebSocket.onFrameSent(handler)
事件数据
¥Event data
onSocketError(handler)
Added in: v1.9当 websocket 出现错误时触发。
¥Fired when the websocket has an error.
用法
¥Usage
WebSocket.onSocketError(handler)
事件数据
¥Event data