Websockets newConnect signal doesn't seems to be emitted in my own class



  • Everything works fine when I wrote code exactly like it is in Websocket Echo Server example: http://doc.qt.io/qt-5/qtwebsockets-echoserver-echoserver-cpp.html. But when I move code to my own "Websokety" class, then it seems that the signal isn't emitted, or I can't catch it. I've made a class only for Webosckets:

    main.cpp:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow server(1234); 
        server.show();
    
        return a.exec();
    }
    
    

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "websokety.h"
    
    namespace Ui 
    {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
    public:
        explicit MainWindow(quint16 port, QWidget *parent = 0);
        virtual ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(quint16 ws_port, QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        Websokety WebsoketySerwer(ws_port);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    websokety.h:

    #ifndef WEBSOKETY_H
    #define WEBSOKETY_H
    
    #include <QObject>
    #include <QtCore/QObject>
    #include <QtCore/QList>
    #include <QtCore/QByteArray>
    #include "QtWebSockets/QWebSocketServer"
    #include "QtWebSockets/QWebSocket"
    
    QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
    QT_FORWARD_DECLARE_CLASS(QWebSocket)
    
    class Websokety: public QObject
    {
        Q_OBJECT
    
    public:
        Websokety(quint16 port);
    
        QWebSocketServer *m_pWebSocketServer;
        QList<QWebSocket *> m_clients;
    
    public Q_SLOTS:
        void onNewConnection();
    };
    
    #endif // WEBSOKETY_H
    
    

    websokety.cpp:

    #include "websokety.h"
    
    Websokety::Websokety(quint16 port):
        m_pWebSocketServer(Q_NULLPTR),
        m_clients()
    {
        m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Chat Server"),
                                                  QWebSocketServer::NonSecureMode,
                                                  this);
        if (m_pWebSocketServer->listen(QHostAddress::Any, port))
        {
            qDebug() << "Server listening on port " << QString::number(port) << "\n";
            connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
                    this, &Websokety::onNewConnection);
        }
    }
    
    void Websokety::onNewConnection()
    {
        qDebug() << "New websocking connection!";
    }
    
    

    Everything works fine when I merge back my own "Websokety" class into Mainwindow, and I receive message on qDebug: "New websocking connection!". What am I doing wrong?


  • Lifetime Qt Champion

    Hi,

    You declare a local Websokety variable in your MainWindow constructor, it will get out of scope at the end of the constructor and thus get destroyed at that moment.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.