gpt4 book ai didi

c++ - QWebSocket Hello World示例

转载 作者:行者123 更新时间:2023-12-02 10:21:38 26 4
gpt4 key购买 nike

我正在尝试在项目中使用QWebSockets。我写了一个小的“Hello World”示例来测试它如何工作。问题:我可以启动WebSocket服务器,但是客户端将无法连接到它。我的客户端进入QAbstractSocket::ConnectingState,但从未真正连接。

#if !defined(RAVI_SERVER_WEBSOCKETCLIENT_HPP)
#define RAVI_SERVER_WEBSOCKETCLIENT_HPP

//Qt includes
//WebSocket
#include <QtWebSockets/QWebSocketServer>
#include <QtWebSockets/QWebSocket>
//Util
#include <QObject>
#include <QDebug>

namespace ravi{
namespace server{
class WebSocketClient : public QObject{
public:
WebSocketClient(const QUrl &url, QObject* parent = nullptr);
private slots:
void handle_new_connection();
void handle_close_connection();
void handle_new_text_message();
void handle_ssl_errors(const QList<QSslError> &errors);
QWebSocket _socket;
};
}
}

#endif // RAVI_SERVER_WEBSOCKETCLIENT_HPP
#include "WebSocketClient.hpp"

ravi::server::WebSocketClient::WebSocketClient(const QUrl &url, QObject* parent):
QObject(parent),
_socket(){
connect(&_socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
[=](QAbstractSocket::SocketError error){
qDebug() << "Error";
});
connect(&_socket, &QWebSocket::connected, this, &WebSocketClient::handle_new_connection);
connect(&_socket, &QWebSocket::disconnected, this, &WebSocketClient::handle_close_connection);
connect(&_socket, QOverload<const QList<QSslError>&>::of(&QWebSocket::sslErrors),
this, &WebSocketClient::handle_ssl_errors);
qDebug() << url.path();
qDebug() << url.port();
qDebug() << url.url();
qDebug() << url.host();
_socket.open(url);
qDebug() << _socket.state();
}


void ravi::server::WebSocketClient::handle_new_connection(){
qDebug() << "New Connection";

_socket.sendTextMessage(QStringLiteral("Hello, world!"));
connect(&_socket, &QWebSocket::textMessageReceived,
this, &WebSocketClient::handle_new_text_message);
}

void ravi::server::WebSocketClient::handle_close_connection(){
qDebug() << "Closed Connection";
}

void ravi::server::WebSocketClient::handle_new_text_message(){
qDebug() << "New Text Message";
}

void ravi::server::WebSocketClient::handle_ssl_errors(const QList<QSslError> &errors){
Q_UNUSED(errors);
qDebug() << "SSL";
_socket.ignoreSslErrors();
}
#include "source/server/WebSocketClient.hpp"
#include <iostream>
int main(int argc, char const *argv[])
{
ravi::server::WebSocketClient(QUrl("ws://127.0.0.1:8888"));
std::string line;
std::getline(std::cin, line);

}

#if !defined(RAVI_SERVER_WEBSOCKETSERVER_HPP)
#define RAVI_SERVER_WEBSOCKETSERVER_HPP

//Qt includes
//WebSocket
#include <QtWebSockets/QWebSocketServer>
#include <QtWebSockets/QWebSocket>
//Util
#include <QObject>
#include <QDebug>

namespace ravi{
namespace server{
class WebSocketServer : public QObject{
Q_OBJECT
public:
WebSocketServer(int port, QObject* parent = nullptr);
private:
void handle_new_connection();
void handle_close_connection();
void handle_text_message();
void handle_binary_message();
void handle_disconnect();

QWebSocketServer* _ws_server;
std::vector<QWebSocket*> _clients;
};
}
}

#endif // RAVI_SERVER_WEBSOCKETSERVER_HPP

#include "WebSocketServer.hpp"

ravi::server::WebSocketServer::WebSocketServer(int port, QObject* parent):
QObject(parent),
_ws_server(new QWebSocketServer("WebSocket Server", QWebSocketServer::NonSecureMode, this)){
if(_ws_server->listen(QHostAddress::Any, port)){
qDebug() << "Connect handlers";
connect(_ws_server, &QWebSocketServer::newConnection, this, &WebSocketServer::handle_new_connection);
connect(_ws_server, &QWebSocketServer::closed, this, &WebSocketServer::handle_close_connection);
}
if(_ws_server->isListening()){
qDebug() << "Still Listening";
}
}


void ravi::server::WebSocketServer::handle_new_connection(){
qDebug() << "New Connection";
QWebSocket *client = _ws_server->nextPendingConnection();

connect(client, &QWebSocket::textMessageReceived, this, &WebSocketServer::handle_text_message);
connect(client, &QWebSocket::binaryMessageReceived, this, &WebSocketServer::handle_binary_message);
connect(client, &QWebSocket::disconnected, this, &WebSocketServer::handle_disconnect);

_clients.push_back(client);
}

void ravi::server::WebSocketServer::handle_close_connection(){
qDebug() << "Closed Connection";
}

void ravi::server::WebSocketServer::handle_text_message(){

}

void ravi::server::WebSocketServer::handle_binary_message(){

}

void ravi::server::WebSocketServer::handle_disconnect(){

}



#include "source/server/WebSocketServer.hpp"
#include <iostream>

int main(int argc, char const *argv[])
{
auto ws_server = ravi::server::WebSocketServer(8888);

std::string line;
std::getline(std::cin, line);


return 0;
}


我看不到我在做什么错。以我的观点,这应该起作用(绝地是邪恶的)。
将不胜感激,将不胜感激。

最佳答案

您有2个错误:

  • 通过不将客户端对象分配给变量,则该对象立即被销毁。
  • Qt需要一个事件循环来使信号,事件等起作用,在这种情况下,一个QCoreApplication就足够了。
  • #include "source/server/WebSocketClient.hpp"

    #include <QCoreApplication>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    ravi::server::WebSocketClient client(QUrl("ws://127.0.0.1:8888"));
    return a.exec();
    }
    #include "source/server/WebSocketServer.hpp"

    #include <QCoreApplication>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    ravi::server::WebSocketServer server(8888);
    return a.exec();
    }

    另一方面,我感到奇怪的是 auto ws_server = ravi::server::WebSocketServer(8888);不会生成编译错误,因为作为WebSocketServer,QObject不应允许复制构造函数。

    关于c++ - QWebSocket Hello World示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59895192/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com