Integrate QT client in Socket.io demo



  • Hello everyone!
    I am a beginner and would like to understand how to realize this small application, using node.js, socket.io and QT.

    I made a server-side chat, following the tutorial on socket.io: I created a server using node.js and going to http://localhost:3000 I can access the webclient.
    Now I would like to create a desktop client (QT and C ++) that exploits the same server nodejs. How can I get this? [sketch]

    I found QtSocketIo (a conversion of socket.io for QT) but do not get how to use it in my project. Launching the example, in echoserver.h I get this error: "Project ERROR: Unknown module(s) in QT: socketio".

    // echoserver.h in QtSocketIo https://github.com/KurtPattyn/QtSocketIo.git
    
    #ifndef ECHOCLIENT_H
    #define ECHOCLIENT_H 
    
    #include <QtCore/QObject>
    #include <QtSocketIo/QSocketIoClient> // NOT FOUND
    
    class EchoClient : public QObject
    {
        Q_OBJECT
    public:
        explicit EchoClient(QObject *parent = Q_NULLPTR);
        virtual ~EchoClient();
    
        void open(QUrl url);
    
    public Q_SLOTS:
        void messageReceived(QString message);
        void errorReceived(QString reason, QString advice);
        void ackReceived(int messageId, QJsonArray arguments);
        void connected(QString endpoint);
        void disconnected(QString endpoint);
        void heartbeatReceived();
    
    private:
        QSocketIoClient m_client;
    };
    
    #endif // ECHOCLIENT_H
    

    I am sorry for my bad English. Thank you all! :)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you first build and install that library ?



  • @SGaist Thanks for your reply!
    I tried to import the QtSocketIo/src /socketio/socketio.pro but .cpp and .h files are not loaded. I'm have this error:
    error: Missing CMake tests


  • Lifetime Qt Champion

    You should also get the code from the pull request



  • @SGaist Sorry if I reply now. What do you mean?
    If you want to give me an advice or suggestions. They are welcome.
    My focus is to have a client-server model with a client in QT and a WebClient.

    Thanks again for your time!


  • Lifetime Qt Champion

    Sorry, I meant that you would need that pull request to build the QtSocketIo module



  • @SGaist Thank You! Other people have had the same problem, without any replies.
    I have found a workaround: I used normal WebSockets (not socket.io).
    wp for node.js: https://www.npmjs.com/package/ws
    QWebSockets for Qt: http://doc.qt.io/qt-5/qwebsocket.html

    Example:

    // mainwindow.cpp (QtClient)
    void MainWindow::onConnected()
    {
        connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &MainWindow::onTextMessageReceived);
        m_webSocket.sendTextMessage(QStringLiteral("Hello World! - from QtClient"));
    }
    
    // index.html (WebClient)
    socket.onopen = function() {
        socket.send("Hello World! - from WebClient"); 
    };
    // server.js
    socket.on('connection', function(ws) {
      ws.on('message', function(message) {
        var m = JSON.parse(message);
        console.log(m);
      });
    });
    
    // console node
    C:\Users\PietroP\Desktop\cs\v0.3>node server.js
    Server connect on http://192.168.1.60:3000/
    a user connected
    Hello World! - from WebClient
    a user connected
    Hello World! - from QtClient
    

    Thanks for all!


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please use the Topic Tool button to mark the thread as solved so other forum users may know a solution has been found :)



  • @SGaist Hello! In TopicTool I only see the "delete" button!


  • Lifetime Qt Champion

    Then just edit the title by hand and prepend [solved]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.