最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to know if remote peer is disconnected when using Cloudflare Calls SFU? - Stack Overflow

programmeradmin3浏览0评论

Is there any way to determine if a remote peer is disconnected while using the Cloudflare SFU without a signaling server?

Suppose there are five users (A, B, C, D, E) connected to the SFU. And C, E disconnects. I would like to know would A, B, D knows about the disconnection via peer connection.

One solution would be implementing a signaling server that sends a heartbeat every 5 seconds. If no response is received, the session could be terminated.

However, since the client's peer connection is aware of other connections, I assume it should detect the disconnection.

This is what I have tried to check, but to no avail.

 peerConnection.ontrack = (event: RTCTrackEvent) => {
  const checkTrack = setInterval(() => {
    if (event.track.readyState === "ended") {
      console.log(`Track ${event.track.id} is closed with mid ${event.transceiver.mid}`);
      clearInterval(checkTrack);
    }

    if (event.transceiver.currentDirection === 'stopped' || event.transceiver.currentDirection === 'inactive') {
      console.log(`Transceiver ${event.track.id} is closed with mid ${event.transceiver.mid}`);
      clearInterval(checkTrack);
    }
  }, 500);

  event.track.onended = () => {
    console.log(`${event.transceiver.mid} is ended, ${event.track.id}`);
  };

  event.track.onmute = () => {
    console.log(`${event.transceiver.mid} is muted, ${event.track.id}`);
  };
};

Is there any way to determine if a remote peer is disconnected while using the Cloudflare SFU without a signaling server?

Suppose there are five users (A, B, C, D, E) connected to the SFU. And C, E disconnects. I would like to know would A, B, D knows about the disconnection via peer connection.

One solution would be implementing a signaling server that sends a heartbeat every 5 seconds. If no response is received, the session could be terminated.

However, since the client's peer connection is aware of other connections, I assume it should detect the disconnection.

This is what I have tried to check, but to no avail.

 peerConnection.ontrack = (event: RTCTrackEvent) => {
  const checkTrack = setInterval(() => {
    if (event.track.readyState === "ended") {
      console.log(`Track ${event.track.id} is closed with mid ${event.transceiver.mid}`);
      clearInterval(checkTrack);
    }

    if (event.transceiver.currentDirection === 'stopped' || event.transceiver.currentDirection === 'inactive') {
      console.log(`Transceiver ${event.track.id} is closed with mid ${event.transceiver.mid}`);
      clearInterval(checkTrack);
    }
  }, 500);

  event.track.onended = () => {
    console.log(`${event.transceiver.mid} is ended, ${event.track.id}`);
  };

  event.track.onmute = () => {
    console.log(`${event.transceiver.mid} is muted, ${event.track.id}`);
  };
};
Share Improve this question edited Apr 2 at 12:53 Prateek Thapa asked Apr 2 at 4:19 Prateek ThapaPrateek Thapa 4,9481 gold badge11 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

This is not specific to Cloudflare Calls at all. Since you PeerConnection terminates at the SFU you no longer "see" what happens with the PeerConnections of the other peers connected to the same room on the SFU. You can look at your own PC in a pure P2P call, but not when using an SFU. The two options to solve this would be:

  1. Use a signaling server, which I would assume you need anyway to setup the call.
  2. Technically the SFU could send your endpoint some kind of event if it detects that one of the other PeerConnections disconnected. But I'm not sure if any pure SFU without a signaling server supports something like that.

You can use WebRTC’s connectionState and iceConnectionState events on your RTCPeerConnection to reliably identify disconnections. These events would fire when the connection state changes.

peerConnection.onconnectionstatechange = () => {
  console.log("Connection State:", peerConnection.connectionState);

  if (peerConnection.connectionState === "disconnected" || 
      peerConnection.connectionState === "failed" || 
      peerConnection.connectionState === "closed") {
    console.log("Remote peer disconnected!");
    // Handle disconnection logic here
  }
};

peerConnection.oniceconnectionstatechange = () => {
  console.log("ICE Connection State:", peerConnection.iceConnectionState);

  if (peerConnection.iceConnectionState === "disconnected" ||
      peerConnection.iceConnectionState === "failed") {
    console.log("ICE Connection Disconnected!");
    // Handle disconnection logic here
  }
};

发布评论

评论列表(0)

  1. 暂无评论