Skip to content

lab6-Jeremiah #14

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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"
}
93 changes: 93 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Created by https://www.gitignore.io/api/node,osx,windows,linux
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### 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 ###
*.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
### 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/node,osx,windows,linux
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"vsicons.presets.angular": false
}
82 changes: 50 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
![cf](https://i.imgur.com/7v5ASc8.png) Lab 06: TCP Chat Server
======

## To Submit this Assignment
* Fork this repository
* Write all of your code in a directory named `lab-` + `<your name>` **e.g.** `lab-brian`
* 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

## Include
* `.gitignore`
* `.eslint`
* `package.json`
* `gulpfile.js`
* `README.md`
* write a paragraph about your project
* write documentation on how to get the project running
* write documentation on how to connect to the server
This app is a telnet chatroom that uses sockets vis server.js powered by node.

-Many users allowed to chat.
-Each user is assigned unique user name.
-Local Server

### Tech

The TCP chat server uses a number of open source projects to work properly:

* [node.js] - evented I/O for the backend
* [Server.js]
* [node modules]

And of course TCP Chat Server itself is open source with a [public repository][TCP]
on GitHub.

### Installation

TCP Chat Server requires [Node.js](https://nodejs.org/) v4+ to run.

Install the dependencies and devDependencies and start the server.

```sh
$ node Install
$ npm init -y
$ npm i -S uuid
```
## How to connect

* Clone the Repo in terminal
* Open you network Preferences and grab your IP address
* In your terminal run the "node server.js" command and you will recieve a message saying that you are connected
* Split pane from your Shell menu in terminal
* In the new pane run this command telnet -your IP address- -port- then hit enter
for ex. "telnet 10.0.0.178 3000" and you will see "Escape character is '^]'." and that means you are connected.

## Description

* 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 chatterz`
* When sockets connect to the server, a new `Client` instance is made.
* All clients have a unique `id` property - this will come from the use of `node-uuid`
* When sockets are connected with the client pool they will be given event listeners for `data`, `error`, and `close` events
* When a socket emits the `close` event, the socket will be removed from the client pool
* When a socket emits the `error` event, the error will be logged on the server
* When a socket emits the `data` event, the data will be logged on the server and the commands below will be implemented

### Chat Features

* @all triggers a broadcast event
* @nickname allows a user change their nickname
Copy link

Choose a reason for hiding this comment

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

Your code is looking for @NewName not @Nickname.

* @dm allows 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 will be printed
i.e. cupid: sup lover
23 changes: 23 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require ('gulp-mocha');

gulp.task('test', function(){
gulp.src('./test/*-test.js', { read: false})
.pipe(mocha({ reporter: 'spec'}));
});

gulp.task('lint', function() {
return gulp.src(['**/*.js', '!node_modules'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('dev', function(){
gulp.watch(['**/*.js', '!node_modules/**'], ['lint', 'test']);
});

gulp.task('default', ['dev']);
9 changes: 9 additions & 0 deletions models/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const uuid = require('uuid');

const Client = module.exports = function(socket) {
this.socket = socket;
this.nickname = `newperson_${Math.floor((Math.random() * 500) + 1)}`;
this.id = uuid.v4();
};
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": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jtwalters25/06-tcp_servers.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/jtwalters25/06-tcp_servers/issues"
},
"homepage": "https://github.com/jtwalters25/06-tcp_servers#readme",
"dependencies": {
"uuid": "^3.0.1"
}
}
84 changes: 84 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

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

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

pool.forEach( c => {

if (c.nickname === nickname) {
c.socket.write(`${client.nickname}: ${message}`);
}
});
});
ee.on('@newname', function(client, string){
if(string == '') {
client.socket.write('Enter a new username\n');
return; }
client.nickname = string.split(' ').shift().trim();
client.socket.write(`Your new username is ${client.nickname}\n`);
});

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



ee.on('default', function(client, string) {
client.socket.write('not a command\n');
});

server.on('connection', function(socket) {
var client = new Client(socket);
pool.push(client); //adds client each time connects
pool.forEach( c => {
c.socket.write(`${client.nickname} is in the chatroom now! \n There are
${pool.length} users in the chat room\n`);
});

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


socket.on('error', function(err){

Copy link

Choose a reason for hiding this comment

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

You're not handling your error event here. Something should go inside of this function.

});
socket.on('close', function(){
pool.forEach(c => {
let bucket = pool.indexOf(c);
if(c.id === client.id) {
pool.splice(bucket, 1);
}
});
});

if (command.startsWith('@')) {

ee.emit(command, client, data.toString().split(' ').slice(1).join(' '));
//have customer user based event initiate
return;
}

socket.destroy(error);
Copy link

Choose a reason for hiding this comment

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

This line is causing your socket to close when a user enters a command that does not satisfy the condition of any of your if blocks. Instead you should continuing down to the default emitter on line 77. You should remove this line from here. You could put it in its own event.


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

server.listen(PORT, function() {
console.log('server is up cuh:', PORT);
});