| 25 | |
| 26 | // Sample options: { host: "www.moddable.com", id: 0, interval: 5000 } |
| 27 | constructor(options, callback) { |
| 28 | if (!callback) throw new Error; |
| 29 | |
| 30 | super({kind: "RAW", protocol: 1}); |
| 31 | |
| 32 | this.#callback = callback; |
| 33 | this.#identifier = options.id; |
| 34 | |
| 35 | Net.resolve(options.host, (host, address) => { |
| 36 | if (!this.#callback) |
| 37 | return; // closed |
| 38 | |
| 39 | if (address) { |
| 40 | this.#address = address; |
| 41 | this.ping(); |
| 42 | this.#timer = Timer.repeat(() => { |
| 43 | if (!this.#reply) |
| 44 | this.#callback(Ping.timeout); |
| 45 | if (this.#callback) |
| 46 | this.ping(); |
| 47 | }, options.interval ?? 5000); |
| 48 | } |
| 49 | else |
| 50 | this.failed(`Cannot resolve ${host}`); |
| 51 | }); |
| 52 | } |
| 53 | ping() { |
| 54 | this.#icmp_seq++; |
| 55 | const values = new Uint8Array(56); // 8 for icmp header + 48 for icmp payload |