-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (132 loc) · 4.24 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
require('dotenv').config();
const { Socket } = require("engine.io");
const express = require("express");
const cors = require('cors');
const fs = require("fs");
const path = require("path");
var app = express();
var options = {
key: fs.readFileSync(process.env.SSL_KEY_PATH),
cert: fs.readFileSync(process.env.SSL_CERT_PATH)
};
var server = require('https').createServer(options, app)
var PORT = process.env.NODE_API_PORT || 3000;
server.listen(PORT, function () {
console.debug(`listening on port ${PORT}`);
})
const io = require('socket.io')(server, {
allowEIO3: true,
});
var allowedOrigins = [];
allowedOrigins.push(process.env.NODE_API_URL + ':' + process.env.NODE_API_PORT);
allowedOrigins.push(process.env.NODE_API_URL);
allowedOrigins.push(process.env.APP_URL);
app.use(cors({
origin: function (origin, callback) {
console.debug('origin:', origin)
console.debug('allowedOrigins', allowedOrigins)
// allow requests with no origin
// (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg = 'The CORS policy for this site does not ' + 'allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
app.use(express.static(path.join(__dirname, "public")));
let userConnection = [];
io.on("connection", (socket) => {
socket.on("userconnect", (data) => {
if (data.meeting_id) {
var other_users = userConnection.filter((p) => p.meeting_id == data.meeting_id);
userConnection.push({
connectionId: socket.id,
user_id: data.user_id,
username: data.username,
avatar: data.avatar,
meeting_id: data.meeting_id,
is_organizer: data.is_organizer,
created_at: data.created_at
})
var userCount = userConnection.length;
other_users.forEach((v) => {
socket.to(v.connectionId).emit("inform_others_about_me", {
other_user_id: data.user_id,
other_user_name: data.username,
other_user_avatar: data.avatar,
connId: socket.id,
userNumber: userCount,
is_organizer: data.is_organizer,
created_at: data.created_at
});
})
socket.emit("inform_me_about_other_user", other_users);
}
});
socket.on("SDPProcess", (data) => {
socket.to(data.to_connId).emit("SDPProcess", {
message: data.message,
from_connId: socket.id,
to_connId: data.to_connId
})
})
socket.on("disconnect", function () {
var disconnect_user = userConnection.find((p) => p.connectionId == socket.id);
if (disconnect_user) {
var meeting_id = disconnect_user.meeting_id;
userConnection = userConnection.filter((p) => p.connectionId != socket.id);
var list = userConnection.filter((p) => p.meeting_id == meeting_id);
list.forEach((v) => {
var userNumber = userConnection.length;
socket.to(v.connectionId).emit("inform_other_about_disconnected_user", {
connId: socket.id,
userNumber: userNumber
});
})
}
});
socket.on("inform_action_to_participant", function (data) {
var org_user = userConnection.find((p) => p.connectionId == data.from_connId);
socket.to(data.connId).emit("inform_participant_about_action", {
from_username: org_user.username,
action: data.action
});
});
socket.on("inform_action_to_me", function (data) {
var org = userConnection.filter((p) => p.meeting_id == data.meeting_id).filter((p) => p.is_organizer === true);
var target_user = userConnection.find((p) => p.connectionId == data.connId);
org.forEach(v => {
socket.to(v.connectionId).emit("inform_me_about_action", {
action: data.action,
username: target_user.username,
from_username: data.from_username,
});
});
});
})
// Start API
app.get('/iceserver', async (req, res) => {
if (process.env.STUN_URL && process.env.TURN_URL) {
let token = {
iceServers: [
{
"urls": "stun:" + process.env.STUN_URL
}, {
"urls": "turn:" + process.env.TURN_URL,
"username": process.env.TURN_ID,
"credential": process.env.TURN_AUTH_TOKEN
}
]
};
res.send(token);
} else {
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
let token = await client.tokens.create();
res.send(token);
}
});
module.exports = app;