-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientMessageReceiver.js
193 lines (169 loc) · 7.29 KB
/
clientMessageReceiver.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
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
//init and get serverState
var state = require('./serverState');
// load utility module
var utils = require('./utils');
//Logging System
var logSystem = require('./logger');
var serverLog = logSystem.getServerLog();
var msgHandler = require('./messageHandler');
/**
* Called after receiving HELLO:client, registers for all messages a client can send a listener with
* socket.on() and takes corresponding actions.
* @param socket
* @param pingInterval
* @param clientToken
* @param clientName
*/
exports.handleClient = function (socket, pingInterval, clientToken, clientName) {
if (!utils.isValidRandomNumberToken(clientToken)) {
clientToken = utils.rand();
}
var client = state.getClientGivenToken(clientToken);
if (client == null) {
// The client is not known and we create a new one
client = {
socket: socket,
basicInfo: {
clientToken: clientToken,
clientName: clientName,
connected: true
},
team: state.lobbyTeam,
timer: null
};
state.addClient(client);
state.addClientToTeam(client.basicInfo, state.lobbyTeam);
msgHandler.emitMessageToSocket(socket, 'HELLO:ack', {
messageId: utils.rand(),
token: clientToken,
clientName: clientName
});
}
else {
// The client is already known and we update the clientName if a new name was
// included in the HELLO:client message
if (utils.isValidClientName(clientName).value && !state.clientNameAlreadyExists(clientToken, clientName)) {
client.basicInfo.clientName = clientName;
}
client.socket = socket;
client.basicInfo.connected = true;
if (client.timer){
clearTimeout(client.timer);
}
msgHandler.emitMessageToSocket(socket, 'HELLO:ack', {
messageId: utils.rand(),
token: clientToken,
clientName: client.basicInfo.clientName
});
}
msgHandler.sendConfigInformationToClient(client);
// send teamInformation to all devices
msgHandler.sendTeamInformation();
/**
* Removes the client socket from the state as reaction to a disconnect event
*/
socket.on('disconnect', function () {
serverLog.log("info", "client named " + client.basicInfo.clientName + " disconnected");
clearInterval(pingInterval);
// set the connected status to false
state.getClientGivenToken(client.basicInfo.clientToken).basicInfo.connected = false;
//if a lobby client disconnects he gets removed from the serverState after 60 seconds
if (client.team == state.lobbyTeam) {
msgHandler.clientStillDisconnected(client, 0);
}
//inform all devices
msgHandler.sendTeamInformation();
});
/**
* Takes corresponding actions after receiving a BUZZ:buzz:trial message from the
* connected client. That is: send a ERROR message if the received message is invalid, or
* otherwise call the function checkIfBuzzFirstAndInformAllDevices on the messageHandler which
* sends BUZZ:buzz:ack messages to all connected devices when this client has buzzed first.
*/
socket.on('BUZZ:buzz:trial', function (data) {
serverLog.log("debug", "received BUZZ:buzz:trial: " + JSON.stringify(data));
if (!utils.isValidRandomNumberToken(data.messageId) || !utils.isValidRandomNumberToken(data.clientToken) || !utils.isValidRandomNumberToken(data.buzzerRound)) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'Invalid clientToken, messageId or buzzerRound\n'
});
} else {
msgHandler.checkIfBuzzFirstAndInformAllDevices(client, data.buzzerRound);
}
});
/**
* Takes corresponding actions after receiving a TEAMS:change message from the
* connected client. That is: send a ERROR message if the received message is invalid, or
* otherwise move the client to the given team and send a TEAMS:change message to the
* client and a TEAMS message to all connected devices.
*/
socket.on('TEAMS:change', function (data) {
serverLog.log("debug", "received TEAMS:change: " + JSON.stringify(data));
if (!utils.isValidRandomNumberToken(data.messageId) || !utils.isValidRandomNumberToken(data.clientToken) || !data.teamName) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'Invalid clientToken, messageId or teamName.\n'
});
return;
}
if (!state.isTeamChangeAllowed()) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'Team change is currently not allowed!\n'
});
return;
}
if (!state.teamNameAlreadyExists(data.teamName)) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'The destination team does not exist\n'
});
return;
}
msgHandler.moveClientToTeamAndInformAllDevices(client, data.teamName);
serverLog.admin("Client: " + client.basicInfo.clientName + " moved to team: " + data.teamName + ".");
// construct and send answer to client
data.messageId = utils.rand();
msgHandler.emitMessageToSocket(socket, 'TEAMS:change', data);
});
/**
* Takes corresponding actions after receiving a TEAMS:create message from the
* connected client. That is: send a ERROR message if the received message is invalid, or
* otherwise create a new team with the provided name and send a TEAMS message to all
* connected devices.
*/
socket.on('TEAMS:create', function (data) {
serverLog.log("debug", "received TEAMS:create: " + JSON.stringify(data));
if (!utils.isValidRandomNumberToken(data.messageId)) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'Invalid messageId provided.\n'
});
return;
}
if (!state.isTeamChangeAllowed()) {
msgHandler.emitMessageToSocket(socket, 'ERROR', {
originatorId: data.messageId,
errorMessage: 'Team change and creation is currently not allowed!\n'
});
return;
}
var val_res = utils.isValidTeamName(data.teamName);
if(!val_res.value) {
msgHandler.emitMessageToSocket(socket, 'ERROR:TEAMS:create', {
originatorId: data.messageId,
errorMessage: val_res.description
});
return;
}
if(state.teamNameAlreadyExists(data.teamName)) {
msgHandler.emitMessageToSocket(socket, 'ERROR:TEAMS:create', {
originatorId: data.messageId,
errorMessage: 'TeamName already given to another team.\n.'
});
return;
}
msgHandler.createTeamAndInformAllDevices(data.teamName);
serverLog.admin("Client: " + client.basicInfo.clientName + " created team: " + data.teamName + ".");
});
};