bricks/dist/docs/en/websocket.md
yumoqing 2e22085122 feat: 401后登录成功自动重试原始请求
- withLoginInfo 改为接收完整 opts(含 method/headers/params)
- 等待 login_window 的 destroy 事件(=登录成功信号)
- 登录成功后重试原始请求
- 重试仍401则返回null(避免死循环)
- 用户手动关闭登录窗口时也触发重试,401则返回null
2026-05-27 15:39:34 +08:00

2.0 KiB

WebSocket

Widget Functionality: Establishes a WebSocket connection with the backend, supports sending and receiving text and Base64-encoded audio/video data, and provides various event callbacks.
Type: Regular widget
Parent Widget: bricks.VBox

Initialization Parameters

Parameter Name Type Description
ws_url string The WebSocket server URL (e.g., ws://example.com/socket)
with_session boolean Whether to include current session information; if true, retrieves session from bricks.app.get_session() and passes it to the WebSocket constructor (Note: There is a typo in the source code — sessopn should be session)

Example:

new bricks.WebSocket({
  ws_url: "ws://localhost:8080/ws",
  with_session: true
});

Main Events

Event Name Trigger Condition
onopen Triggered when the WebSocket connection is successfully opened
onmessage Triggered when a message is received from the server (raw message)
onerror Triggered when an error occurs in the WebSocket connection
onclose Triggered when the WebSocket connection is closed
ontext Triggered when a message of type text is received (dispatched after parsing by on_message)
onbase64audio Triggered when a message of type base64audio is received
onbase64video Triggered when a message of type base64video is received

Other custom types can be dynamically dispatched via the ontypedata pattern, formatted as on + type.

Event Data Description

  • All event data dispatched after parsing in onmessage comes from JSON-formatted e.data, with the following structure:
    {
      "type": "text",
      "data": "Hello"
    }
    
    The widget automatically dispatches the corresponding event (e.g., ontext) based on the type field, and passes the data field as the parameter to the event handler.