Last active
April 17, 2020 08:29
-
-
Save koyanloshe/1dd1531509fd8f4d1c6a123954346352 to your computer and use it in GitHub Desktop.
Chat Server #Javascript
This file contains 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
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