You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

195 lines
6.9 KiB

"use strict";
//
// Na Documentação incluir os sites da simplesip.com.br no Content-Security-Policy
//
const UASimplesIP = (function() {
function carregandoScriptSimpleSIP(url, callback) {
var script = document.createElement('script');
script.src = url;
script.async = true; // Asynchronous loading
script.onload = function() {
if (typeof callback === 'function') {
callback();
}
// Clean up after script execution
script.onload = script.onerror = null;
document.head.removeChild(script);
};
script.onerror = function() {
// Handle errors
console.error('Error loading script:', url);
if (typeof callback === 'function') {
callback(new Error('Error loading script'));
}
// Clean up after script execution
script.onload = script.onerror = null;
document.head.removeChild(script);
};
document.head.appendChild(script);
}
carregandoScriptSimpleSIP("simplesiplib.js", (error) => {
if (!error) {
console.log("::: Script loaded successfully!");
} else {
console.error(":::: Failed to load script:", error);
}
});
class EventEmitter {
constructor() {
this.events = {};
}
on(eventName, listener) {
if (!this.events[eventName]) {
this.events[eventName] = [];
}
this.events[eventName].push(listener);
}
emit(eventName, ...args) {
if (this.events[eventName]) {
this.events[eventName].forEach(listener => listener(...args));
}
}
off(eventName, listener) {
if (this.events[eventName]) {
this.events[eventName] = this.events[eventName].filter(fn => fn !== listener);
}
}
}
const meuEvento = new EventEmitter();
meuEvento.on("evento", (payload) => {
console.log("hello eventos", payload);
});
setInterval(() => {
//meuEvento.emit("evento", _Autenticacao);
}, 5000);
//const PROTOCOLO = "ws|wss"
function passa(nome) {
console.log(SimpleSIP.settings);
}
function Autenticacao(PROTOCOLO, SERVIDOR, PORTA, NOME, RAMAL, SENHA) {
this.PROTOCOLO = PROTOCOLO;
this.SERVIDOR = SERVIDOR;
this.PORTA = PORTA;
this.NOME = NOME;
this.RAMAL = RAMAL;
this.SENHA = SENHA;
}
const _Autenticacao = new Autenticacao();
const incomingCallAudio = new window.Audio('https://cdn.pixabay.com/download/audio/2021/08/04/audio_bb630cc098.mp3?filename=short-success-sound-glockenspiel-treasure-video-game-6346.mp3');
incomingCallAudio.loop = true;
var remoteAudio = new window.Audio();
remoteAudio.autoplay = true;
var phone;
var session;
function iniciandoAutenticacaonoPBX() {
const configuration = {
uri: "sip:" + _Autenticacao.RAMAL + "@" + _Autenticacao.SERVIDOR,
password: _Autenticacao.SENHA,
sockets: new SimpleSIP.WebSocketInterface(_Autenticacao.PROTOCOLO + "://" + _Autenticacao.SERVIDOR + ":" + _Autenticacao.PORTA + "/ws"),
display_name: (_Autenticacao.NOME !== null?_Autenticacao.NOME:_Autenticacao.RAMAL),
user_agent: "UASimpleSIP 1.2.0"
};
if (configuration.uri && configuration.password) {
phone = new SimpleSIP.UA(configuration);
phone.on('registrationFailed', function(ev){
console.log('Registering on SIP server failed with error: ' + ev.cause);
configuration.uri = null;
configuration.password = null;
});
phone.on('newRTCSession',function(ev){
const newSession = ev.session;
if (session) {
session.terminate();
}
session = newSession;
var completeSession = function(){
session = null;
};
session.on('ended', completeSession);
session.on('failed', completeSession);
session.on('accepted', (accepted) => {
console.log(accepted);
});
session.on('confirmed', function(confirmed){
console.log('confirmed ::::::::::::::::::::::::::::::::::::::::::::::::::::', confirmed)
const remoteStreams = session.connection.getRemoteStreams()[0];
remoteAudio.srcObject = remoteStreams;
});
session.on("icecandidate", function (event) {
if (event.candidate.type === "srflx" &&
event.candidate.relatedAddress !== null &&
event.candidate.relatedPort !== null) {
event.ready();
}
});
session.on('addstream', function(e){
incomingCallAudio.pause();
remoteAudio.src = window.URL.createObjectURL(e.stream);
});
if(session.direction === 'incoming'){
incomingCallAudio.play();
}
});
phone.start();
}
}
function ativaDebug(value) { ( value === true?SimpleSIP.debug.enable('SimpleSIP:*'):SimpleSIP.debug.disable('SimpleSIP:*')); }
return {
config: _Autenticacao,
start: function() {
if ( _Autenticacao.PROTOCOLO === undefined || _Autenticacao.SERVIDOR === undefined || _Autenticacao.RAMAL === undefined || _Autenticacao.SENHA === undefined ) {
console.error(":::: Configuração do Usuário Inválida para Autenticação no PBX !");
}else{
ativaDebug(true);
iniciandoAutenticacaonoPBX();
}
},
realizaUmaChamada: (numero) => {
phone.call(numero);
},
ativaDebug: (value) => {
ativaDebug(value);
},
estouRegistradonoPBX: () => {
console.log("estouRegistradonoPBX ::: " + phone.isRegistered())
},
myEvento: meuEvento,
help: function() {
console.log("========================================================");
console.log("=== Unified Communications - Simples Ip - Versão 1.2 ===");
console.log("========================================================");
console.log("UASimplesIP.start() - Para iniciar o SDK");
}
};
})();