forked from fallenangel42/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketHandler.js
136 lines (122 loc) · 5.51 KB
/
socketHandler.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
const { generateToken } = require('./utils');
module.exports = function (electronState) {
return function (socket) {
console.log('User connected');
// ====== registerRider ======
// a rider trying to join a session driven by someone else
// (or an automated session)
socket.on('registerRider', function (msg) {
const sessId = msg.sessId;
if (!electronState.driverTokenExists(sessId)) {
// this session doesn't exist, apparently
socket.emit('riderRejected');
console.log('User REJECTED as rider for ' + sessId);
return;
}
// store the socket for this new rider
console.log('User APPROVED as rider for ' + sessId);
electronState.addRiderSocket(sessId, socket);
});
// ====== requestLast ======
// send the last status for the left & right channels so this new rider
// is synchronized with the current status
socket.on('requestLast', function (msg) {
const sessId = msg.sessId;
const lastLeft = electronState.getLastMessage(sessId, 'left');
const lastRight = electronState.getLastMessage(sessId, 'right');
if (lastLeft) {
socket.emit('left', lastLeft);
}
if (lastRight) {
socket.emit('right', lastRight);
}
});
// ====== registerDriver ======
// new driver, let's generate a new authentication token... unless someone
// else is already driving this session!
socket.on('registerDriver', function (msg) {
const sessId = msg.sessId;
console.log('User registered as driver for ' + sessId);
if (!electronState.driverTokenExists(sessId)) {
const token = generateToken();
electronState.addDriverToken(sessId, token, socket);
socket.emit('driverToken', token);
console.log('User APPROVED as driver for ' + sessId);
} else {
socket.emit('driverRejected');
console.log('User REJECTED as driver for ' + sessId);
}
});
// ====== left ======
// left channel updates... send them over to all riders
socket.on('left', function (msg) {
if (!msg.sessId || !electronState.validateDriverToken(msg.sessId, msg.driverToken)) {
return;
}
// store the current status of the left channel for the future
electronState.storeLastMessage(msg.sessId, 'left', msg);
// send real time updates to all riders
electronState.getRiderSockets(msg.sessId).forEach(function (s) {
s.emit('left', msg);
});
});
// ====== right ======
// right channel updates... send them over to all riders
socket.on('right', function (msg) {
if (!msg.sessId || !electronState.validateDriverToken(msg.sessId, msg.driverToken)) {
return;
}
// store the current status of the right channel for the future
electronState.storeLastMessage(msg.sessId, 'right', msg);
// send real time updates to all riders
electronState.getRiderSockets(msg.sessId).forEach(function (s) {
s.emit('right', msg);
});
});
// ====== pain-left ======
// left pain tool updates... send them over to all riders
socket.on('pain-left', function (msg) {
if (electronState.validateDriverToken(msg.sessId, msg.driverToken)) {
electronState.getRiderSockets(msg.sessId).forEach(function (s) {
s.emit('pain-left', msg);
});
}
});
// ====== pain-right ======
// right pain tool updates... send them over to all riders
socket.on('pain-right', function (msg) {
if (electronState.validateDriverToken(msg.sessId, msg.driverToken)) {
electronState.getRiderSockets(msg.sessId).forEach(function (s) {
s.emit('pain-right', msg);
});
}
});
// ====== getRiderCount ======
// returns how many riders are currently connected to this session
// and how many are there in each possible traffic light status
// (green, yellow, red)
socket.on('getRiderCount', function (msg) {
if (electronState.validateDriverToken(msg.sessId, msg.driverToken)) {
const riderData = electronState.getRiderData(msg.sessId);
socket.emit('riderCount', riderData);
}
});
// ====== trafficLight ======
// handles the red / yellow / green traffic light system that riders
// use to inform drivers about how they are doing
socket.on('trafficLight', function (msg) {
electronState.setRiderTrafficLight(msg.sessId, socket, msg.color);
const riderData = electronState.getRiderData(msg.sessId);
const driverSocket = electronState.getDriverSocket(msg.sessId);
if (driverSocket) {
driverSocket.emit('riderCount', riderData);
}
});
// ====== disconnect ======
// remove person from list of riders if they close the connection
socket.on('disconnect', function () {
console.log('User disconnected');
electronState.onDisconnect(socket);
});
};
};