-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
166 lines (129 loc) · 4.66 KB
/
server.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
const WebSocketServer = require('ws').Server
const wss = new WebSocketServer({ port: process.env.port || 8888 })
let connections = []
wss.on('connection', (connection) => {
connection.on('message', (message) => {
message = parseMessage(message)
if ( message ) {
switch (message.command) {
case 'connect': {
onConnect(connection, message)
break;
}
case 'requestConnection': {
onRequestConnection(connection, message)
break;
}
case 'acceptConnection': {
onAcceptConnection(connection, message)
break;
}
case 'closeConnection': {
onClose(connection, message)
break;
}
case 'iceCandidateProposal': {
onCandidateProposal(connection, message)
break;
}
}
} else {
respond(connection, malformedMessage())
}
})
connection.on('close', () => {
const { roomId, userId } = connection
onClose(connection, { roomId, userId })
})
})
parseMessage = (message) => {
try { return JSON.parse(message) }
catch (exception) { return undefined }
}
respond = (connection, message) => {
connection.send(JSON.stringify(message))
}
malformedMessage = () => {
return { error: "Malformed message" }
}
// handle the first connection command
onConnect = (connection, command) => {
const { roomId, userId } = command
// if the requested fields has been given
if ( roomId && userId ) {
// we keep the roomId and the userId attached to the connection
// in case of unexpected closing
connection.roomId = roomId
connection.userId = userId
// if the roomId isn't in the data structure, we setup it
if ( !connections[roomId] ) connections[roomId] = {}
// add the new user connection to the room
connections[roomId][userId] = connection
// send to all the users connected at the same
// user is logged in room that a new
Object.values(connections[roomId]).forEach( (roomUserConnection) => {
respond(roomUserConnection, { event: 'connected', user: userId })
})
// otherwise the message is malformed
} else {
respond(connection, malformedMessage())
}
}
// handle the connection requests from a user to another
onRequestConnection = (connection, command) => {
const { roomId, userId, recipientId, sessionDescription } = command
// if the requested fields has been given
if ( roomId && userId && recipientId && sessionDescription ) {
// turn the connection request to the recipient connection
const recipientConnection = connections[roomId][recipientId]
respond(recipientConnection, { event: 'connectionRequest', user: userId, sessionDescription })
// otherwise the message is malformed
} else {
respond(connection, malformedMessage())
}
}
// handle the connection acceptance of a user to another
onAcceptConnection = (connection, command) => {
const { roomId, userId, requesterId, sessionDescription } = command
// if the requested fields has been given
if ( roomId && userId && requesterId && sessionDescription ) {
// turn the connection acceptance to the requester connection
const requesterConnection = connections[roomId][requesterId]
respond(requesterConnection, { event: 'connectionAccepted', user: userId, sessionDescription })
// otherwise the message is malformed
} else {
respond(connection, malformedMessage())
}
}
// handle the ice candidate proposal from a user to the room
onCandidateProposal = (connection, command) => {
const { roomId, userId, iceCandidate } = command
// if the requested fields has been given
if ( roomId && userId && iceCandidate ) {
// turn the ice candidate proposal to all the users of the room
Object.values(connections[roomId]).forEach( (roomUserConnection) => {
respond(roomUserConnection, { event: 'candidateProposal', user: userId, iceCandidate })
})
// otherwise the message is malformed
} else {
respond(connection, malformedMessage())
}
}
// handle the close connection command
onClose = (connection, command) => {
const { roomId, userId } = command
// if the requested fields has been given
if ( roomId && userId ) {
// remove the user from the room connections
delete connections[roomId][userId]
// send to all the users connected the signal of
// the user disconnection
const users = Object.keys(connections[roomId])
Object.values(connections[roomId]).forEach( (roomUserConnection) => {
respond(roomUserConnection, { event: 'disconnected', roomId, disconnectedUserId: userId, users })
})
// otherwise the message is malformed
} else {
respond(connection, malformedMessage())
}
}