-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
69 lines (49 loc) · 1.51 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Modules
express = require 'express'
util = require 'util'
dnode = require 'dnode'
EventEmitter = require('events').EventEmitter
# Initializerz
app = express.createServer()
emitter = new EventEmitter
# Setup Template Engine
app.register '.coffee', require('coffeekup')
app.set 'view engine', 'coffee'
# Setup Static Files
app.use express.static(__dirname + '/public')
browserify = require 'browserify';
app.use browserify(
require : [
'dnode',
jquery : 'jquery-browserify'
],
entry : __dirname + '/client.coffee'
);
# App Routes
app.get '/', (request, response) ->
response.render 'index'
# Listen
app.listen 3000
# On prévient que le serveur écoute
console.log ' ** Express server started and listening on port 3000'
console.log ' * Accesible via http://localhost:3000/'
clients = {}
chat_server = (client, con) ->
events_names = ['joined', 'said', 'parted']
con.on 'ready', =>
console.log(' ** New client connected with nickname : ' + client.name)
emitter.on name, client[name] for name in events_names
emitter.emit 'joined', client.name
clients[client.name] = client
con.on 'end', ->
emitter.removeListener(name, client[name]) for name in events_names
emitter.emit('parted', client.name);
delete clients[client.name];
this.say = (msg) ->
emitter.emit('said', client.name, msg);
this.names = (cb) ->
cb Object.keys(clients)
this
# Lancement du serveur DNode
dnode(chat_server).listen(app)
console.log ' ** Dnode server started, listening to Express server'