Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebSocket closeCode always 1000 and QAbstractSocket::RemoteHostClosedError



  • example server:

    wsstestsrv::wsstestsrv(QObject *parent)
    	: QObject(parent)
    {
    	srv = new QWebSocketServer(QStringLiteral("test"), QWebSocketServer::SecureMode, this);
    	srv->setSslConfiguration(sslConf);
    
    	if (srv->listen(QHostAddress::AnyIPv4, 13000))
    	{
    		qDebug() << QString("Listen on: %1 port: %2").arg(srv->serverAddress().toString()).arg(srv->serverPort());
    		connect(srv, &QWebSocketServer::newConnection, this, &wsstestsrv::onNewConnection);
    	}
    	else
    		qDebug() << srv->errorString();
    }
    
    void wsstestsrv::onNewConnection()
    {
    	QWebSocket *socket = srv->nextPendingConnection();
    
    	connect(socket, &QWebSocket::connected, [=]() { qDebug() << "connected!"; });
    	connect(socket, &QWebSocket::disconnected, [=]() { qDebug() << "disconnected!" << socket->closeCode();  });
    
    	typedef void (QWebSocket:: *sslErrorsSignal)(const QList<QSslError> &);
    	connect(socket, static_cast<sslErrorsSignal>(&QWebSocket::sslErrors),
    		[&](const QList<QSslError> &errors) {
    		for (const QSslError &e : errors)
    			qDebug() << e; });
    	connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [&](QAbstractSocket::SocketError e) {qDebug() << e; });
    }
    

    example client:

    wssclient::wssclient(QObject *parent)
    	: QObject(parent)
    {
    	socket = new QWebSocket();
    	connect(socket, &QWebSocket::connected, [&]() { qDebug() << "connected!"; });
    	connect(socket, &QWebSocket::disconnected, [&]() { qDebug() << "disconnected!"; });
    
    	typedef void (QWebSocket:: *sslErrorsSignal)(const QList<QSslError> &);
    	connect(socket, static_cast<sslErrorsSignal>(&QWebSocket::sslErrors),
    		[&](const QList<QSslError> &errors) {
    		for (const QSslError &e : errors)
    			qDebug() << e;
    		socket->ignoreSslErrors(); });
    	connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [&](QAbstractSocket::SocketError e) {qDebug() << e; });
    
    	socket->open(QUrl("wss://127.0.0.1:13000"));
    
    	QTimer::singleShot(3000, [&]() {socket->close(QWebSocketProtocol::CloseCodeGoingAway); });
    }
    

    Hi,
    First problem:
    After remote client close, server socket randomly emit QAbstractSocket::RemoteHostClosedError (not every close)
    Second:
    closedCode always 1000(NORMAL)

    any advices how to fix this?

    P.S.(Qt 5.10/Windows 10 Pro)



  • @Xardas You find solution for this problem?
    We has the same local ws and websocket closed with error RemoteHostClosedError


Log in to reply