本页内容

Qt WebChannel ChatClient HTML 示例

一个通过 WebSocket 与QWebChannel 服务器进行通信的 HTML/JavaScript 客户端。

一个浏览器窗口,其中包含一个“聊天登录”对话框,该对话框内设有“姓名”字段和“登录”按钮,旁边是一个“用户”面板

ChatClient HTML提供了一个使用 JavaScript 和 HTML 实现的简单QWebChannel 客户端。

运行示例

要运行此示例,请在浏览器中打开chatclient.html 文件。

实现 Web Channel 客户端

客户端会初始化与聊天服务器的 WebSocket 连接,并接收一个对象,该对象包含实现聊天客户端所需的所有信号、槽和属性。

登录后,客户端可以调用sendMessage 方法,并接收newMessage 信号以进行消息的发送和接收。此外,userList 属性提供了所有其他已连接客户端的名称。当列表内容发生变化时,该列表会自动更新。同时,客户端会响应服务器的 keep-alive 信号,该信号用于检测已断开连接的客户端,并将其从userList 属性中移除。

该示例演示了如何在QWebChannel 的客户端 JavaScript 实现中使用基本元素,例如订阅信号(newMessage )、调用插槽(sendMessage )以及处理属性变化(userList )。

客户端能够与Qt WebChannel ChatServer 示例配合使用。

示例项目 @ code.qt.io

另请参阅 Qt WebChannel 聊天服务器示例和Qt WebChannel ChatClient QML 应用程序。

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