Nominate our 2022 Qt Champions!

What does the method Connect do in websockets programming?

  • I am facing difficulty in understanding the following code snippet
    connect(&m_webSocket, &QWebSocket::connected, this, &qtWebSocketClient::onConnected);

    Can somebody explain what is happening here?

    I am trying to control the connect,send,rcv aspects of the client - server application externally.

  • @Mohankumar_S

    You should have a look to signal and slots
    The connect routine is connecting the signal QWebSocket::connected to the slot qtWebSocketClient::onConnected.

    The naming becomes a bit fusing when a signal is connected to something which reports basically the success of a completely unrelated connection (e.g. within the internet).

  • Also I believe you do have an object of the m_webSocket class created , and this is set to the current class (for me a lot of the time that is the mainwindow class). So the ' &QWebSocket::connected SIGNAL comes from the m_webSocket class '
    and the ' &qtWebSocketClient::onConnected SLOT comes from this (the current class object) '.

  • @koahnig Thank you

  • @koahnig How to serve multiple client requests through a single server?

  • @Mohankumar_S

    It may depend on the server implementation. Especially you need to check the web socket protocol. I have no experience there.

    In general you can send a couple of requests subsequently and wait for the corresponding replies. If the server does not maintain a stack for requests, you have to wait for each reply and send the next request. However, this is dependent on the server implementation.

    Have a look to the websocket examples. There might be one already close to what you try to do.

  • @koahnig Thank you

  • @koahnig I am getting the following linking error
    /home/mohan/QtWebSocketServer/qtwebsocketserver.cpp:-1: error: undefined reference to QHostAddress::QHostAddress(QHostAddress::SpecialAddress)' /home/mohan/QtWebSocketServer/qtwebsocketserver.cpp:-1: error: undefined reference toQWebSocketServer::listen(QHostAddress const&, unsigned short)'
    /home/mohan/QtWebSocketServer/qtwebsocketserver.cpp:-1: error: undefined reference to `QWebSocketServer::newConnection()'

    These are my include files
    #include "qtwebsocketserver.h"
    #include "QtWebSockets/qwebsocketserver.h"
    #include "QtWebSockets/qwebsocket.h"
    #include <QtCore/QDebug>
    QTime t;

    This is my project file
    QT += core
    QT -= gui

    CONFIG += c++11

    TARGET = QtWebSocketServer
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    SUBDIRS +=


    HEADERS +=

  • Lifetime Qt Champion

    As the documentation says you need this line in your pro file in order to use network functionality:

    QT += network

Log in to reply