forked from grabc59/cobeats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (126 loc) · 4.18 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
//////////////////////////////////////
/////// SERVER REQUIREMENTS
//////////////////////////////////////
const path = require('path');
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io').listen(server);
const port = process.env.PORT || 3000;
const logger = require('morgan');
const bodyParser = require('body-parser');
//////////////////////////////////////
/////// SOCKET VARIABLES
//////////////////////////////////////
var connected_users = [];
var connections = [];
//////////////////////////////////////
/////// ROUTE FILE DECLARATIONS
//////////////////////////////////////
const users = require('./src/server/routes/users');
const messages = require('./src/server/routes/messages');
//////////////////////////////////////
/////// MIDDLEWARE
//////////////////////////////////////
app.use(logger('short'));
app.set('views', path.join(__dirname, 'src/client'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', express.static(path.join(__dirname, 'src/client')));
app.use('/users', users);
app.use('/messages', messages);
//////// WILDCARD ROUTE
// allows user to refresh the page while on an angular route
// example: refresh http://localhost:3000/main
// >> no matching express routes besides this wildcard
// >> server sends index.html
// >> browser loads angular from the index
// >> angular reads the url and loads the right view
app.use('*', function(req, res, next) {
res.sendFile('index.html', {root: path.join(__dirname, 'src/client')});
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handlers
// will print stacktrace
if (app.get('env') === 'development') {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handlers
// no stacktrace leaked to user
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
//////////////////////////////////////
////////// SOCKET SERVER
//////////////////////////////////////
io.sockets.on('connection', function(socket) {
//////////// NEW CONNECTION EVENT
// connections.push(socket);
// informational logs
console.log('yay socket connection event!')
// console.log('Connected: %s sockets connected', connections.length);
// console.log(connected_users);
//////////// DISCONNECT EVENT
socket.on('disconnect', function(data) {
if (!socket.username) return;
connected_users.splice(connected_users.indexOf(socket.username), 1);
updateUsernames();
connections.splice(connections.indexOf(socket),1);
console.log('disconnected: %s sockets connected', connections.length);
});
//////////// SEND MESSAGE EVENT
socket.on('send message', function(data) {
console.log(data);
io.sockets.emit('new message', {content: data, username: socket.username});
});
//////////// NEW USER EVENT
socket.on('new user', function(data, callback) {
console.log('new user event! ', data)
callback(true);
socket.username = data;
connected_users.push(socket.username);
connections.push(socket)
console.log('Connected: %s sockets connected', connections.length);
console.log(connected_users);
updateUsernames();
io.sockets.emit('new user notification', socket.username);
});
//////////// GET USERNAMES
function updateUsernames() {
io.sockets.emit('get users', connected_users);
}
//////////// USER IS TYPING EVENT
socket.on("typing", function(data) {
io.sockets.emit('isTyping', {
isTyping: data,
person: socket.username,
});
});
//////////// PIXEL CLICK
///// pixelInfo is an object containing the pixel ID and color (easel.component.js)
socket.on("pixel click", function(pixelInfo) {
console.log(pixelInfo)
io.sockets.emit("update pixel", pixelInfo);
});
});
server.listen(port, () => {
console.log(`CoBeats is running on http://localhost:${port} ...`);
});
module.exports = app;