Skip to content

Commit

Permalink
Added clist mgmt
Browse files Browse the repository at this point in the history
added: server-side clist
added: client-side clist widget
added: random (not changeable) usernames from a static array (BAD!)
  • Loading branch information
xge committed Jan 16, 2015
1 parent e691863 commit 9f88c20
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
12 changes: 8 additions & 4 deletions app/index/index.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
app.controller 'IndexController',
class IndexController
constructor: (@Socket) ->
constructor: (@$scope, @Socket) ->
@username = ''
@clist = []
@messages = []
@Socket.on 'msg', (data) =>
@messages.push data
@Socket.on 'new username', (name) =>
@username = name
@Socket.on 'clist changed', (clist) =>
@clist = clist
send: () ->
msg = new Message(moment().valueOf(), 'User', @currentPayload)
@messages.push msg
@Socket.emit 'msg', new Message(moment().valueOf(), @username, @currentPayload)
delete @currentPayload
@Socket.emit 'msg', msg
10 changes: 7 additions & 3 deletions app/index/index.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Currently online</h3>
<h3 class="panel-title">Currently online: {{ index.clist.length }}</h3>
</div>
<div class="panel-body">
User
<ul class="list-unstyled">
<li ng-repeat="user in index.clist">
{{ user }}
</li>
</ul>
</div>
</div>
</div>
Expand All @@ -22,7 +26,7 @@ <h3 class="panel-title">Currently online</h3>
<div class="col-md-9">
<form name="formMsg" ng-submit="index.send()" novalidate>
<div class="input-group">
<span class="input-group-addon">User</span>
<span class="input-group-addon">{{ index.username }}</span>
<input type="text" class="form-control" id="inputMsg" ng-model="index.currentPayload" required />
<span class="input-group-btn">
<button class="btn btn-primary">Send</button>
Expand Down
2 changes: 1 addition & 1 deletion lib/clist.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class Clist
constructor: (@randomNames = ['Alice', 'Bob']) ->
constructor: (@randomNames) ->
@list = []
addUser: (username) ->
if not (username in @list)
Expand Down
21 changes: 19 additions & 2 deletions lib/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('coffee-trace')
path = require 'path'

bodyParser = require 'body-parser'
clist = require path.join(__dirname, 'clist')
Clist = require path.join(__dirname, 'clist')
config = require '../config.json'
compression = require 'compression'
debug = require('debug')(config.logger)
Expand All @@ -18,6 +18,8 @@ app.use bodyParser.json()
app.use morgan(config.logger)
app.use express.static(path.join(__dirname, '../build/app'))

clist = new Clist ['Alice', 'Bob', 'Charlene', 'Denzel', 'Edgar', 'Fausto', 'Gary', 'Harriet', 'Ingo', 'Jockel', 'Kid']

# GET /
app.get '/', (req, res) ->
res.sendfile path.join(__dirname, '../build/index.html')
Expand All @@ -35,7 +37,22 @@ server.listen config.port, () ->
debug '%s listening at http://%s:%s', config.name, address.address, address.port

io.on 'connection', (socket) ->

updateClist = () ->
socket.emit 'clist changed', clist.getUsernames()
socket.broadcast.emit 'clist changed', clist.getUsernames()

socket.emit 'msg', new Message(moment().valueOf(), 'System', "Welcome to #{ config.name }")

username = clist.addRandomUser()
socket.emit 'new username', username
updateClist()

socket.on 'disconnect', () ->
clist.removeUser username
updateClist()

socket.on 'msg', (msg) ->
socket.broadcast.emit 'msg', new Message(msg.timestamp, msg.user, msg.payload)
toSend = new Message(msg.timestamp, msg.user, msg.payload)
socket.emit 'msg', toSend
socket.broadcast.emit 'msg', toSend
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "chat.js",
"main": "app.js",
"scripts": {
"start": "coffee -w lib/server.coffee",
"start": "grunt && coffee -w lib/server.coffee",
"test": "mocha test/**/*.spec.coffee"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion test/clist.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe 'Clist', () ->
beforeEach () ->
clist = new Clist ['Alice', 'Bob']

it 'should return a username', () ->
it 'should add a random username and return it', () ->
clist.addRandomUser().should.eql 'Alice'
clist.getUsernames().should.eql ['Alice']

Expand Down

0 comments on commit 9f88c20

Please sign in to comment.