qtqtwebsockets

Qt5 WebSockets test app not connecting to test service


I wanted to open a qt websocket to the test service in ws://echo.websocket.org but I got the error QAbstractSocket::RemoteHostClosedError I am connecting the signal error(QAbstractSocket::SocketError socketError) to a slot in my code in order to read the error number then look for it in here

My code looks like this

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Controller w;
    w.initializeWebSocket("ws://echo.websocket.org", true);
    w.show();

    return a.exec();
}






Controller::Controller(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

void Controller::initializeWebSocket(QString url, bool debug)
{
    m_webSocketURL = url;
    m_webSocketDebug = debug;
    if(m_webSocketDebug)
        std::cout << "WebSocket server: " << m_webSocketURL.toStdString() << std::endl;
    QObject::connect(&m_webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
    QObject::connect(&m_webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
    QObject::connect(&m_webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
    QObject::connect(&m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));
    m_webSocket.open(QUrl(m_webSocketURL));
}

void Controller::onConnected()
{
    if (m_webSocketDebug)
        std::cout << "WebSocket connected" << std::endl;
    m_webSocket.sendTextMessage(QStringLiteral("Rock it with HTML5 WebSocket"));
}

void Controller::onDisconnected()
{
    if (m_webSocketDebug)
        std::cout << "WebSocket disconnected" << std::endl;
}

void Controller::onError(QAbstractSocket::SocketError error)
{
    std::cout << error << std::endl;
}

void Controller::onTextMessageReceived(QString message)
{
    if (m_webSocketDebug)
        std::cout << "Message received:" << message.toStdString() << std::endl;
    m_webSocket.close();
}

Im new to websockets so I have no idea where could the problem be. Can anyone give advise?


Solution

  • Opening websocket at "ws://echo.websocket.org" works for me just fine.

    These handlers are sufficient in my project:

    connect(&webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
    connect(&webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
    connect(&webSocket, SIGNAL(textMessageReceived(const QString&)), this, SLOT(onTextMessageReceived(const QString&)));
    

    I also just realized that I don't connect error() signal yet the program code is quite reliable for more than a year already and in case of disconnect there is a connection restore kick in. Maybe I should connect error() signal as well for infrequent strange cases.

    The error QAbstractSocket::RemoteHostClosedError can be just a correct thing to get. Try to get the echo within reasonable time. The websocket farm we use in our project is holding the connection for up to 50 minutes so we do ping-pong between the client and the server to keep the connection live before this period expires.

        // you can try that immediately after opening the web socket and also using some QTimer
        m_webSocket.sendTextMessage("Pong!");
    

    Try that and see the text reply as long as you are playing some public echo service.