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
22 changes: 22 additions & 0 deletions lib/tcp-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const net = require('net');
const server = net.createServer;
const sockets = [];

server((socket)=>{
socket.ID = 'User' + Math.floor(Math.random() * 1000);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This works as far as attaching an unique value to the socket instance. There's always a question as to whether the socket already has a property called ID that's you'd be overwriting. You could test for this (if !socket.ID), check docs, or you could pick a property name that is more specific - like MYAPP_ID. You could also just use a local var with the index of the array like "user " + index. You know index as length -1 after the push.

sockets.push(socket);
socket.on('data', (chunk)=>{
let message = chunk.toString();
socket.write('SENDING... \n');
sockets.forEach((s)=>{
if (s != socket) s.write('INCOMING MESSAGE FROM ' + socket.ID + ':'+ '\n' + message);
})
});
socket.on('end', ()=>{
console.log('Socket ended')
});
}).listen(3000, ()=>{
console.log('Up on port 3000');
});
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "tcp-server-ckperez",
"version": "1.0.0",
"description": "##To Submit this Assignment * fork this repository * write all of your code in a folder containing your name * push to your repository * submit a pull request to this repository * submit a link to your PR in canvas",
"main": "tcp-server.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ckperez/tcp_server.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/ckperez/tcp_server/issues"
},
"homepage": "https://github.com/ckperez/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"
}
}
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const expect = require('chai').expect;
const net = require('net');
const server = require(__dirname + '/../lib/tcp-server');

describe('tcp tests', ()=>{
it('should send a message to all clients except sender', (done)=>{
let result;
let wroteBack = false;
const client1 = net.connect(3000, ()=>{
client1.on('data', (data)=>{
if (data.toString() !== 'SENDING... \n') wroteBack = true;
})
client1.write('testing');
});
const client2 = net.connect(3000, ()=>{
client2.on('data', (data)=>{
result = data.toString();
});
});
setTimeout(()=>{
expect(result.slice(-7)).to.eql('testing');
expect(wroteBack).to.be.false;
done();
}, 200)
});
});