diff --git a/Network-Broadcast-News.sublime-workspace b/Network-Broadcast-News.sublime-workspace new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Network-Broadcast-News.sublime-workspace @@ -0,0 +1 @@ + diff --git a/README.md b/README.md index 8fe91d2..fa551b4 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ A NodeJS broadcast server for the largest media conglomerate in the world slides -https://slides.com/theremix/events-and-emitters -https://slides.com/theremix/nodejs-streams +https://slides.com/theremix/events-and-emitters +https://slides.com/theremix/nodejs-streams ## Goals @@ -31,9 +31,9 @@ Whenever the connected socket (client) emits a 'data' event, then data is being ### Additional Features -1. add username registration +1. add username registration once connected, the new client will be prompted to enter a username, store the username and then allow them to broadcast messages with all messages prepended with their username. -1. add admin broadcast +1. add admin broadcast the admin (server.js) can broadcast messages and each message will be prepended with `[ADMIN]` 1. prevent users to set their name as `[ADMIN]` or any other user's name 1. [bonus] admin can enter a command to kick a client `\kick username` and `kick ip:port` will both disconnect the client diff --git a/client.js b/client.js new file mode 100644 index 0000000..177c090 --- /dev/null +++ b/client.js @@ -0,0 +1,48 @@ +var net = require('net'); +var CONFIG = require('./config'); +var readline = require ('readline'); + +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +var name = ' '; +var socket = new net.Socket(); + +socket.setEncoding('utf8'); + +username(); + +function username () { + rl.question("Yo! What's your username? ", function (answer) { + name += answer; + console.log('Welcome ' + answer + '!'); + rl.close(); + process.stdin.resume(); + connect(); + }); +} + +socket.on('error', function (error) { + socket.destroy(); + console.log('ERROR: connection could not be opened.'); +}); + +function connect () { + socket.connect({ port: CONFIG.PORT}, function () { + console.log('Connection opened succesfully.'); + }); + + process.stdin.on('data', function (data) { + socket.write(name + ': ' + data); + }); + + socket.on('data', function (data) { + process.stdout.write(data); + }); + + socket.on('end', function () { + console.log('Goodbye!'); + }); +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..a698de7 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "PORT": 6969 +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..1ddc2f9 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "network-broadcast-news", + "version": "1.0.0", + "description": "A NodeJS broadcast server for the largest media conglomerate in the world", + "main": "client.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/marunoa/Network-Broadcast-News.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/marunoa/Network-Broadcast-News/issues" + }, + "homepage": "https://github.com/marunoa/Network-Broadcast-News#readme" +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..5ae2d99 --- /dev/null +++ b/server.js @@ -0,0 +1,63 @@ +var net = require('net'); +var CONFIG = require('./config'); +var user = []; + +var server = net.createServer (function (socket) { + + var remoteAddress = socket.remoteAddress + ' PORT: ' + socket.remotePort; + + console.log('Somebody connected!'); + console.log('Connection data from: ' + remoteAddress); + + // Connection Listener + + server.on('error', function (error) { + if (error.code === 'EADDRINUSE') { + console.log('Adress already in use. Trying to reconnect...'); + setTimeout (function () { + server.close(); + server.listen(PORT); + }, 1000); + } +}); + + socket.setEncoding('utf8'); + + user.push(socket); + + socket.on('data', function (data) { + process.stdout.write(data); + user.forEach(function (person) { + person.write(data); + }); + }); + + socket.on('end', function () { + console.log(remoteAddress + ' just disconnected.'); + }); + + socket.on('error', function (error) { + console.log('Error from' + remoteAddress + error); + }); + +}); + +process.stdin.on('data', function (data) { + user.forEach(function(person) { + person.write('[ADMIN]: ' + data); + }); +}); + +server.listen(CONFIG.PORT, function () { + var PORT = server.address().port; + console.log('Listening on port:', PORT); +}); + +server.on('close', function () { + server.close(); +}); + + + + +