You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/wai-server/api/channels/call.controller.js

56 lines
1.8 KiB
JavaScript

6 months ago
// const callService = require('../../core/services/call');
// const websocketManager = require('../../core/websocket/manager');
const { callService, websocketManager } = require('../../core'); // Import from core/index.js
exports.getIn = async ctx => {
// Wait for at least one connection to be established (or handle the case where no connections are available)
// await new Promise(resolve => {
// const checkConnections = () => {
// if (websocketManager.getConnections().length > 0) {
// resolve();
// } else {
// setTimeout(checkConnections, 100); // Check again after 100ms
// }
// };
// checkConnections();
// });
const availableWs = websocketManager.getAvailableConnection();
if (!availableWs) {
// ctx.status = 503;
// ctx.body = { message: 'No available connections' };
ctx.assert(availableWs, 503, 'No available connections');
return;
}
// return availableWs;
const { sessionId, url } = callService.createSession(availableWs);
return { sessionId, url, message: 'Connection established' }; // availableWs
};
exports.sendMsg = ctx => {
const { sessionId, message } = ctx.request.body;
if (!sessionId || !message) {
// ctx.status = 400;
// ctx.body = { message: 'Session ID and message are required' };
ctx.assert(sessionId, 400, 'Session ID and message are required');
return;
}
const wsToSend = callService.getSession(sessionId);
if (!wsToSend) {
// ctx.status = 404;
// ctx.body = { message: 'Session not found' };
ctx.assert(wsToSend, 400, 'Session not found');
return;
}
try {
wsToSend.send(message);
ctx.body = { message: 'Message sent' };
} catch (error) {
console.error('Error sending message:', error);
ctx.status = 500;
ctx.body = { message: 'Failed to send message' };
}
};