-
Notifications
You must be signed in to change notification settings - Fork 13
Chris Perez Lab-5/17 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ckperez
wants to merge
3
commits into
sea-401d5-javascript:master
Choose a base branch
from
ckperez:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| 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'); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.