Merge remote-tracking branch 'origin/utilities-ws' into dev
This commit is contained in:
commit
bb60ebfb7e
1 changed files with 76 additions and 14 deletions
|
|
@ -9,28 +9,76 @@ const NO_SESSIONS = "SESSION_DISCONNECTED";
|
|||
const SESSION_ALREADY_CONNECTED = "SESSION_ALREADY_CONNECTED";
|
||||
// const wsReconnectionTimeout = process.env.wsReconnectionTimeout | 10 * 1000;
|
||||
|
||||
let connectedSessions = {};
|
||||
|
||||
let io;
|
||||
|
||||
wsRouter.get(`/${process.env.S3_KEY}/sockets-list`, function (req, res) {
|
||||
console.log("[WS]looking for all available sessions");
|
||||
let liveSessions = {};
|
||||
for (let peerId of io.sockets.adapter.rooms.keys()) {
|
||||
let {projectKey, sessionId} = extractPeerId(peerId);
|
||||
if (projectKey !== undefined) {
|
||||
liveSessions[projectKey] = liveSessions[projectKey] || [];
|
||||
liveSessions[projectKey].push(sessionId);
|
||||
}
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": connectedSessions}));
|
||||
res.end(JSON.stringify({"data": liveSessions}));
|
||||
});
|
||||
wsRouter.get(`/${process.env.S3_KEY}/sockets-list/:projectKey`, function (req, res) {
|
||||
console.log(`[WS]looking for available sessions for ${req.params.projectKey}`);
|
||||
let liveSessions = {};
|
||||
for (let peerId of io.sockets.adapter.rooms.keys()) {
|
||||
let {projectKey, sessionId} = extractPeerId(peerId);
|
||||
if (projectKey === req.params.projectKey) {
|
||||
liveSessions[projectKey] = liveSessions[projectKey] || [];
|
||||
liveSessions[projectKey].push(sessionId);
|
||||
}
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": connectedSessions[req.params.projectKey] || []}));
|
||||
res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []}));
|
||||
});
|
||||
|
||||
const removeSession = (projectKey, sessionId) => {
|
||||
const i = (connectedSessions[projectKey] || []).indexOf(sessionId);
|
||||
if (i !== -1) {
|
||||
connectedSessions[projectKey].splice(i, 1);
|
||||
wsRouter.get(`/${process.env.S3_KEY}/sockets-live`, async function (req, res) {
|
||||
console.log("[WS]looking for all available LIVE sessions");
|
||||
let liveSessions = {};
|
||||
for (let peerId of io.sockets.adapter.rooms.keys()) {
|
||||
let {projectKey, sessionId} = extractPeerId(peerId);
|
||||
if (projectKey !== undefined) {
|
||||
let connected_sockets = await io.in(peerId).fetchSockets();
|
||||
for (let item of connected_sockets) {
|
||||
if (item.handshake.query.identity === IDENTITIES.session) {
|
||||
liveSessions[projectKey] = liveSessions[projectKey] || [];
|
||||
liveSessions[projectKey].push(item.handshake.query);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": liveSessions}));
|
||||
});
|
||||
wsRouter.get(`/${process.env.S3_KEY}/sockets-live/:projectKey`, async function (req, res) {
|
||||
console.log(`[WS]looking for available LIVE sessions for ${req.params.projectKey}`);
|
||||
let liveSessions = {};
|
||||
for (let peerId of io.sockets.adapter.rooms.keys()) {
|
||||
let {projectKey, sessionId} = extractPeerId(peerId);
|
||||
if (projectKey === req.params.projectKey) {
|
||||
let connected_sockets = await io.in(peerId).fetchSockets();
|
||||
for (let item of connected_sockets) {
|
||||
if (item.handshake.query.identity === IDENTITIES.session) {
|
||||
liveSessions[projectKey] = liveSessions[projectKey] || [];
|
||||
liveSessions[projectKey].push(item.handshake.query);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []}));
|
||||
});
|
||||
|
||||
const findSessionSocketId = async (io, peerId) => {
|
||||
const connected_sockets = await io.in(peerId).fetchSockets();
|
||||
|
|
@ -61,10 +109,28 @@ async function sessions_agents_count(io, socket) {
|
|||
return {c_sessions, c_agents};
|
||||
}
|
||||
|
||||
async function extract_live(io, socket) {
|
||||
if (io.sockets.adapter.rooms.get(socket.peerId)) {
|
||||
const connected_sockets = await io.in(socket.peerId).fetchSockets();
|
||||
|
||||
for (let item of connected_sockets) {
|
||||
if (item.handshake.query.identity === IDENTITIES.session) {
|
||||
c_sessions++;
|
||||
} else {
|
||||
c_agents++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c_agents = -1;
|
||||
c_sessions = -1;
|
||||
}
|
||||
return {c_sessions, c_agents};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
wsRouter,
|
||||
start: (server) => {
|
||||
const io = _io(server, {
|
||||
io = _io(server, {
|
||||
maxHttpBufferSize: 7e6,
|
||||
cors: {
|
||||
origin: "*",
|
||||
|
|
@ -83,10 +149,6 @@ module.exports = {
|
|||
socket.lastMessageReceivedAt = Date.now();
|
||||
let {c_sessions, c_agents} = await sessions_agents_count(io, socket);
|
||||
if (socket.identity === IDENTITIES.session) {
|
||||
connectedSessions[socket.projectKey] = connectedSessions[socket.projectKey] || [];
|
||||
if (!connectedSessions[socket.projectKey].includes(socket.sessionId)) {
|
||||
connectedSessions[socket.projectKey].push(socket.sessionId);
|
||||
}
|
||||
if (c_sessions > 0) {
|
||||
console.log(`session already connected, refusing new connexion`);
|
||||
io.to(socket.id).emit(SESSION_ALREADY_CONNECTED);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue