Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
201 lines (160 loc) · 4.96 KB
/
Copy pathapp.js
File metadata and controls
201 lines (160 loc) · 4.96 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var express = require('express'),
path = require('path'),
http = require('http'),
queue = require('./routes/queue'),
move = require('./routes/move'),
info = require('./routes/info'),
logger = require('morgan'),
bodyParser = require('body-parser'),
static = require('serve-static');
global.ControllingSocketId = null;
global.ControlDuration = 60;// [s]
var app = express();
app.set('port', process.env.PORT || 80);
app.use(logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(bodyParser.json());
app.use(static(path.join(__dirname, 'public')));
var theServer = http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
var io = require('socket.io').listen(theServer);
var theQueue = new queue(io);
var theInfo = new info(io);
var numUsers = 0;
io.on('connection', function(socket)
{
var addedUser = false;
console.log('user connected: ' + socket.id);
socket.emit('queue', theQueue.getQueue());
socket.emit('info', theInfo.getInfo());
var theControlUser = theQueue.getControlUser()
if(theControlUser) socket.emit('ControlStart', theControlUser);
socket.on('check user', function (username, fn)
{
console.log('check user');
if(username.length < 2)
{
fn('Name ist zu kurz');
return;
}
for(var i=0;i<io.sockets.sockets.length;i++)
{
if(io.sockets.sockets[i].id != socket.id && io.sockets.sockets[i].username == username)
{
fn('Dieser Name ist bereits vergeben');
return;
}
}
if(username == "admin")
{
fn('Du bist kein Administrator');
return;
}
fn(null);
});
socket.on('add user', function (username, fn)
{
for(var i=0;i<io.sockets.sockets.length;i++)
{
if(io.sockets.sockets[i].id != socket.id && io.sockets.sockets[i].username == username)
{
console.log(username);
username = username + (Math.floor(Math.random() * 100) + 1);
}
}
// administrator benutzer name
if(username == "hansdampf42")
{
socket.isAdmin = true;
username = "admin";
}
else
{
socket.isAdmin = false;
}
// we store the username in the socket session for this client
socket.username = username;
if(!addedUser)
{
++numUsers;
addedUser = true;
}
socket.emit('login', {
numUsers: numUsers,
username: socket.username
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
fn(username);
});
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data)
{
console.log('new message' + data);
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
socket.on('add control user', function(fn)
{
var info = theInfo.getInfo();
if(!socket.isAdmin && info.Voltage < info.VoltageLimit)
{
return fn(false);
}
theQueue.addUserToQueue(socket.username, socket.id, fn);
io.emit('queue', theQueue.getQueue());
});
socket.on('move', function(data, fn)
{
var info = theInfo.getInfo();
if(!socket.isAdmin && info.Voltage < info.VoltageLimit)
{
return fn(false);
}
if(socket.id == global.ControllingSocketId)
{
move.moveRobot(data, fn);
}
else
{
fn(false);
}
});
socket.on('disconnect', function()
{
console.log('user disconnected: ' + socket.id);
// remove user from control queue
if(theQueue.removeUserFromQueue(socket.id))
{
io.emit('queue', theQueue.getQueue());
}
// remove the username from global usernames list
if (addedUser)
{
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});