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
25 changes: 25 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const net = require('net');

const clients = [];

net.createServer((socket) => {
socket.name = "User - " + socket.remotePort;
clients.push(socket);

socket.on('data', (chunk) => {
clients.forEach((client) => {
if (socket.name === client.name) return;
client.write(socket.name + ': ' + chunk.toString());
});
});

function broadcast(message, socket) {
sockets.forEach(function(s){
if (s === socket) return;
s.write(message);
});
};

}).listen(3000, () =>{
console.log('Up on 3000!');
});
25 changes: 25 additions & 0 deletions stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

/*eslint-env es6*/

const fs = require('fs');

const readStream = fs.createReadStream(__dirname + '/test.txt');

let dataString = '';

readStream.on('data', (chunk) => {
console.log('Truffles');
console.log(chunk);
dataString =+ chunk.toString();
});

readStream.on('end', () => {
console.log('stream ended');
});

process.stdin.on('data', (chunk) => {
console.log('${chunk.toString()} was piped into this program');
});

process.stdin.pipe(process.stdout);
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing one two three
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

const expect = require('chai').expect;
const net = require('net');
const tcp = require('../server');

describe('Chat server testing: ', () => {
it('should send a message to all clients except sender', (done) => {
let result;
let wroteBack = false;

const clientOne = net.connect(3000, () => {
clientOne.on('data', () => {
throw new Error('it wrote back');
});
clientOne.write('test');
});
const clientTwo = net.connect(3000, () => {
clientTwo.on('data', (data) => {
expect(data.toString()).to.eql('test');
done();
});
});
setTimeout(() => {
expect(result).to.eql('test');
expect(wroteBack).to.eql(false);
done();
}, 200)
});

})