简体   繁体   中英

Node.js : how to handle callback with async.js and websocket?

I'm creating a REST API to store setting from a specific camera sending data through TCP. Camera communicate through TCP with a request/response pattern.
eg : You can send "Get rate\\n" to the camera and it responds "100 fps", in order to get the current framerate of the camera.

How to get data and store it in API?

var command = "rate"; //Array with setting I want to store in my API

function getDataFromCamera(command) { // This function will be iterate in another function
    async.series([
        console.log('Test1'); // console log to test output
        function(callback) {

            CAMERA_TCP_SOCKET.send("get "+command+"\n");
            callback(null,command);
},
     function(callback) {
         console.log('Test2'); // console log to test output
         CAMERA_TCP_SOCKET.onmessage = function(event) {
         callback(null, event.data); // THIS line is the problem. I can't retrieve event.data because i don't know how to callback this variable
     }
}],
     function(err, results) {
         if (err) {
        //Handle the error in some way. Here we simply throw it
             throw err;
         }
         if (results) {
             console.log(results); // expected output : // ['rate','100']
         }
    });
}

At the moment, I get this error:

Test1
Test2
/Users/maximecongi/Desktop/Hologram/node_modules/async/dist/async.js:966
        if (fn === null) throw new Error("Callback was already called.");
                         ^

Error: Callback was already called.
    at /Users/maximecongi/Desktop/Hologram/node_modules/async/dist/async.js:966:32
    at /Users/maximecongi/Desktop/Hologram/node_modules/async/dist/async.js:3885:13
    at W3CWebSocket.CAMERA_TCP_SOCKET.onmessage (/Users/maximecongi/Desktop/Hologram/core/api.js:91:13)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/maximecongi/Desktop/Hologram/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/Users/maximecongi/Desktop/Hologram/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/Users/maximecongi/Desktop/Hologram/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:188:13)
    at WebSocketConnection.processFrame (/Users/maximecongi/Desktop/Hologram/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /Users/maximecongi/Desktop/Hologram/node_modules/websocket/lib/WebSocketConnection.js:323:40
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
[nodemon] app crashed - waiting for file changes before starting...

How to solve my problem?

It's because you are listening on every message, and looks like you're sending 3 commands, and I guess, every command get a response. This callback after first serie is not destroyed, its alive. For me, you should check if the response is for this specific command, then execute the problematic callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM