Multithreaded qwebsocket ConnectingState forever?

  c++, qt, qt5, qthread

QT 5.12.11

msvc2017 32bit

I created a subclass that inherits from qthread.

And use qwebsocket in the custom thread.

But it cannot connect to server, and the qwebsocket’s state is always QAbstractSocket::ConnectingState.

I can only use another way like qthread example in assistant and this is work good (But I need custom qthread, not like this):

**

    QThread workerThread_websocket;
    WSocketClient* websocketObj=new WSocketClient(WEBSOCKET_VIDEO_UPLOAD);// only qobject
    websocketObj->moveToThread(&workerThread_websocket);
    QObject::connect(&workerThread_websocket, &QThread::started, 
        websocketObj, &WSocketClient::startConnection);// create qwebsocket ptr
    workerThread_websocket.start();

**

Does anybody know what’s wrong?

Problem code:

**

    class TestWebSocket : public QThread
    {
        Q_OBJECT
    public:
        TestWebSocket()
        {
            m_client = NULL;
        }
    protected:
        void run()override
        {
            m_client = new QWebSocket();
            m_client->open(QUrl(WEBSOCKET_VIDEO_UPLOAD));
            while(true)
            {
                qDebug()<<"TestWebSocket state:"<<m_client->state();//QAbstractSocket::ConnectingState forever
                msleep(500);
            }
        }
        QWebSocket* m_client;
    };

**

Source: Windows Questions C++

LEAVE A COMMENT