This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues or pull requests.
vn-softphone/static/js/ua.js

171 lines
4.3 KiB
JavaScript

const socketHost = 'ws://pbx.dyn.verdnatura.es:8088/asterisk/ws';
const uriHost = 'pbx.dyn.verdnatura.es';
var options = {
mediaConstraints: {
audio: true,
video: false
},
session_timers: false,
volume: 0.5,
microphone: true
};
let socket = new JsSIP.WebSocketInterface(socketHost);
let configuration = {
sockets : [ socket ],
uri : 'sip:8000@pbx.dyn.verdnatura.es',
password : '123456',
authorizationUser: '8000',
hackIpInContact: true,
rtcpMuxPolicy: 'negotiate',
hackWssInTransport: true
};
let ua = new JsSIP.UA(configuration);
/**
* Show event message
* @param {Object} event
*/
function eventStatus(event) {
if (event.cause === JsSIP.C.causes.CONNECTION_ERROR)
return notify('Ha ocurrido un error de conexión');
if (event.cause === JsSIP.C.causes.INTERNAL_ERROR)
return notify('Ha ocurrido un error inesperado');
if (event.cause === JsSIP.C.causes.AUTHENTICATION_ERROR)
return notify('No se ha podido autenticar la sesión');
if (event.cause === JsSIP.C.causes.UNAVAILABLE)
return notify('El destinatario no está disponible');
if (event.cause === JsSIP.C.causes.BUSY)
return notify('El destinatario está ocupado');
if (event.cause === JsSIP.C.causes.REJECTED)
return notify('La conexión ha sido rechazada')
if (event.cause === JsSIP.C.causes.NOT_FOUND)
return notify('No se ha podido encontrar el destinatario');
if (event.cause === JsSIP.C.causes.REQUEST_TIMEOUT)
return notify('El destinatario no ha respondido la llamada');
if (event.cause === JsSIP.USER_DENIED_MEDIA_ACCESS)
return notify('Es necesario permitir el acceso al micrófono');
}
/**
* UA register
*/
ua.on('registered', function(event) {
$('callStatus').innerHTML = 'Conectado';
});
/**
* Client disconnection
*/
ua.on('disconnected', function(event) {
$('callStatus').innerHTML = 'Desconectado';
});
ua.on('registrationFailed', function(event) {
Softphone.notifyStatus(event);
$('callStatus').innerHTML = 'Desconectado';
});
/**
* Call session
*/
ua.on('newRTCSession', function(event) {
let session = event.session;
ua.sessionInstance = session;
console.log(session);
session.on('confirmed', function() {
console.log('confirmed');
})
session.on('reinvite', function() {
console.log('reinvite');
})
session.on('update', function() {
console.log('update');
})
/**
* Call in progress
*/
session.on('progress', function(event) {
console.log('progress');
if (session.direction === 'incoming') {
audio.addTrack('incoming');
audio.play(true);
$('callStatus').innerHTML = 'Llamada entrante...';
} else {
audio.addTrack('outgoing');
audio.play(true);
$('callStatus').innerHTML = 'Llamando...';
}
});
/**
* Call accepted
*/
session.on('accepted', function(event) {
console.log('accepted');
$('callStatus').innerHTML = 'Llamada conectada';
if (session.connection.getRemoteStreams().length > 0) {
let stream = session.connection.getRemoteStreams()[0];
audio.addStream(stream);
audio.play();
}
startTimer();
});
session.on('hold', function(event) {
$('callStatus').innerHTML = 'Llamada en espera';
});
session.on('unhold', function(event) {
$('callStatus').innerHTML = 'Llamada conectada';
});
session.on('refer', function() {
$('callStatus').innerHTML = 'Llamada transferida';
});
/**
* Call ended
*/
session.on('ended', function(event) {
audio.addTrack('hangup');
audio.play();
stopTimer();
$('callStatus').innerHTML = 'Conectado';
ua.sessionInstance = null;
eventStatus(event);
});
/**
* Call failed
*/
session.on('failed', function(event) {
audio.addTrack('hangup');
audio.play();
$('callStatus').innerHTML = 'Conectado';
ua.sessionInstance = null;
eventStatus(event);
});
});
ua.start();