Created
July 12, 2017 18:29
-
-
Save roppa/b7536da308dd7d58de723f75b10bad07 to your computer and use it in GitHub Desktop.
Node.js socket server and client
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 net = require('net'); | |
| const PORT = 6000; | |
| const server = net.createServer((connection) => { | |
| console.log('created server'); | |
| connection.on('data', (data) => { | |
| connection.write('pong\n'); | |
| }); | |
| connection.pipe(connection); | |
| }); | |
| server.listen(PORT); | |
| const client = net.createConnection({ port: PORT }, () => { | |
| console.log('created client'); | |
| setInterval(() => { | |
| client.write('ping'); | |
| }, 1000); | |
| }); | |
| client.on('data', (data) => { | |
| console.log('client got data', data.toString()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment