Hello!

If you are using a QWebSocketServer you can call close() when a new connection 
has been established and call listen() again when the currently-connected 
socket disconnects.

Attached is a toy example of doing so.

Mårten

PS: Apologies for double mail to Alexander, pressed Reply instead of Reply 
all...

________________________________________
From: Interest <interest-boun...@qt-project.org> on behalf of "Alexander Carôt" 
<alexander_ca...@gmx.net>
Sent: Monday, May 11, 2020 10:13
To: qt qt
Subject: [Interest] QWebSocket limit connection

Hello all,

I am using a QWebSocket in order to communicate between a browser and an 
application which works fine.

In my specific use case I want to prevent more than one browser to connect to 
the application.

Is there a way to decline further connections after one has been established ?

Thanks in advance,
best

Alex


--
http://www.carot.de
Email : alexan...@carot.de
Tel.: +49 (0)177 5719797

_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest
#include <QCoreApplication>
#include <QWebSocketServer>
#include <QWebSocket>
#include <QHostAddress>

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    QWebSocketServer server("hi", QWebSocketServer::NonSecureMode);
    server.setMaxPendingConnections(1);
    server.listen(QHostAddress::Any, 24242);
    QObject::connect(&server, &QWebSocketServer::newConnection, [&server]() {
        QWebSocket *socket = server.nextPendingConnection();

        QObject::connect(socket, &QWebSocket::textMessageReceived, [](const 
QString &message) {
            qDebug() << message;
        });

        QObject::connect(socket, &QWebSocket::disconnected, [&server]() {
            server.listen(QHostAddress::Any, 24242);
        });

        server.close();
    });

    return app.exec();
}
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to