After extensive research and Google searches, I have successfully obtained the local client IP address using JQuery in my Angular 6 project. However, upon building the application, I encountered errors that I believe are due to the JQuery code (specifically window.something) integrated within Angular.
Does anyone know how to achieve this solely with Angular, without relying on JQuery or third-party URLs like and other similar websites?
Code:
ngOnInit() {
var self = this;
var localIp1 = "";
$(document).ready(function(){
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice){
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
var localIp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
// this.localIpAddress = myIP;
localIp = localIp.replace(/\./g, '-');
sessionStorage.setItem("LOCAL_IP", localIp);
$.ajax({
type: "GET",
dataType: "json",
url: "http://localhost:8080/api/setLocalIpAddress/" + localIp,
success: function(data){
console.log(data);
}
});
};
});
this.localIp = sessionStorage.getItem("LOCAL_IP");
}
Error:
ERROR in src/app/selectline/select-line.component.ts(35,20): error TS2339: Property 'RTCPeerConnection' does not exist on type 'Window'.
src/app/selectline/select-line.component.ts(35,47): error TS2339: Property 'RTCPeerConnection' does not exist on type 'Window'.
src/app/selectline/select-line.component.ts(35,75): error TS2339: Property 'mozRTCPeerConnection' does not exist on type 'Window'.
src/app/selectline/select-line.component.ts(35,106): error TS2339: Property 'webkitRTCPeerConnection' does not exist on type 'Window'.
src/app/selectline/select-line.component.ts(39,16): error TS2339: Property 'createDataChannel' does not exist on type 'RTCPeerConnection'.
Thanks a lot in advance for any help!