diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/chat.js b/chat.js new file mode 100644 index 0000000..c0740ab --- /dev/null +++ b/chat.js @@ -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'); +}); diff --git a/clients.js b/clients.js new file mode 100644 index 0000000..b820a18 --- /dev/null +++ b/clients.js @@ -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'); diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..c259763 --- /dev/null +++ b/gulpfile.js @@ -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'); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..fb6ffc0 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/chat-test.js b/test/chat-test.js new file mode 100644 index 0000000..e2fc105 --- /dev/null +++ b/test/chat-test.js @@ -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'); + }); +});