-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeting-server.js
72 lines (63 loc) · 2.33 KB
/
meeting-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
const meetingHelper = require('./utils/meeting-helper');
const { MeetingPayloadEnum } = require('./utils/meeting-payload.enum');
function parseMessage(message){
try{
const payload = JSON.parse(message);
return payload;
} catch (error){
return { type: MeetingPayloadEnum.UNKNOWN };
}
}
function listenMessage(meetingId, socket, meetingServer){
socket.on("message", (message) => handleMessage(meetingId, socket, message, meetingServer));
}
function handleMessage(meetingId, socket, message, meetingServer){
var payload = "";
if(typeof message === "string"){
payload = parseMessage(message);
}
else {
payload = message;
}
switch(payload.type){
case MeetingPayloadEnum.JOIN_MEETING:
meetingHelper.joinMeeting(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.CONNECTION_REQUEST:
meetingHelper.forwardConnectionRequest(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.ICECANDIDATE:
meetingHelper.forwardIceCandidate(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.OFFER_SDP:
meetingHelper.forwardOfferSDP(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.ANSWER_SDP:
meetingHelper.forwardAnswerSDP(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.LEAVE_MEETING:
meetingHelper.userLeft(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.END_MEETING:
meetingHelper.endMeeting(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.VIDEO_TOGGLE:
case MeetingPayloadEnum.AUDIO_TOGGLE:
meetingHelper.forwardEvent(meetingId, socket, meetingServer, payload);
break;
case MeetingPayloadEnum.UNKNOWN:
break;
default:
break;
}
}
function initMeetingServer(server){
const meetingServer = require("socket.io")(server);
meetingServer.on('connection', socket => {
const meetingId = socket.handshake.query.id;
listenMessage(meetingId, socket, meetingServer);
});
}
module.exports = {
initMeetingServer
}