Skip to content

Instantly share code, notes, and snippets.

@koyanloshe
Last active April 17, 2020 08:29
Show Gist options
  • Save koyanloshe/1dd1531509fd8f4d1c6a123954346352 to your computer and use it in GitHub Desktop.
Save koyanloshe/1dd1531509fd8f4d1c6a123954346352 to your computer and use it in GitHub Desktop.
Chat Server #Javascript
var net = require('net')
var chatServer = net.createServer(), clientList = []
chatServer.on('connection', function(client) {
client.name = client.remoteAddress + ':' + client.remotePort client.write('Hi ' + client.name + '!\n');
clientList.push(client)
client.on('data', function(data) { broadcast(data, client)
}) })
function broadcast(message, client) { for(var i=0;i<clientList.length;i+=1) {
if(client !== clientList[i]) { clientList[i].write(client.name + " says " + message)
} }
}
chatServer.listen(9000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment