Api v1.15.0 (#1602)
* refactor(assist): optimized assist * refactor(assist): defined a server-update event
This commit is contained in:
parent
c95a5f4254
commit
52d1d1baeb
2 changed files with 52 additions and 25 deletions
|
|
@ -11,6 +11,10 @@ const EVENTS_DEFINITION = {
|
||||||
CONNECT_ERROR: "connect_error",
|
CONNECT_ERROR: "connect_error",
|
||||||
CONNECT_FAILED: "connect_failed",
|
CONNECT_FAILED: "connect_failed",
|
||||||
ERROR: "error"
|
ERROR: "error"
|
||||||
|
},
|
||||||
|
//The following list of events will be only emitted by the server
|
||||||
|
server: {
|
||||||
|
UPDATE_SESSION: "SERVER_UPDATE_SESSION"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
EVENTS_DEFINITION.emit = {
|
EVENTS_DEFINITION.emit = {
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,10 @@ const findSessionSocketId = async (io, roomId, tabId) => {
|
||||||
|
|
||||||
async function sessions_agents_count(io, socket) {
|
async function sessions_agents_count(io, socket) {
|
||||||
let c_sessions = 0, c_agents = 0;
|
let c_sessions = 0, c_agents = 0;
|
||||||
const rooms = await getAvailableRooms(io);
|
// const rooms = await getAvailableRooms(io);
|
||||||
if (rooms.get(socket.roomId)) {
|
// if (rooms.get(socket.roomId)) {
|
||||||
const connected_sockets = await io.in(socket.roomId).fetchSockets();
|
const connected_sockets = await io.in(socket.roomId).fetchSockets();
|
||||||
|
if (connected_sockets.length > 0) {
|
||||||
for (let item of connected_sockets) {
|
for (let item of connected_sockets) {
|
||||||
if (item.handshake.query.identity === IDENTITIES.session) {
|
if (item.handshake.query.identity === IDENTITIES.session) {
|
||||||
c_sessions++;
|
c_sessions++;
|
||||||
|
|
@ -58,15 +58,15 @@ async function sessions_agents_count(io, socket) {
|
||||||
|
|
||||||
async function get_all_agents_ids(io, socket) {
|
async function get_all_agents_ids(io, socket) {
|
||||||
let agents = [];
|
let agents = [];
|
||||||
const rooms = await getAvailableRooms(io);
|
// const rooms = await getAvailableRooms(io);
|
||||||
if (rooms.get(socket.roomId)) {
|
// if (rooms.get(socket.roomId)) {
|
||||||
const connected_sockets = await io.in(socket.roomId).fetchSockets();
|
const connected_sockets = await io.in(socket.roomId).fetchSockets();
|
||||||
for (let item of connected_sockets) {
|
for (let item of connected_sockets) {
|
||||||
if (item.handshake.query.identity === IDENTITIES.agent) {
|
if (item.handshake.query.identity === IDENTITIES.agent) {
|
||||||
agents.push(item.id);
|
agents.push(item.id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
return agents;
|
return agents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ function processNewSocket(socket) {
|
||||||
socket._connectedAt = new Date();
|
socket._connectedAt = new Date();
|
||||||
socket.identity = socket.handshake.query.identity;
|
socket.identity = socket.handshake.query.identity;
|
||||||
socket.peerId = socket.handshake.query.peerId;
|
socket.peerId = socket.handshake.query.peerId;
|
||||||
let {projectKey: connProjectKey, sessionId: connSessionId, tabId:connTabId} = extractPeerId(socket.peerId);
|
let {projectKey: connProjectKey, sessionId: connSessionId, tabId: connTabId} = extractPeerId(socket.peerId);
|
||||||
socket.roomId = extractRoomId(socket.peerId);
|
socket.roomId = extractRoomId(socket.peerId);
|
||||||
socket.projectId = socket.handshake.query.projectId;
|
socket.projectId = socket.handshake.query.projectId;
|
||||||
socket.projectKey = connProjectKey;
|
socket.projectKey = connProjectKey;
|
||||||
|
|
@ -121,11 +121,16 @@ async function onConnect(socket) {
|
||||||
io.to(socket.id).emit(EVENTS_DEFINITION.emit.NO_SESSIONS);
|
io.to(socket.id).emit(EVENTS_DEFINITION.emit.NO_SESSIONS);
|
||||||
}
|
}
|
||||||
await socket.join(socket.roomId);
|
await socket.join(socket.roomId);
|
||||||
const rooms = await getAvailableRooms(io);
|
// const rooms = await getAvailableRooms(io);
|
||||||
if (rooms.has(socket.roomId)) {
|
// if (rooms.has(socket.roomId)) {
|
||||||
let connectedSockets = await io.in(socket.roomId).fetchSockets();
|
// let connectedSockets = await io.in(socket.roomId).fetchSockets();
|
||||||
|
// debug_log && console.log(`${socket.id} joined room:${socket.roomId}, as:${socket.identity}, members:${connectedSockets.length}`);
|
||||||
|
// }
|
||||||
|
let connectedSockets = await io.in(socket.roomId).fetchSockets();
|
||||||
|
if (connectedSockets.length > 0) {
|
||||||
debug_log && console.log(`${socket.id} joined room:${socket.roomId}, as:${socket.identity}, members:${connectedSockets.length}`);
|
debug_log && console.log(`${socket.id} joined room:${socket.roomId}, as:${socket.identity}, members:${connectedSockets.length}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (socket.identity === IDENTITIES.agent) {
|
if (socket.identity === IDENTITIES.agent) {
|
||||||
if (socket.handshake.query.agentInfo !== undefined) {
|
if (socket.handshake.query.agentInfo !== undefined) {
|
||||||
socket.handshake.query.agentInfo = JSON.parse(socket.handshake.query.agentInfo);
|
socket.handshake.query.agentInfo = JSON.parse(socket.handshake.query.agentInfo);
|
||||||
|
|
@ -142,6 +147,9 @@ async function onConnect(socket) {
|
||||||
// Handle update event
|
// Handle update event
|
||||||
socket.on(EVENTS_DEFINITION.listen.UPDATE_EVENT, (...args) => onUpdateEvent(socket, ...args));
|
socket.on(EVENTS_DEFINITION.listen.UPDATE_EVENT, (...args) => onUpdateEvent(socket, ...args));
|
||||||
|
|
||||||
|
// Handle server update event
|
||||||
|
socket.on(EVENTS_DEFINITION.server.UPDATE_SESSION, (...args) => onUpdateServerEvent(socket, ...args));
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
socket.on(EVENTS_DEFINITION.listen.ERROR, err => errorHandler(EVENTS_DEFINITION.listen.ERROR, err));
|
socket.on(EVENTS_DEFINITION.listen.ERROR, err => errorHandler(EVENTS_DEFINITION.listen.ERROR, err));
|
||||||
socket.on(EVENTS_DEFINITION.listen.CONNECT_ERROR, err => errorHandler(EVENTS_DEFINITION.listen.CONNECT_ERROR, err));
|
socket.on(EVENTS_DEFINITION.listen.CONNECT_ERROR, err => errorHandler(EVENTS_DEFINITION.listen.CONNECT_ERROR, err));
|
||||||
|
|
@ -186,19 +194,34 @@ async function onUpdateEvent(socket, ...args) {
|
||||||
args[0] = {meta: {tabId: socket.tabId, version: 1}, data: args[0]};
|
args[0] = {meta: {tabId: socket.tabId, version: 1}, data: args[0]};
|
||||||
}
|
}
|
||||||
Object.assign(socket.handshake.query.sessionInfo, args[0].data, {tabId: args[0]?.meta?.tabId});
|
Object.assign(socket.handshake.query.sessionInfo, args[0].data, {tabId: args[0]?.meta?.tabId});
|
||||||
socket.to(socket.roomId).emit(EVENTS_DEFINITION.emit.UPDATE_EVENT, args[0]);
|
// socket.to(socket.roomId).emit(EVENTS_DEFINITION.emit.UPDATE_EVENT, args[0]);
|
||||||
|
socket.to(socket.roomId).emit(EVENTS_DEFINITION.server.UPDATE_SESSION, args[0]);
|
||||||
// Update sessionInfo for all sessions in room
|
// Update sessionInfo for all sessions in room
|
||||||
const rooms = await getAvailableRooms(io);
|
// const rooms = await getAvailableRooms(io);
|
||||||
for (let roomId of rooms.keys()) {
|
// for (let roomId of rooms.keys()) {
|
||||||
if (roomId === socket.roomId) {
|
// if (roomId === socket.roomId) {
|
||||||
const connected_sockets = await io.in(roomId).fetchSockets();
|
// const connected_sockets = await io.in(roomId).fetchSockets();
|
||||||
for (let item of connected_sockets) {
|
const connected_sockets = await io.in(socket.roomId).fetchSockets();
|
||||||
if (item.handshake.query.identity === IDENTITIES.session && item.handshake.query.sessionInfo) {
|
for (let item of connected_sockets) {
|
||||||
Object.assign(item.handshake.query.sessionInfo, args[0]?.data, {tabId: args[0]?.meta?.tabId});
|
if (item.handshake.query.identity === IDENTITIES.session && item.handshake.query.sessionInfo) {
|
||||||
}
|
Object.assign(item.handshake.query.sessionInfo, args[0]?.data, {tabId: args[0]?.meta?.tabId});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onUpdateServerEvent(socket, ...args) {
|
||||||
|
debug_log && console.log(`Server sent update event through ${socket.id}.`);
|
||||||
|
if (socket.identity !== IDENTITIES.session) {
|
||||||
|
debug_log && console.log('Ignoring server update event.');
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Back compatibility (add top layer with meta information)
|
||||||
|
if (args[0]?.meta === undefined && socket.identity === IDENTITIES.session) {
|
||||||
|
args[0] = {meta: {tabId: socket.tabId, version: 1}, data: args[0]};
|
||||||
|
}
|
||||||
|
Object.assign(socket.handshake.query.sessionInfo, args[0].data, {tabId: args[0]?.meta?.tabId});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onAny(socket, eventName, ...args) {
|
async function onAny(socket, eventName, ...args) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue