Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Network-Broadcast-News.sublime-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -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!');
});
}
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"PORT": 6969
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
63 changes: 63 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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();
});