Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect to the last call #1078

Open
minhtruong92 opened this issue Apr 17, 2024 · 1 comment
Open

Reconnect to the last call #1078

minhtruong92 opened this issue Apr 17, 2024 · 1 comment

Comments

@minhtruong92
Copy link

Hi all, I am newbie with WebRTC, I am using SIP.JS to make outbound/inbound call in Web. It works well but if user refresh the browser, I don't know how to reconnect last call session with SIP.JS

I am using Asterisk 18, SIP.JS 0.21.2

@thanossOp
Copy link

can you provide me code for reciver side like i create reciver side but its not get any invite

<title>SIP Calling Receiver</title>

SIP Calling Receiver

Incoming call from UserA

Answer Call Reject Call
<script src="https://cdnjs.cloudflare.com/ajax/libs/sip.js/0.20.0/sip.min.js"></script>
<script>
  let userAgent;
  let currentSession;

  const socket = new WebSocket("ws://localhost:8080");

  socket.onopen = () => {
    console.log("WebSocket connected");
  };

  socket.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log("Received message:", data);

    if (data.event === "incoming_call") {
      document.getElementById("callModal").style.display = "block";
    } else if (data.event === "endCall") {
      endCall();
    }
  };

  async function answerCall() {
    if (!userAgent) {
      try {
        const uri = new SIP.URI(
          "sip",
          "UserB",
          "recieverside.sip.us1.twilio.com"
        );

        const sipConfig = {
          uri: uri,
          transportOptions: {
            wsServers: "ws://localhost:8080",
          },
          authorizationUser: "user",
          password: "password",
          delegate: {
            onInvite,
          },
        };

        console.log("sipConfig", sipConfig);

        userAgent = new SIP.UserAgent(sipConfig);
        //await userAgent.start();
        console.log("userAgent", userAgent);

        document.getElementById("callModal").style.display = "none";
        socket.send(JSON.stringify({ event: "accepted" }));
      } catch (error) {
        console.error("Error accessing user media:", error);
      }
    }
  }

  function onInvite(invitation) {
    const stream = navigator.mediaDevices.getUserMedia({
      audio: true,
      video: false,
    });
    console.log("Incoming call invitation received:", invitation);

    // Handle incoming call acceptance
    try {
      invitation.accept({
        sessionDescriptionHandlerOptions: {
          constraints: {
            stream: stream,
          },
        },
      });

      console.log("Call accepted successfully.");

      // Attach the incoming audio stream to an audio element
      const remoteAudio = document.getElementById("remoteAudio");
      remoteAudio.srcObject = invitation.remoteMediaStream;
    } catch (e) {
      console.error("Error accepting call:", e);
    }
  }

  function rejectCall() {
    // Hide the call modal after rejecting
    document.getElementById("callModal").style.display = "none";
    socket.send(JSON.stringify({ event: "rejected" }));
  }
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants