Verwenden Sie denselben Port für http und WebSocket.
Ich hatte vor einiger Zeit ein Problem, das im QTBUG-121562 beschrieben wird (QHttpServer gibt keine Emission aus neue WebSocket-Verbindung() zu myclass::myslot(), die (bis Qt 6.7.x) mit einer von Ievgenii Meshcheriakov vorgeschlagenen Lösung gelöst wurde:
Aber jetzt (Qt6.8.0) habe ich beim Kompilieren den Fehler erhalten und funktioniert nicht mehr:
Statische Assertion fehlgeschlagen: ViewHandler-Argumente Fehler: QHttpServerResponder kann nur als Referenz übergeben werden .
Ich habe das in Qt 6.8.0 gefunden:
Code: Select all
httpServer->route("/ws/", [](QHttpServerResponder &respond) {
//httpServer->route("/ws/", [](QHttpServerResponder &&respond) { // here compiler gives error in Qt 6.8.0
auto _respond = std::move(respond);
});
Ich weiß nicht, ob es ein Fehler oder nur eine Änderung ist So funktioniert Qt6.8.0.
Der Beispielcode unten läuft einwandfrei in Qt 6.7.2, aber NICHT in 6.8.0 und 6.8.1.
Überprüfen Sie ===>> #Wenn (QT_VERSION < QT_VERSION_CHECK(6, 8, 0))
Ports sind 4445 (nur Websocket) und 8001 (http und Websocket gleicher Port).
Verwenden Sie localhost:8001 für http Server (index.html) und das Projekt muss auf den Hauptprojektordner verweisen.
httpsocketlistener.h
Code: Select all
#ifndef HTTPSOCKETLISTENER_H
#define HTTPSOCKETLISTENER_H
#include
#include
#include
#include
#include
#include
class HttpSocketListener : public QObject
{
Q_OBJECT
public:
explicit HttpSocketListener(QObject *parent = nullptr);
~HttpSocketListener();
#if (QT_VERSION < QT_VERSION_CHECK(6, 8, 0))
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
#else
quint16 listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
#endif
void addRoutes(QHttpServer * httpServer);
protected:
signals:
void clientConnected(QWebSocket * newWebSocketConnection);
public slots:
void incomingConnection();
private:
QHttpServer * m_pHttpSocketServer;
};
#endif // HTTPSOCKETLISTENER_H
Code: Select all
#include "httpsocketlistener.h"
#include
#include
#include
#include
HttpSocketListener::HttpSocketListener(QObject *parent)
: QObject{parent}
{
qDebug() listen(address, port) || !m_pHttpSocketServer->bind(tcpserver.get())) {
qDebug() route("/ws/", [](QHttpServerResponder &&respond) {
//httpServer->route("/ws/", [](QHttpServerResponder &respond) { // this compiles, but it does not emit the signal
auto _respond = std::move(respond);
});
#endif
httpServer->route("/json/", [] {
return QJsonObject{
{
{"key1", "1"},
{"key2", "2"},
{"key3", "3"}
}
};
});
httpServer->route("/api/", [] {
return QJsonObject{
{
{"Api_1", "1"},
{"Api_2", "2"},
{"Api_3", "3"}
}
};
});
}
Code: Select all
#ifndef WEBSOCKETLISTENER_H
#define WEBSOCKETLISTENER_H
#include
#include
#include
#include
class WebSocketListener : public QObject
{
Q_OBJECT
public:
explicit WebSocketListener(QObject *parent = 0);
~WebSocketListener();
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
protected:
signals:
void clientConnected(QWebSocket * newWebSocketConnection);
public slots:
void incomingConnection();
private:
QPointer m_pWebSocketServer;
};
#endif // WEBSOCKETLISTENER_H
Code: Select all
#include "websocketlistener.h"
WebSocketListener::WebSocketListener(QObject *parent) :
QObject(parent)
{
qDebug() nextPendingConnection();
qDebug() listen( QHostAddress::AnyIPv4, websocketlistenPort ) ){
qDebug() listen( QHostAddress::AnyIPv4, httpsocketlistenPort ) ){
qDebug()