This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
82 lines (63 loc) · 2.01 KB
/
app.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes/routes.js');
var http = require('http');
var path = require('path');
var socketio = require('socket.io')
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.cookieSession({secret: "socialtables"}));
app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//index route
// App Routing
app.get('/', routes.index);
app.get('/bands', routes.list);
app.get('/bands/:id', routes.band);
app.get('/bands/:id/join', routes.join);
app.get('/create', routes.chooseName);
app.post('/create', routes.create);
app.post('/join', routes.joinBand);
//Create the server
var server = http.createServer(app)
//Start the web socket server
var io = socketio.listen(server);
var users = {};
//If the client just connected
io.sockets.on('connection', function(socketObj) {
socketObj.on('room', function(room) {
socketObj.join(room);
});
io.sockets.emit("connected",{message:"is connected",id:socketObj.id});
users[socketObj.id] = socketObj.id;
io.sockets.emit('id',users);
socketObj.on('sound', function(message){
io.sockets.emit('serverSound', message);
});
socketObj.on("disconnect",function(){
io.sockets.emit("disconnected",{message:"is disconnected",id:users[socketObj.id]});
delete users[socketObj.id];
io.sockets.emit('id',users);
});
socketObj.on('clientName', function(userName){
users[socketObj.id] = userName;
io.sockets.emit('id',users);
});
});
server.listen(3000, function(){
console.log('Express server listening on port ' + app.get('port'));
});