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)
expect_event
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.
使用方式
web_socket.expect_event(event)
web_socket.expect_event(event, **kwargs)
參數
-
Event name, same one would pass into
webSocket.on(event)
. -
predicate
Callable (optional)#Receives the event data and resolves to truthy value when the waiting should resolve.
-
Maximum time to wait for in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().
傳回值
wait_for_event
Added before v1.9In most cases, you should use web_socket.expect_event().
Waits for given event
to fire. If predicate is provided, it passes event's value into the predicate
function and waits for predicate(event)
to return a truthy value. Will throw an error if the socket is closed before the event
is fired.
使用方式
web_socket.wait_for_event(event)
web_socket.wait_for_event(event, **kwargs)
參數
-
Event name, same one typically passed into
*.on(event)
. -
predicate
Callable (optional)#Receives the event data and resolves to truthy value when the waiting should resolve.
-
Maximum time to wait for in milliseconds. Defaults to
30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().
傳回值
屬性 (Properties)
is_closed
Added before v1.9Indicates that the web socket has been closed.
使用方式
web_socket.is_closed()
傳回值
url
Added before v1.9Contains the URL of the WebSocket.
使用方式
web_socket.url
傳回值
事件 (Events)
on("close")
Added before v1.9Fired when the websocket closes.
使用方式
web_socket.on("close", handler)
事件資料
on("framereceived")
Added in: v1.9Fired when the websocket receives a frame.
使用方式
web_socket.on("framereceived", handler)
事件資料
on("framesent")
Added in: v1.9Fired when the websocket sends a frame.
使用方式
web_socket.on("framesent", handler)
事件資料
on("socketerror")
Added in: v1.9Fired when the websocket has an error.
使用方式
web_socket.on("socketerror", handler)
事件資料