有 Qt 的 WebSocket 示例吗?

发布于 2024-11-01 20:28:52 字数 28 浏览 1 评论 0原文

有 Qt 的 WebSocket 示例吗?

Is there an example WebSockets for Qt?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-11-08 20:28:52

您还可以查看 QtWebSockets。 QtWebSockets 可用于客户端和服务器应用程序,并通过了 Autobahn 测试套件。

You can also have a look at QtWebSockets. QtWebSockets can be used both for client and server applications, and passes the Autobahn test suite.

凉风有信 2024-11-08 20:28:52

QtWebKit 内部的 QWebView 支持使用 Web Sockets(来自 HTML5 标准)。我已经使用它们多次,没有任何问题。

QWebView inside of QtWebKit supports the use of Web Sockets (from the HTML5 standard). I have used them multiple times without any issue.

再浓的妆也掩不了殇 2024-11-08 20:28:52

我创建了一个例子。以下是代码:

inspection_server.hpp:

#ifndef __INSPECTION_SERVER_HPP__
#define __INSPECTION_SERVER_HPP__

#include <QWebSocketServer>
#include <QWebSocket>
#include <QObject>
#include <iostream>
#include <memory>

class InspectionServer;

typedef std::shared_ptr<QWebSocketServer> QWebSocketServerPtr;
typedef std::shared_ptr<QWebSocket> QWebSocketPtr;
typedef std::shared_ptr<InspectionServer> InspectionServerPtr;

class InspectionServer: public QObject
{
    Q_OBJECT

    QWebSocketServerPtr websocketServer;
    QList<QWebSocketPtr> clients;

public:
    InspectionServer(uint16_t port);

signals:
    void closed();

private slots:
    void onNewConnection();
    void processTextMessage(const QString& message);
    void socketDisconnected();

};

#endif

inspection_server.cpp:

#include "inspection_server.hpp"

#include <QDebug>

InspectionServer::InspectionServer(uint16_t port)
    : websocketServer(new QWebSocketServer(QStringLiteral("Inspection server"), QWebSocketServer::NonSecureMode))
{
    if(this->websocketServer->listen(QHostAddress::Any, port))
    {
        QObject::connect(websocketServer.get(), SIGNAL(newConnection()), this, SLOT(onNewConnection()));
    }
    else
    {
        throw std::runtime_error("InspectionServer: failed to listen");
    }
}

void InspectionServer::onNewConnection()
{
    qInfo() << "InspectionServer::onNewConnection";
    QWebSocketPtr socket(this->websocketServer->nextPendingConnection());

    QObject::connect(socket.get(), SIGNAL(textMessageReceived(const QString&)), this, SLOT(processTextMessage(const QString&)));
    QObject::connect(socket.get(), SIGNAL(disconnected()), this, SLOT(socketDisconnected()));

    this->clients.append(socket);
}

void InspectionServer::processTextMessage(const QString& message)
{
    qInfo() << "InspectionServer::processTextMessage: " << message;
}

void InspectionServer::socketDisconnected()
{
    qInfo() << "InspectionServer::socketDisconnected";
}

main.cpp:

#include "inspection_server.hpp"

#include <QCoreApplication>

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    InspectionServer server(1234);

    return app.exec();
}

以下是如何在 Web 浏览器中测试该代码:

<html>
<body>
<button onClick="initWebSocket();">Connect</button>
<br>
<input type="text" id="messageInput">
<button onClick="sendMessage();">Send message</button>
</body>
</html>

<script type="text/javascript">

var websocket = null;

function initWebSocket()
{
    websocket = new WebSocket("ws://localhost:1234");
}

function sendMessage()
{
    websocket.send(document.getElementById("messageInput").value);
}

</script>

这是一个很好的参考链接:
http://code.qt.io/cgit/ qt/qtwebsockets.git/tree/examples/websockets/echoserver

I have created an example. Here is the code:

inspection_server.hpp:

#ifndef __INSPECTION_SERVER_HPP__
#define __INSPECTION_SERVER_HPP__

#include <QWebSocketServer>
#include <QWebSocket>
#include <QObject>
#include <iostream>
#include <memory>

class InspectionServer;

typedef std::shared_ptr<QWebSocketServer> QWebSocketServerPtr;
typedef std::shared_ptr<QWebSocket> QWebSocketPtr;
typedef std::shared_ptr<InspectionServer> InspectionServerPtr;

class InspectionServer: public QObject
{
    Q_OBJECT

    QWebSocketServerPtr websocketServer;
    QList<QWebSocketPtr> clients;

public:
    InspectionServer(uint16_t port);

signals:
    void closed();

private slots:
    void onNewConnection();
    void processTextMessage(const QString& message);
    void socketDisconnected();

};

#endif

inspection_server.cpp:

#include "inspection_server.hpp"

#include <QDebug>

InspectionServer::InspectionServer(uint16_t port)
    : websocketServer(new QWebSocketServer(QStringLiteral("Inspection server"), QWebSocketServer::NonSecureMode))
{
    if(this->websocketServer->listen(QHostAddress::Any, port))
    {
        QObject::connect(websocketServer.get(), SIGNAL(newConnection()), this, SLOT(onNewConnection()));
    }
    else
    {
        throw std::runtime_error("InspectionServer: failed to listen");
    }
}

void InspectionServer::onNewConnection()
{
    qInfo() << "InspectionServer::onNewConnection";
    QWebSocketPtr socket(this->websocketServer->nextPendingConnection());

    QObject::connect(socket.get(), SIGNAL(textMessageReceived(const QString&)), this, SLOT(processTextMessage(const QString&)));
    QObject::connect(socket.get(), SIGNAL(disconnected()), this, SLOT(socketDisconnected()));

    this->clients.append(socket);
}

void InspectionServer::processTextMessage(const QString& message)
{
    qInfo() << "InspectionServer::processTextMessage: " << message;
}

void InspectionServer::socketDisconnected()
{
    qInfo() << "InspectionServer::socketDisconnected";
}

main.cpp:

#include "inspection_server.hpp"

#include <QCoreApplication>

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    InspectionServer server(1234);

    return app.exec();
}

And here is how to test that in your Web browser:

<html>
<body>
<button onClick="initWebSocket();">Connect</button>
<br>
<input type="text" id="messageInput">
<button onClick="sendMessage();">Send message</button>
</body>
</html>

<script type="text/javascript">

var websocket = null;

function initWebSocket()
{
    websocket = new WebSocket("ws://localhost:1234");
}

function sendMessage()
{
    websocket.send(document.getElementById("messageInput").value);
}

</script>

This is a good link for reference:
http://code.qt.io/cgit/qt/qtwebsockets.git/tree/examples/websockets/echoserver

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文