Created
January 9, 2019 16:32
-
-
Save w7089/d435025c2361b0c7fcfe9e933778085a to your computer and use it in GitHub Desktop.
server.js
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 server = require('net').createServer() | |
let sockets = {} | |
let counter = 0 | |
server.on('connection', socket => { | |
socket.id = counter++ | |
sockets[socket.id] = socket | |
console.log('client connected') | |
socket.write('welcome client\n') | |
socket.on('data', data => { | |
console.log('data is:', data) | |
Object.entries(sockets).forEach(([_, cs]) => { | |
cs.write(`${socket.id}: `) | |
cs.write(data) | |
}); | |
socket.write('data is: ') | |
socket.write(data) | |
socket.on('end', () => { | |
delete sockets[socket.id] | |
console.log('client disconnected') | |
}) | |
}) | |
}) | |
server.listen(8000, ()=> console.log('server bound')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment