Ошибка с window.RTCPeerConnection в Angular получить локальный IP? - PullRequest
0 голосов
/ 05 августа 2020

Мне нужно просмотреть локальный IP-адрес клиента.

Я нашел этот код:

getIdentificativo() {
    this.determineLocalIp();
  }

  private determineLocalIp() {
    window.RTCPeerConnection = this.getRTCPeerConnection();

    const pc = new RTCPeerConnection({iceServers: []});
    pc.createDataChannel('');
    pc.createOffer().then(pc.setLocalDescription.bind(pc));

    pc.onicecandidate = (ice) => {
      this.zone.run(() => {
        if (!ice || !ice.candidate || !ice.candidate.candidate) {
          return;
        }

        this.localIp = this.ipRegex.exec(ice.candidate.candidate)[1];
        sessionStorage.setItem('LOCAL_IP', this.localIp);

        pc.onicecandidate = () => {
        };
        pc.close();
      });
    };
  }

  private getRTCPeerConnection() {
    return window.RTCPeerConnection ||
      window.mozRTCPeerConnection ||
      window.webkitRTCPeerConnection;
  }

Но у меня есть эта ошибка:

ERROR в src / app / app.component.ts: 31: 5 - ошибка TS2322: Тип «RTCPeerConnection» не может быть назначен типу «RTCPeerConnection & {новое (конфигурация ?: RTCConfig uration): RTCPeerConnection; прототип: RTCPeerConnection; generateCertificate (keygenAlgorithm: строка | Алгоритм): Promise <...>; getDefaultIceServers (): RTCIceSe rver []; } '. В типе RTCPeerConnection отсутствуют следующие свойства типа {new (конфигурация ?: RTCConfiguration): RTCPeerConnection; прототип: RTCPeerConnection; generateCertificate (keygenAlgorithm: строка | Алгоритм): Promise <...>; getDefaultIceServers (): RTCIceServer []; } ': prototype, generateCertificate, getDefaul tIceServers

В журнале консоли Chrome У меня есть эта ошибка:

Uncaught TypeError: Cannot read property '1' of null

Эта ошибка относится к коду:

this.localIp = this.ipRegex.exec(ice.candidate.candidate)[1];

Помогите !!!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...