Last active
March 11, 2019 05:10
-
-
Save lenconda/fac0053c1735060f717c12ca6af20472 to your computer and use it in GitHub Desktop.
Check if remote service is available
This file contains hidden or 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
const tcpp = require('tcp-ping') | |
const _ip = Symbol('ip') | |
const _port = Symbol('port') | |
class TCPChecker { | |
/** | |
* @constructor | |
* @param {string} address | |
*/ | |
constructor (address) { | |
let port | |
[this[_ip], port] = address.split(':') | |
this[_port] = parseInt(port) | |
} | |
/** | |
* check if the service is available | |
* @public | |
* @return {Promise<boolean>} | |
*/ | |
async available () { | |
return new Promise((resolve, reject) => { | |
tcpp.probe(this[_ip], this[_port], (err, available) => { | |
if (err) reject(err.toString()) | |
else resolve(available) }) | |
}) | |
} | |
/** | |
* show the average speed after 10 attempts | |
* @public | |
* @return {Promise<number>} | |
*/ | |
async speed () { | |
return new Promise((resolve, reject) => tcpp.ping({ | |
address: this[_ip], port: this[_port] }, (err, data) => { | |
if (err) reject(err.toString()) | |
else resolve(Math.round(data.avg)) })) | |
} | |
} |
This file contains hidden or 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
import tcpp from 'tcp-ping' | |
interface ITCPCheckerAddress extends String { | |
split: () => Array<string> | |
} | |
export default class TCPChecker<ITCPCheckerAddress> { | |
/** | |
* @constructor | |
* @param {string} address | |
*/ | |
constructor (address: String) { | |
let port: string | |
[this.ip, port] = address.split(':') | |
this.port = parseInt(port) | |
} | |
private ip: string | |
private port: number | |
/** | |
* check if the service is available | |
* @public | |
* @return {Promise<boolean>} | |
*/ | |
public async available (): Promise<boolean> { | |
return new Promise((resolve, reject) => { | |
tcpp.probe(this.ip, this.port, (err, available) => { | |
if (err) reject(err.toString()) | |
else resolve(available) }) | |
}) | |
} | |
/** | |
* show the average speed after 10 attempts | |
* @public | |
* @return {Promise<number>} | |
*/ | |
public async speed (): Promise<number> { | |
return new Promise((resolve, reject) => tcpp.ping({ | |
address: this.ip, port: this.port }, (err, data) => { | |
if (err) reject(err.toString()) | |
else resolve(Math.round(data.avg)) })) | |
} | |
} |
This file contains hidden or 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
const TCPChecker = require('./checker') | |
const tcpChecker = new TCPChecker('123.123.123.123:233') | |
async function test () { | |
console.log('Availability: ', await tcpChecker.available()) | |
console.log('Speed: ', await tcpChecker.available()) | |
} | |
test() |
This file contains hidden or 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
import TCPChecker from './checker' | |
const tcpChecker = new TCPChecker('123.123.123.123:233') | |
async function test () { | |
console.log('Availability: ', await tcpChecker.available()) | |
console.log('Speed: ', await tcpChecker.available()) | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment