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

QTcpSocket and WebAssemblity trouble



  • Hi guys. I built QTcpSocket example
    https://github.com/manfredipist/QTcpSocket.git
    with WebAssemblity.
    Unfortunately on start I receive error
    000The following error occurred: Network operation timed out.

    Same code with MSVC build work perfect.

    An important part of the code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        socket = new QTcpSocket(this);
    
        connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage);
        connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket);
        connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket);
        connect(socket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
    
        socket->connectToHost("192.168.1.222", 8080);
    
       if(socket->waitForConnected())
            ui->statusBar->showMessage("Connected to Server");
        else{
            QMessageBox::critical(this,"QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString()));
            exit(EXIT_FAILURE);
        }
    }
    
    MainWindow::~MainWindow()
    {
        if(socket->isOpen())
            socket->close();
        delete ui;
    }
    
    void MainWindow::readSocket()
    {
        ...
    }
    
    void MainWindow::discardSocket()
    {
        socket->deleteLater();
        socket=nullptr;
    
        ui->statusBar->showMessage("Disconnected!");
    }
    
    void MainWindow::displayError(QAbstractSocket::SocketError socketError)
    {
        switch (socketError) {
            case QAbstractSocket::RemoteHostClosedError:
            break;
            case QAbstractSocket::HostNotFoundError:
                QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings.");
            break;
            case QAbstractSocket::ConnectionRefusedError:
                QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct.");
            break;
            default:
                QMessageBox::information(this, "QTCPClient", QString("000The following error occurred: %1.").arg(socket->errorString()));
            break;
        }
    }
    

    Any ideas on how to fix this?
    Thanks.



  • @Bleach said in QTcpSocket and WebAssemblity trouble:

    Hi guys. I built QTcpSocket example
    https://github.com/manfredipist/QTcpSocket.git
    with WebAssemblity.
    Unfortunately on start I receive error
    000The following error occurred: Network operation timed out.
    Same code with MSVC build work perfect.

    AFAIK QtWebAssembly does not support QTcpSocket, only QWebSocket are possible (sandboxing issues).


  • Lifetime Qt Champion

    Does some process listening on 192.168.1.222:8080?



  • @Bleach said in QTcpSocket and WebAssemblity trouble:

    Hi guys. I built QTcpSocket example
    https://github.com/manfredipist/QTcpSocket.git
    with WebAssemblity.
    Unfortunately on start I receive error
    000The following error occurred: Network operation timed out.
    Same code with MSVC build work perfect.

    AFAIK QtWebAssembly does not support QTcpSocket, only QWebSocket are possible (sandboxing issues).



  • @Christian-Ehrlicher No.

    @KroMignon Thanks. I'll try to use QWebSocket.



  • Qt Webassembly cannot block the main thread, so normal Qt socket classes still do not work correctly, as is. There is some work being done to get them working better with a threaded version of Qt WebAssembly, but it is still fairly WIP and not scheduled for any Qt version yet. Even so, some things like select() and poll() will not work.

    Best to use websockets, as any posix sockets get translated into websockets, unless you use a proxy server method mentioned in:

    https://emscripten.org/docs/porting/networking.html


Log in to reply