* https://gist.github.com/tedmiston/5935757
()
| 48 | * https://gist.github.com/tedmiston/5935757 |
| 49 | */ |
| 50 | public testTcp (): Promise<boolean> { |
| 51 | log.verbose('Doctor', 'testTcp()') |
| 52 | |
| 53 | return new Promise<boolean>((resolve, reject) => { |
| 54 | /** |
| 55 | * Server |
| 56 | */ |
| 57 | const server = createServer(socket => socket.pipe(socket)) |
| 58 | /** |
| 59 | * Promise Reject |
| 60 | */ |
| 61 | server.on('error', reject) |
| 62 | server.on('close', () => log.silly('Doctor', 'testTcp() server closed')) |
| 63 | |
| 64 | server.listen(8788, 'localhost', () => { |
| 65 | /** |
| 66 | * Client |
| 67 | */ |
| 68 | const client = new Socket() |
| 69 | client.connect(8788, 'localhost', () => { |
| 70 | log.silly('Doctor', 'testTcp() client connected') |
| 71 | client.write('ding') |
| 72 | }) |
| 73 | |
| 74 | client.on('data', () => { |
| 75 | /** |
| 76 | * Promise Resolve |
| 77 | */ |
| 78 | resolve(true) |
| 79 | |
| 80 | client.destroy() // kill client after server's response |
| 81 | }) |
| 82 | /** |
| 83 | * Promise Reject |
| 84 | */ |
| 85 | client.on('error', reject) |
| 86 | |
| 87 | client.on('close', () => server.close()) |
| 88 | }) |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | } |