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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
18 changes: 18 additions & 0 deletions chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const net = require('net');
const sockets = [];

net.createServer((socket) => {
sockets.push(socket);
socket.on('data', (chunk) => {
sockets.forEach((sock) => {
socket.name = 'Client' + sockets.indexOf(sock);
console.log(socket.name);
if (socket.name !== sock.name) {
socket.write(socket.name + ' says: ' + chunk.toString() + '\n');
}
});
});
}).listen(3000, () => {
console.log('listening on port 3000');
});
17 changes: 17 additions & 0 deletions clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const net = require('net');

function createClient(username) {
let client = net.connect(3000, () => {
client.write('My name is ' + username + '!\n');
client.on('data', (chunk) => {
console.log('Message received from ' + chunk.toString().split('\s')[0].trim() + ':\n', chunk.toString());
});
}).on('error', (err) => {
console.log(err);
client.end();
});
}

createClient('stefanie');
createClient('bob');
65 changes: 65 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const gulp = require('gulp');
const mocha = require('gulp-mocha');
const lint = require('gulp-eslint');
const opts = {
'extends': 'eslint:recommended',
'ecmaFeatures': {
'modules': true
},
'rules': {
'no-alert': 0,
'no-bitwise': 0,
'camelcase': 1,
'no-console': 1,
'curly': 1,
'eqeqeq': 0,
'no-eq-null': 0,
'guard-for-in': 1,
'no-empty': 1,
'no-use-before-define': 0,
'no-obj-calls': 2,
'no-unused-vars': 0,
'new-cap': 1,
'no-shadow': 0,
'strict': 1,
'no-invalid-regexp': 2,
'comma-dangle': 2,
'no-undef': 1,
'no-new': 1,
'no-extra-semi': 1,
'no-debugger': 2,
'no-caller': 1,
'semi': 1,
'quotes': 0,
'no-unreachable': 2
},
'globals': {
'$': false
},
'env': {
'node': true,
'es6': true
}
};

gulp.task('linter' , () => {
return gulp.src('./**/*.js')
.pipe(lint(opts))
.pipe(lint.format());
});

gulp.task('tests', () => {
return gulp.src('./test/chat-test.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('watch', () => {
gulp.watch('./test/chat-test.js', ['linter', 'tests']);
gulp.watch('./**/*.js', ['linter']);
});

gulp.task('default', ['watch', 'linter', 'tests'], () => {
console.log('All tasks completed successfully');
});
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "stefanie-hansen",
"version": "1.0.0",
"description": "chat server",
"main": "chat.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stefuhnee/tcp_server.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/stefuhnee/tcp_server/issues"
},
"homepage": "https://github.com/stefuhnee/tcp_server#readme",
"devDependencies": {
"chai": "^3.5.0",
"gulp": "^3.9.1",
"gulp-eslint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"mocha": "^2.4.5"
}
}
58 changes: 58 additions & 0 deletions test/chat-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const net = require('net');
const expect = require('chai').expect;
const data = {};
const sockets = [];

function createServer() {
net.createServer((socket) => {
sockets.push(socket);
socket.on('data', (chunk) => {
sockets.forEach((sock) => {
socket.name = 'Client' + sockets.indexOf(sock);
if (sockets.length === 1 || socket.name !== sock.name) {
socket.write('test received');
} else {
socket.write('not receiving');
}
});
});
}).listen(3000, () => {
console.log('listening on port 3000');
});
}

function createClient(clientName, done) {
if (!data[clientName]) data[clientName] = '';
let client = net.connect(3000, () => {
client.write('test');
client.on('data', (chunk) => {
console.log(chunk.toString());
data[clientName] += chunk.toString();
done();
});
}).on('error', (err) => {
console.log(err);
client.end();
});
}

describe('chat test suite', () => {
before((done) => {
createServer();
createClient('test', done);
});
it('should receive data from the server', () => {
expect(data['test']).to.eql('test received');
});
before((done) => {
createClient('secondTest', done);
});
it('should send data to clients that did not write it', () => {
expect(data['test']).to.eql('test received');
});
it('should not send data to clients that wrote it', () => {
expect(data['secondTest']).to.not.eql('test received');
});
});