|
|
|
"use strict";
|
|
|
|
//
|
|
|
|
// Na Documentação incluir os sites da simplesip.com.br no Content-Security-Policy
|
|
|
|
//
|
|
|
|
const UASimplesIP = (function () {
|
|
|
|
function carregandoScriptSimpleSIP(url, callback) {
|
|
|
|
let 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 eventoSip = new EventEmitter();
|
|
|
|
//const PROTOCOLO = "ws|wss"
|
|
|
|
|
|
|
|
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();
|
|
|
|
let localAudio = new window.Audio();
|
|
|
|
localAudio.autoplay = true;
|
|
|
|
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
|
|
|
|
const outgoingCallAudio = new window.Audio(
|
|
|
|
"https://cdn.pixabay.com/download/audio/2022/03/14/audio_461a4fb0c4.mp3?filename=phonesound-65797.mp3"
|
|
|
|
);
|
|
|
|
|
|
|
|
const endCallAudio = new window.Audio(
|
|
|
|
"https://cdn.pixabay.com/download/audio/2022/03/17/audio_5eaa35a92e.mp3?filename=mobile_phone_hanging_up-94525.mp3"
|
|
|
|
);
|
|
|
|
|
|
|
|
incomingCallAudio.loop = true;
|
|
|
|
outgoingCallAudio.loop = true;
|
|
|
|
let remoteAudio = new window.Audio();
|
|
|
|
remoteAudio.autoplay = true;
|
|
|
|
|
|
|
|
let phone;
|
|
|
|
let 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",
|
|
|
|
pcConfig: {
|
|
|
|
iceServers: [
|
|
|
|
{
|
|
|
|
urls: "stun:stun.l.google.com:19302",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urls: "stun:stun1.l.google.com:19302",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urls: "stun:stun2.l.google.com:19302",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
|
|
|
|
session = newSession;
|
|
|
|
|
|
|
|
eventoSip.on("endCall", () => {
|
|
|
|
if (session) {
|
|
|
|
eventoSip.emit("confirmedEnded");
|
|
|
|
endCallAudio.play();
|
|
|
|
session.terminate();
|
|
|
|
} else {
|
|
|
|
endCallAudio.play();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eventoSip.on("evento", (payload) => {
|
|
|
|
console.log(payload);
|
|
|
|
});
|
|
|
|
eventoSip.on("mute", () => {
|
|
|
|
session.mute();
|
|
|
|
console.log(session?.isMuted());
|
|
|
|
});
|
|
|
|
|
|
|
|
eventoSip.on("unmute", () => {
|
|
|
|
session.unmute();
|
|
|
|
console.log(session?.isMuted());
|
|
|
|
});
|
|
|
|
|
|
|
|
eventoSip.on("sendDTMF", (data) => {
|
|
|
|
if (session?.isEstablished) {
|
|
|
|
session.sendDTMF(data);
|
|
|
|
console.log("foi ativado o DTMF para: " + data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
session.on("candidate", (event) => {
|
|
|
|
console.log(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
phone.on("connectionstatechange", (event) => {
|
|
|
|
console.log("EVENTO ESTADO DA CONEXAO: ", event);
|
|
|
|
});
|
|
|
|
|
|
|
|
session.on("ended", () => {
|
|
|
|
endCallAudio.play();
|
|
|
|
eventoSip.emit("confirmedEnded");
|
|
|
|
});
|
|
|
|
|
|
|
|
//QUANDO UMA CHAMADA É REJEITADA
|
|
|
|
session.on("failed", () => {
|
|
|
|
incomingCallAudio.pause();
|
|
|
|
outgoingCallAudio.pause();
|
|
|
|
eventoSip.emit("home");
|
|
|
|
});
|
|
|
|
|
|
|
|
session.on("confirmed", function (confirmed) {
|
|
|
|
// Verifica se session.connection está definido
|
|
|
|
if (
|
|
|
|
session.connection &&
|
|
|
|
session.connection.getRemoteStreams().length > 0
|
|
|
|
) {
|
|
|
|
const remoteStreams = session.connection.getRemoteStreams()[0];
|
|
|
|
remoteAudio.srcObject = remoteStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifica se session.connection está definido e se existem streams locais
|
|
|
|
if (
|
|
|
|
session.connection &&
|
|
|
|
session.connection.getLocalStreams().length > 0
|
|
|
|
) {
|
|
|
|
const localStreams = session.connection.getLocalStreams()[0];
|
|
|
|
localAudio.srcObject = localStreams;
|
|
|
|
}
|
|
|
|
//PAUSA TODOS OS AUDIOS DE FEEDBACK
|
|
|
|
incomingCallAudio.pause();
|
|
|
|
outgoingCallAudio.pause();
|
|
|
|
|
|
|
|
eventoSip.emit("incall");
|
|
|
|
});
|
|
|
|
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) {
|
|
|
|
// Verifica se session.connection está definido
|
|
|
|
if (session.connection) {
|
|
|
|
remoteAudio.src = window.URL.createObjectURL(e.stream);
|
|
|
|
remoteAudio.play();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//RECEBENDO UMA CHAMADA
|
|
|
|
if (session.direction === "incoming") {
|
|
|
|
incomingCallAudio.play();
|
|
|
|
eventoSip.emit("incomingcall", session.remote_identity.uri.user);
|
|
|
|
|
|
|
|
eventoSip.on("rejected", () => {
|
|
|
|
if (session?.isInProgress()) {
|
|
|
|
incomingCallAudio.pause();
|
|
|
|
eventoSip.emit("home");
|
|
|
|
session.terminate();
|
|
|
|
session = null;
|
|
|
|
} else {
|
|
|
|
eventoSip.emit("home");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eventoSip.on("accepted", () => {
|
|
|
|
if (session?.isInProgress()) {
|
|
|
|
session.answer();
|
|
|
|
incomingCallAudio.pause();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//REALIZANDO UMA CHAMADA
|
|
|
|
if (session.direction === "outgoing") {
|
|
|
|
eventoSip.emit("outgoingcall", session.remote_identity.uri.user);
|
|
|
|
if (session.isInProgress()) {
|
|
|
|
outgoingCallAudio.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
phone.on("registered", function (e) {
|
|
|
|
eventoSip.emit("statusChange", "registered");
|
|
|
|
});
|
|
|
|
|
|
|
|
phone.on("unregistered", function (e) {
|
|
|
|
eventoSip.emit("statusChange", "unregistered");
|
|
|
|
});
|
|
|
|
|
|
|
|
phone.on("registrationFailed", function (e) {
|
|
|
|
eventoSip.emit("statusChange", "registrationFailed");
|
|
|
|
});
|
|
|
|
|
|
|
|
phone.on("connected", function (e) {
|
|
|
|
eventoSip.emit("statusChange", "connected");
|
|
|
|
});
|
|
|
|
|
|
|
|
phone.on("disconnected", function (e) {
|
|
|
|
eventoSip.emit("statusChange", "disconnected");
|
|
|
|
});
|
|
|
|
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 ? phone.call(numero) : eventoSip.emit("error");
|
|
|
|
},
|
|
|
|
|
|
|
|
ativaDebug: (value) => {
|
|
|
|
ativaDebug(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
unregister: () => {
|
|
|
|
phone
|
|
|
|
? phone.unregister()
|
|
|
|
: console.warn("Sem ramal cadastrado para apagar");
|
|
|
|
},
|
|
|
|
|
|
|
|
estouRegistradonoPBX: () => {
|
|
|
|
console.log("estouRegistradonoPBX ::: " + phone.isRegistered());
|
|
|
|
},
|
|
|
|
|
|
|
|
simplesipEvento: eventoSip,
|
|
|
|
|
|
|
|
help: function () {
|
|
|
|
console.log("========================================================");
|
|
|
|
console.log("=== Unified Communications - Simples Ip - Versão 1.2 ===");
|
|
|
|
console.log("========================================================");
|
|
|
|
console.log("UASimplesIP.start() - Para iniciar o SDK");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|