Pular para o conteúdo principal
Use o áudio ao vivo via WebRTC quando um usuário precisar falar e ouvir através de um navegador.

Fluxo

  1. Conecte e escaneie o token do WaGo.
  2. Inicie ou atenda uma chamada.
  3. Obtenha a configuração do WebRTC.
  4. Crie um RTCPeerConnection no navegador.
  5. Adicione a trilha do microfone.
  6. Envie a oferta SDP para o WaGo.
  7. Defina a resposta SDP do WaGo como a descrição remota.

Obter configuração do WebRTC

curl -H "token: YOUR_TOKEN" \
  http://localhost:1337/call/webrtc/config
Resposta:
{
  "iceServers": [
    {
      "urls": ["stun:stun.l.google.com:19302"]
    }
  ],
  "turn": {
    "enabled": false,
    "status": "coming_soon"
  }
}
O TURN ainda não é configurável. O WaGo utiliza apenas STUN no momento.

Enviar uma oferta SDP

curl -X POST http://localhost:1337/call/webrtc/offer \
  -H "Content-Type: application/json" \
  -H "token: YOUR_TOKEN" \
  -d '{
    "callID": "CALL_ID",
    "type": "offer",
    "sdp": "BROWSER_SDP_OFFER"
  }'
O WaGo retorna uma resposta SDP:
{
  "callID": "CALL_ID",
  "type": "answer",
  "sdp": "WAGO_SDP_ANSWER",
  "turn": {
    "enabled": false,
    "status": "coming_soon"
  }
}

Exemplo para navegador

const config = await fetch("https://your-wago-server.com/call/webrtc/config", {
  headers: { token: "YOUR_TOKEN" }
}).then((res) => res.json());

const pc = new RTCPeerConnection({
  iceServers: config.data?.iceServers || config.iceServers
});

const stream = await navigator.mediaDevices.getUserMedia({
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    autoGainControl: true
  }
});

for (const track of stream.getAudioTracks()) {
  const sender = pc.addTrack(track, stream);
  const transceiver = pc.getTransceivers().find((item) => item.sender === sender);
  if (transceiver) transceiver.direction = "sendrecv";
}

pc.ontrack = (event) => {
  const audio = document.querySelector("audio");
  audio.srcObject = event.streams[0];
  audio.play();
};

const offer = await pc.createOffer();
await pc.setLocalDescription(offer);

await new Promise((resolve) => {
  if (pc.iceGatheringState === "complete") return resolve();
  pc.addEventListener("icegatheringstatechange", () => {
    if (pc.iceGatheringState === "complete") resolve();
  });
});

const answer = await fetch("https://your-wago-server.com/call/webrtc/offer", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    token: "YOUR_TOKEN"
  },
  body: JSON.stringify({
    callID: "CALL_ID",
    type: pc.localDescription.type,
    sdp: pc.localDescription.sdp
  })
}).then((res) => res.json());

const payload = answer.data || answer;
await pc.setRemoteDescription({
  type: payload.type,
  sdp: payload.sdp
});

Fechar a ponte WebRTC

curl -X POST http://localhost:1337/call/webrtc/close \
  -H "Content-Type: application/json" \
  -H "token: YOUR_TOKEN" \
  -d '{
    "callID": "CALL_ID"
  }'
Isso fecha a ponte de mídia do navegador. Não encerra a chamada do WhatsApp. Use /call/hangup para finalizar a chamada.

Requisito de HTTPS

O acesso ao microfone pelo navegador exige HTTPS em produção. localhost é a principal exceção para desenvolvimento local.