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
21 changes: 21 additions & 0 deletions aliza/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require ('gulp-mocha');

gulp.task('default', ['lint', 'mocha'], () => {
console.log('default for lint and mocha');
});


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

gulp.task('mocha', () => {
gulp.src('test/test.js')
.pipe(mocha());
});

gulp.watch('./*.js', ['lint', 'mocha']);
19 changes: 19 additions & 0 deletions aliza/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "aliza",
"version": "1.0.0",
"description": "tcp server lab",
"main": "tcp_server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^2.10.2",
"gulp": "^3.9.1",
"gulp-eslint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"mocha": "^2.4.5"
}
}
8 changes: 8 additions & 0 deletions aliza/tcp_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const net = require('net');

const client = net.connect(3000, () => {
// client.on('data', (data) => {
// client.write('MESSAGE RECEIVED');
// })
client.write('HERE IS MESSAGE');
})
18 changes: 18 additions & 0 deletions aliza/tcp_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const net = require('net');

const clients = [];

module.exports = exports = net.createServer((socket) => {
socket.name = socket.remotePort;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you add your own property to an instance you could run into a problem if the instance already has a property using that name. You could check docs, use a test in code (!socket.name) to be sure its not used, or assign a more app specific name like myapp_name. In this code you probably don't need to save it because you're using the value of a property that already exists. Wherever you're test or outputting socket.name, you could just use socket.remotePort. If you wanted to maintain a really app specific name, your sockets array could contain an array of objects, each of which holding the socket and the unique name like sockets = [{socket: , name:"Fred"}];. That all said, your stretch to supply a user id works!

clients.push(socket);
socket.on('data', (chunk) => {
console.log(chunk.toString());
socket.write('Sending message\n');
clients.forEach((s) => {
if (socket.name !== s.name)
s.write(socket.name + ': ' + chunk.toString());
});
});
}).listen(3000, () => {
console.log('Up on 3000');
});
26 changes: 26 additions & 0 deletions aliza/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const net = require('net');
const expect = require('chai').expect;
const server = require('../tcp_server');

describe('tcp tests', () => {
it('should send message to all clients, but not to sender', (done) => {
var result;
var wroteBack = false;
var firstClient = net.connect(3000, () => {
firstClient.on('data', (data) => {
if (data.toString() !== 'Sending message\n') wroteBack = true;
})
firstClient.write('message');
})
var secondClient = net.connect(3000, () => {
secondClient.on('data', (data) => {
result = data.toString();
})
});
setTimeout(() => {
expect(result.slice(-7)).to.eql('message');
expect(wroteBack).to.eql(false);
done();
}, 200)
})
});