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 .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
136 changes: 136 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### OSX ###

# Icon must end with two \r

# Thumbnails

# Files that might appear in the root of a volume

# Directories potentially created on remote AFP share

### Vim ###
# swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/osx,vim,node,macos,windows
51 changes: 7 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
![cf](https://i.imgur.com/7v5ASc8.png) Lab 06: TCP Chat Server
======
#TCP Chat Server Docs

## Submission Instructions
* fork this repository & create a new branch for your work
* write all of your code in a directory named `lab-` + `<your name>` **e.g.** `lab-susan`
* push to your repository
* submit a pull request to this repository
* submit a link to your PR in canvas
* write a question and observation on canvas
##For this project, we built a TCP chat server that lets users message everyone in the server, lets them directly message a specific user in the server, and lets the user change their nickname to whatever they desire.

## Learning Objectives
* students will understand how to implement a TCP server using the `net` module
* students will understand how to work with node.js EventEmitter's
##To get this project running, you need to open your terminal and do an npm install command which will install all the dependencies that this project needs to run. After doing that, you'll need two terminal windows to start the project. One window is for running the server, while the other is for using telnet to chat. On the terminal window reserved for the server simply type "node server.js", and it should display a message saying that the server is up and describing what port it is running on. On the terminal window reserverd for the telnet side, you need to figure out what your individual IP address is. This is possible by typing "ifconfig" for linux or "ipconfig" for windows. The IP address is the four numbers seperated by dots after the "inet addr" tag. Once you have that sequence of numbers type in "telnet 'your ip address' and the port you are using". This step will NOT work if you have not previously set up a server in the other terminal window, so make sure to do that first.

## Requirements
#### Configuration
<!-- list of files, configurations, tools, etc that are required -->
Your lab directory must include
* `.gitignore`
* `.eslint`
* `package.json`
* `README.md`
#Commands

#### Feature Tasks
* create a TCP Server using the NodeJS native `net` module
* create a `Client` Constructor
* when sockets connect to the server, a new `Client` instance should be made
* all clients should have a unique `id` property - this should come from the use of `node-uuid`
* when sockets are connected with the client pool they should be given event listeners for `data`, `error`, and `close` events
* when a socket emits the `close` event, the socket should be removed from the client pool
* when a socket emits the `error` event, the error should be logged on the server
* when a socket emits the `data` event, the data should be logged on the server and the commands below should be implemented

## Custom commands
* `@all` should trigger a broadcast event
* `@nickname` should allow a user change their nickname
* `@dm` should allow a user to send a message directly to another user by nick name or by their guest id _(unique client id)_
* when a user sends a message, their nickname should be printed
* **i.e.** `cfcrew: sup hackerz`

#### Documentation
* write a paragraph about your project
* write documentation on how to get the project running
* write documentation on how to connect to the server

#### Testing
* no testing required for this lab - yay!
##The "@all" command lets you type a message to everyone in the chat server.
##The "@nickname" command lets you change your nickname in the client to whatever nickname you desire.
##The "@dm" command lets you directly message anyone in the chat server by their nickname.
9 changes: 9 additions & 0 deletions model/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const uuidv4 = require('uuid/v4');

const Client = module.exports = function(socket) {
this.socket = socket;
this.nickname = `user_${Math.random()}`;
this.id = uuidv4();
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "06-tcp_servers",
"version": "1.0.0",
"description": "![cf](https://i.imgur.com/7v5ASc8.png) Lab 06: TCP Chat Server ======",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bretladenburg/06-tcp_servers.git"
},
"keywords": [],
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/bretladenburg/06-tcp_servers/issues"
},
"homepage": "https://github.com/bretladenburg/06-tcp_servers#readme",
"dependencies": {
"uuid": "^3.1.0"
}
}
69 changes: 69 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const net = require('net');
const EE = require('events');
const Client = require('./model/client.js');
const PORT = process.env.PORT || 3000;
const server = net.createServer();
const ee = new EE();
const pool = [];

ee.on('@dm', function(client, string) {
let nickname = string.split(' ').shift().trim();
let message = string.split(' ').slice(1).join(' ').trim();

pool.forEach( c => {
if (c.nickname === nickname) {
c.socket.write(`${client.nickname}: ${message} \n`);
}
});
});

ee.on('@all', function(client, string) {
pool.forEach( c => {
c.socket.write(`${client.nickname}: ${string} \n`);
});
});

ee.on('@nickname', function(client, string) {
client.nickname = string.trim();
});

server.on('connection', function(socket) {
var client = new Client(socket);
pool.push(client);

socket.on('data', function(data) {
const command = data.toString().split(' ').shift().trim();

if (command.startsWith('@')) {
ee.emit(command, client, data.toString().split(' ').splice(1).join(' '));
return;
}

ee.emit('default', client, data.toString());
});

socket.on('close', function(had_error) {
if (had_error) {
console.log('closed connection due to error');
let clientIndex = pool.findIndex(function(c) {
return (c.id === client.id);
});

pool.splice(clientIndex, 1);

}
});

socket.on('error', function(err, data) {
if (err) {
console.error('There was an error', err);
return;
}
console.log(data);
});
});
server.listen(PORT, function() {
console.log('server up:', PORT);
});