Tuesday, 25 April 2017

Node.ja and Socket.io - how to send an array using the emit function

i'm using Node.js (Version 7.9.0) with socket.io(Version 1.7.3) and trying to send the arrays "softwareBez" and "softwareVersion" from the server to the client. Than the client should output the content of the array as text.

Here is my code:

---The server side---

var softwareBez = new Buffer(recTelegramm[2]);              // Create array with the length recTelegramm[2]
var softwareVersion = new Buffer(recTelegramm[3]);          // Create array with the length recTelegramm[3]

// Fill the array softwareBez
for(var i=0; i<recTelegramm[2]; i++)
{
    softwareBez[i] = recTelegramm[9+i];                     
}
//

// Fill the array softwareVersion 
for(var i=0; i<recTelegramm[3]; i++)
{
    softwareVersion[i] = recTelegramm[9+recTelegramm[2]+i]; 
}
//


    // Output for Tests
    console.log("softwareBez: ", softwareBez , "\n"); 
    console.log("softwareVersion: ", softwareVersion , "\n"); 

    // Send Data to Client
    io.sockets.emit('P_Sysinfo', {softwareBez: [softwareBez], softwareVersion: [softwareVersion]}); 

The output over console.log shows, that the arrays are filled correct.

---The client side---

$('#content').append
        (
            $('<li></li>').append($('<span>').text('Softwarebezeichnung: ' + data.softwareBez)),
            $('<li></li>').append($('<span>').text('Softwareversion: ' + data.softwareVersion))
        );

And the output in the Webbrowser is:

Softwarebezeichnung: [object ArrayBuffer]
Softwareversion [object ArrayBuffer]

So something seems to go wrong with the transmit of the arrays? Here I found something which I thought it was a solution: Are arrays allowed in the socket.io emit function? Therefore the "softwareBez: [softwareBez]" and "softwareVersion: [softwareVersion]" in the emit function (server side) is in the brackets "[" and "]". But with and without these brackets - the same problem....

Thanks for help



via Markus Wilde

No comments:

Post a Comment