本页内容

WebSocket QML Type

QWebSocket 的 QML 接口。更多内容...

Import Statement: import QtWebSockets 1.12

属性

信号

方法

详细说明

WebSockets 是一项通过单个 TCP 连接提供全双工通信通道的 Web 技术。WebSocket 协议于 2011 年由 IETF 作为RFC 6455进行标准化。

属性文档

active : bool

当设置为 true 时,将使用指定的 URL 连接到服务器。当设置为 false 时,将关闭连接。默认值为 false。

errorString : string [read-only]

包含对最近发生的错误的描述。如果未发生错误,则该字符串为空。

negotiatedSubprotocol : string [read-only, since 6.4]

已与服务器协商好的 WebSocket 子协议。

该属性在 Qt 6.4 中引入。

requestedSubprotocols : list<string> [since 6.4]

WebSocket 握手过程中要发送的 WebSocket 子协议列表。

该属性于 Qt 6.4 版本中引入。

status : Status [read-only]

WebSocket 的状态。

该状态可能具有以下值:

  • WebSocket.Connecting
  • WebSocket.Open
  • WebSocket.Closing
  • WebSocket.Closed
  • WebSocket.Error

url : url

要连接的服务器 URL。该 URL 必须采用以下两种方案之一:ws://或wss://。如果未指定,则默认使用ws://。

Signal 文档

binaryMessageReceived(QString message)

当接收到二进制消息时,会发出此信号。message 包含接收到的字节。

注意: 相应的处理程序 是 `onBinaryMessageReceived`。

[since 6.10] pong(quint64 elapsedTime, ArrayBuffer payload)

当收到对先前 ping 请求的响应(pong 消息)时触发。elapsedTime 包含往返时间(单位:毫秒),payload 包含随 ping 请求一并发送的可选有效载荷。

注意: 对应的处理函数 为onPong 。

该信号在 Qt 6.10 中引入。

另请参阅 ping()。

statusChanged(Status status)

当 WebSocket 的状态发生变化时,会触发此信号。status 参数提供当前状态。

注意: 对应的处理程序 是onStatusChanged 。

另请参阅 WebSocket::status 。

textMessageReceived(QString message)

收到短信时会触发此信号。message 包含接收到的字节数据。

注意: 相应的处理程序 为onTextMessageReceived 。

方法文档

[since 6.10] void ping()

[since 6.10] void ping(ArrayBuffer payload)

向服务器发送ping请求,以表明连接仍处于活动状态。可在ping消息中附加一个payload 。

payload 的大小不能超过125字节。如果超过此限制,payload 将被截断为125字节。

注意: QWebSocket 和QWebSocketServer 会在内部处理 ping 请求,这意味着它们会自动向对端发送 pong 响应。

这些方法在 Qt 6.10 中引入。

另请参阅 pong()。

void sendBinaryMessage(ArrayBuffer message)

将参数message 发送至服务器。

void sendTextMessage(string message)

将message 发送至服务器。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.