Last active
March 28, 2016 17:39
-
-
Save AlexFrazer/3315ef1cebfb198d00d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExtendedSocket extends Socket { | |
// ... | |
// need to use this to get the error response. | |
tellErrorCode() { | |
return new Promise((resolve, reject) => { | |
this.once('data', data => { | |
resolve(new Error('CommandError', data.replace(/\:\r\n/, ''))); | |
}).write('TC 1\r\n'); // asks for the error | |
}).timeout(5000); | |
} | |
send(command) { | |
let listener; | |
return new Promise((resolve, reject) => { | |
listener = data => { | |
data.split(/\r\n/).forEach(line => { | |
if (/^\:$/.test(line)) resolve(); | |
if (/^\?$/.test(line)) reject(); // need to tell error code here | |
}) | |
} | |
this.on('data', listener).write(`${command}\r\n`); | |
}).timeout(5000).finally(() => { | |
this.removeListener('data', listener); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment