WebSocket QML Type

QWebSocket 的 QML 接口。更多

Import Statement: import QtWebSockets 1.10

属性

信号

方法

详细说明

WebSockets 是一种网络技术,可通过单个 TCP 连接提供全双工通信通道。2011 年,IETF 将 WebSocket 协议标准化为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 必须有两种方案之一:ws://wss://。如果未提供,则使用ws://。


信号文档

binaryMessageReceived(QString message)

message 包含接收到的字节。

注: 相应的处理程序是onBinaryMessageReceived


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

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 字节。如果大于 125 字节,payload 将被剪切为 125 字节。

注: QWebSocketQWebSocketServer 在内部处理 ping 请求,这意味着它们会自动向对等程序发回 pong 响应。

此 QML 方法在 Qt 6.10 中引入。

另请参阅 pong().


void sendBinaryMessage(ArrayBuffer message)

将参数message 发送给服务器。


void sendTextMessage(string message)

向服务器发送message


© 2025 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.