I have a TCP socket on my server that is connected to a controller. In plain node, I would do something similar to as follows:
let commands = net.connect(config.port, config.host);
let messages = net.connect(config.port, config.host, () => {
messages.write('CF I\r'); // receive messages on this socket
});
function execute(command, callback=_.noop) {
messages.on('data', (data) => {
if (data.toString('ascii') === '?') {
throw new Meteor.Error('ControllerError', 'Command not recognized');
} else {
let args = _.compact(data.toString('ascii').replace('\r\n', '').split(':'));
if (args[0] === 'End') {
callback();
} else if (args[0] === 'Error') {
let report = args.splice(1, args.length).toString('ascii');
throw new Meteor.Error('ControllerError', report);
}
}
});
}
My question is, how does this translate into a Meteor method asynchronously that actually blocks the client until the callback? Essentially what I want to do is:
Template['Controller'].events({
'click button[data-action="start"]': function (e, template) {
$(e.target).attr('disabled', true);
Meteor.call('execute', 'Startup', function () {
// completed the startup operation now
$(e.target).attr('disabled', false);
});
}
});
Ideally, this would also have some sort of timeout. Say like 5 seconds.