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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
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
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ Your lab directory must include
* `README.md`

#### 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
- [x] create a TCP Server using the NodeJS native `net` module
- [x] create a `Client` Constructor
- [x] when sockets connect to the server, a new `Client` instance should be made
- [x] 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
- [x] `@all` should trigger a broadcast event
- [x] `@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)_
- [x] when a user sends a message, their nickname should be printed
* **i.e.** `cfcrew: sup hackerz`

#### Documentation
Expand Down
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();
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "chat-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"uuid": "^3.1.0"
}
}
64 changes: 64 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'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(' ').splice().join(' ').trim();

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

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

ee.on('@nickname', function(client, nameForClient){
client.nickname = nameForClient[0];
});

ee.on('default', function(client, string){
client.socket.write('try a command\n');
});
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(' '));
};

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

});
socket.on('close', function(){
pool.forEach((user,index) => {
if(user.nickname === client.nickname){
console.log(user.nickname, ' disconnected');
pool.splice(index, 1);
};
});
});
socket.on('error', function(err){
console.log(new Error('someone goofed', socket.nickname))
});
});


server.listen(PORT, function(){
console.log('server is online.....\nPORT', PORT);
});