<!DOCTYPE html>
<html lang="pt-BR">
<head>
  <meta charset="UTF-8" />
  <title>Teste SIP.js Registro</title>
  <script src="https://cdn.jsdelivr.net/npm/sip.js@0.20.0/dist/sip.min.js"></script>
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; max-width: 500px; }
    label, input, button { display: block; width: 100%; margin-bottom: 10px; }
    #status { margin-top: 20px; font-weight: bold; }
  </style>
</head>
<body>
  <h2>Teste Registro SIP com SIP.js</h2>

  <label>Servidor SIP (ex: voipdobrasil.net.br):</label>
  <input type="text" id="server" value="voipdobrasil.net.br" />

  <label>Usuário SIP:</label>
  <input type="text" id="username" value="7883190" />

  <label>Senha SIP:</label>
  <input type="password" id="password" value="Aa123456@1" />

  <button id="btnConnect">Conectar SIP</button>

  <div id="status">Status: Não conectado</div>

  <script>
    let userAgent;

    document.getElementById('btnConnect').onclick = () => {
      const server = document.getElementById('server').value.trim();
      const username = document.getElementById('username').value.trim();
      const password = document.getElementById('password').value.trim();
      const statusDiv = document.getElementById('status');

      if (!server || !username || !password) {
        alert('Preencha todos os campos!');
        return;
      }

      if (userAgent) {
        userAgent.stop();
        userAgent = null;
      }

      statusDiv.textContent = 'Status: Conectando...';

      const uri = `sip:${username}@${server}`;

      userAgent = new SIP.UA({
        uri: uri,
        transportOptions: {
          wsServers: [`wss://${server}:7443`]
        },
        authorizationUser: username,
        password: password,
        register: true,
        traceSip: true,
        hackIpInContact: true
      });

      userAgent.on('registered', () => {
        statusDiv.textContent = 'Status: Registrado com sucesso!';
      });

      userAgent.on('registrationFailed', (error) => {
        statusDiv.textContent = 'Status: Falha no registro: ' + error.cause;
      });

      userAgent.on('unregistered', () => {
        statusDiv.textContent = 'Status: Desregistrado';
      });

      userAgent.on('disconnected', () => {
        statusDiv.textContent = 'Status: Desconectado do servidor SIP';
      });
    };
  </script>
</body>
</html>
