How to send array using QWebSocket



  • Hi everybody!
    I created a client-server system comprising:

    • A node.js server (with module ws);
    • A WebClient;
    • A QtClient (using Qt5.4 and QWebSocket).

    The QtClient sends and receives strings via the method * QWebSocket.sendTextMessage (QString s)*. How can I send an array of strings?

    The WebClient send array using JSON:

    # index.html (WebClient)
    socket.onopen = function() {  
            var array = {
                value1: "WebClient value1 = v1", 
                value2: "WebClient value2 = v2"
            };
            socket.send(JSON.stringify(array), {binary: true, mask: false}); 
    };
    
    # server.js
    socket.on('connection', function(ws) {
      ws.on('message', function(message) {
        var array = JSON.parse(message);
        console.log(array["value1"]);
        console.log( array["value2"]);
      });
    });
    
    # console node
    C:\Users\PietroP\Desktop\cs\v0.3>node server.js
    Server connect on http://192.168.1.60:3000/
    a user connected
    WebClient value1 : v1
    WebClient value2 : v2
    

    Thanks for your support! :)



    • You can also send array using JSON,for Details.You can look up The QWebsocket Class
    • I develop a cross-platform app by Qt/QML,I also use websocket in my client.and my server use nodejs and websocket, this is my site :www.heilqt.com

  • Moderators

    @TobyYi We would appreciate if you post some example codes/solution here instead of advertising your blog.



  • Thank you for your advice! I have simplified: I converted the array into a single string


Log in to reply
 

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