G
G
GizzaProger2020-08-20 09:53:29
JavaScript
GizzaProger, 2020-08-20 09:53:29

Why is it not always possible to connect p2p (webrtc)?

Hello.
I can not understand why it is not always possible to connect p2p using simple-peer?
If I try to connect from different browser tabs, then everything is fine. Connects quickly and in all cases. If I try to connect from different PCs, it doesn't always work. Today the connection may pass, but tomorrow it may not. It happens that if the initator is the first PC, then they will connect, but if the second one is not. I can't figure out why this is happening. I thought that the messages were not delivered correctly, but from my PC there can be as many participants as you like. I thought it was a firewall, but then there would not have been such a chaotic result with connections. Maybe someone faced similar?
Soldering two peers with socket io.
1) I get an array of users
2) For each user I create a peer initator
3) As soon as the user signals, I send a message to the user from the array of users by id.
4) As soon as I receive the initator's message, I accept its signal
5) As soon as the receiver gives its signal, I send it back to the initator
6) I receive the signal on the side of the initator and use it
Here is the code

intiatorOnSignal(peer, userId) {
      // Как только инитатор просигналил
      peer.on("signal", signal => {
        console.log("Пытаемся просигналить (инитатор)");
        // Проверка на то, что этот пир еще не сигналил и то, что сигнал это не новое предложение о подключении
        // if (!this.getPeerById(userId).signaling || signal.type != "offer") {
        this.getPeerById(userId).signaling = true;
        // Отправляем предложение о соеденении
        console.log("Инитатор отправляет сигнал ", userId);
        this.sendMsgToUser(
          {
            signal,
            name: "signaling initiator",
            username: this.user.name,
            avatar: this.user.avatar_link,
            sender: this.socketId,
            forPeer: userId
          },
          userId
        );
        console.log("Успешно просигналили юзеру");
        // }
      });
    },
    onGetMsg() {
      this.socket.on("get msg", data => {
        switch (data.msg.name) {
          // Получаем предложение о соеденении
          case "signaling initiator":
            // 4
            this.getMedia(stream => {
              let depended = this.getDependedPeer(stream, data);
              let peer = depended.peer;
              // Как только просигналили,
              // if (depended.isNewPeer) {
              peer.on("signal", signal => {
                console.log("Получили сигнал инитатора");
                // Отправляем инитатору ответный сигнал
                console.log("Отправляеум ответный сигнал ", data.msg.forPeer);
                this.sendMsgToUser(
                  {
                    sendeId: this.id,
                    sender: this.socketId,
                    signal,
                    name: "signaling answer",
                    forPeer: data.msg.forPeer
                  },
                  data.msg.sender
                );
              });
              // }
              peer.signal(data.msg.signal);
            });
            break;
          case "signaling answer":
            console.log("Получили сигнал ответа");
            // 5
            // Получаем ответный сигнал от пира и соеденеяемся с ним
            // if (data.msg.signal.type == "answer") {
            if (this.getPeerById(data.msg.forPeer)) {
              console.log(this.getPeerById(data.msg.forPeer))
              this.getPeerById(data.msg.forPeer).peer.signal(data.msg.signal);
              console.log("Использовали сигнал ответа");
            }
            // }
            break;
        }
      });
    },

Here is the error
Error: Connection failed.
at makeError (index.js?051a:18)
at Peer._onConnectionStateChange (index.js?051a:660)
at RTCPeerConnection.Peer._pc.onconnectionstatechange (index.js?051a:122)
I don't know what could be wrong. Any version will help me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Vasiliev, 2020-08-22
@GizzaProger

Maybe NAT?
https://habr.com/ru/company/Voximplant/blog/417869/
How to overcome NAT (webrtc)?
https://www.frozenmountain.com/developers/blog/web...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question