-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
142 lines (126 loc) · 4.02 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
var DEV_MODE = false;
var PORT = process.env.PORT || 3000;
/**
* Sets the DEV_MODE constant for development.
* Example usage:
* node server.js --dev
*/
process.argv.forEach(function (value, index, array) {
if (value === '--dev' || value === '--development') {
DEV_MODE = true;
}
});
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var socketIO = require('socket.io');
// var app = express();
// var http = require('http').Server(app);
// var io = socketIO(http);
var app = express();
var server = app.listen(PORT);
var io = socketIO.listen(server);
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const COLORS_ARRAY = ['#FFC312', '#C4E538', '#12CBC4', '#FDA7DF', '#ED4C67', '#EE5A24', '#009432', '#0652DD', '#9980FA', '#833471'];
let rooms = {
main: {
'name': 'main',
'colors': [],
'mapName': "none"
},
};
let users = {
// uIJKLh: {id: "", name: "", color: "", socket: null, roomName: ""}
};
app.use(logger('dev'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
console.log('a user connected');
socket.join('main'); // initially join main room
socket.on('disconnect', () => console.log("Client disconnected"));
socket.on('joinRoom', (data) => {
let room;
if (data.roomName) {
if (!(data.roomName in rooms)) {
socket.emit('roomNotFound', {roomName: data.roomName});
return;
}
room = rooms[data.roomName]
}
const color = room.colors.shift();
const user = newUser(data.username, color, socket);
users[user.id] = user;
joinRoom(socket, user, room.name);
const initData = {
roomType: room.type,
userId: user.id,
color: color,
mapName: room.mapName,
roomName: room.name,
clientName: user.name,
}; // parameters when a new user joins
socket.emit('roomJoined', initData); // Send initial data to client
});
socket.on('createRoom', (data) => {
let room;
if (data.mapName) {
room = newRoom(data.roomType, data.mapName);
rooms[room.name] = room;
}
const color = room.colors.shift();
let user = newUser(data.username, color, socket);
users[user.id] = user;
joinRoom(socket, user, room.name);
const initData = {
roomType: room.type,
userId: user.id,
color: color,
mapName: room.mapName,
roomName: room.name,
clientName: user.name,
}; // parameters when a new user joins
socket.emit('roomJoined', initData); // Send initial data to client
});
// Start listening for mouse move events
socket.on('mousemove', function (data) {
// This line sends the event (broadcasts it)
const roomName = users[data.id].roomName;
socket.broadcast.to(roomName).emit('moving', data);
});
});
// Function to join room
function joinRoom(socket, user, roomName) {
user.socket = socket;
user.socket.join(roomName);
user.socket.broadcast.to(roomName).emit('newUser', {
id: user.id,
name: user.name,
color: user.color
});
user.roomName = roomName;
users[user.name] = user;
}
// Function to create new Room Object
function newRoom(roomType, mapName) {
return {
'type': roomType,
'name': Math.random().toString(36).substr(3, 7),
'mapName': mapName,
'colors': [...COLORS_ARRAY]
}
}
// Function to create new User Object
function newUser(name, color, socket) {
return {
'id': Math.round(Math.random() * Date.now()).toString(36),
'name': name,
'color': color,
'socket': socket
}
}