Wednesday, 31 May 2017

Use socketIO.emit in external controller client side

I have the following problem. I want to separate all socket.emit functions from the client into a separate controller class so I can call the Controller functions and receive the response:

app/components/client.js

let response = NodeController.root(path);
console.log('response', response);
if(response){
    var projectInfoId = JSON.parse(response);
}

app/controller/node-controller.js

class NodeController {
    static root(data){
        let tmp;
        socket.emit('/', data, function (response) {
            tmp = response;
        });
        return tmp;
    }
}

The projectInfoId in client.js will be undefined because the response is undefined. If i call socket.emit() directly in the client with the callback function, I will receive the data und my projectInfoId will contain something.

Is it possblie to sepearte the socket functions from the client.js code or do I have to call it directly in the client.js file?



via Morlord

No comments:

Post a Comment