Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Websockets newConnect signal doesn't seems to be emitted in my own class

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

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 388 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marianexyx
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved