This commit is contained in:
wow22831 2024-11-07 15:10:32 +07:00
parent 0ccca0922c
commit 2d94537192
3 changed files with 38 additions and 22 deletions

5
package-lock.json generated
View File

@ -16,7 +16,7 @@
"@testing-library/user-event": "^12.6.3",
"@types/ip": "^1.1.3",
"@types/jest": "^26.0.20",
"@types/node": "^12.19.15",
"@types/node": "^12.20.55",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@types/react-router": "^5.1.11",
@ -3864,7 +3864,8 @@
"node_modules/@types/node": {
"version": "12.20.55",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.55.tgz",
"integrity": "sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ=="
"integrity": "sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==",
"license": "MIT"
},
"node_modules/@types/node-forge": {
"version": "1.3.11",

View File

@ -11,7 +11,7 @@
"@testing-library/user-event": "^12.6.3",
"@types/ip": "^1.1.3",
"@types/jest": "^26.0.20",
"@types/node": "^12.19.15",
"@types/node": "^12.20.55",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@types/react-router": "^5.1.11",

View File

@ -7,7 +7,7 @@ import {
} from '@ionic/react';
import { call as callIcon, close as hangUpIcon, shirt } from 'ionicons/icons';
import Peer, { MediaConnection } from 'peerjs';
import * as os from 'os';
import os from 'os';
const ExploreContainer: React.FC = () => {
const [callString, setCallString] = useState("");
@ -22,30 +22,45 @@ const ExploreContainer: React.FC = () => {
const [callInterval, setCallInterval] = useState<NodeJS.Timeout | null>(null);
const [currentCallId, setCurrentCallId] = useState<string | null>(null);
const [showCopyNotification, setShowCopyNotification] = useState(false);
const [ipv5, setIpv5] = useState('')
function getLocalIPAddress() {
const networkInterfaces = os.networkInterfaces();
for (const interfaceName of Object.keys(networkInterfaces)) {
const networkInterface = networkInterfaces[interfaceName];
if (networkInterface) {
for (const net of networkInterface) {
if (net.family === 'IPv4' && !net.internal) {
console.log(`Your local IP Address is: ${net.address}`);
return net.address;
}
}
}
}
return '';
}
const getLocalIP = async () => {
return new Promise((resolve, reject) => {
const peerConnection = new RTCPeerConnection();
// const noop = () => {};
getLocalIPAddress();
// Обрабатываем события ICE-кандидатов, которые будут содержать IP-адрес
peerConnection.onicecandidate = (event) => {
if (event.candidate) {
const ip = event.candidate.candidate.split(" ")[4];
resolve(ip);
// peerConnection.onicecandidate = noop; // Завершаем обработку
}
};
// Создаём пустой канал данных для инициирования связи
peerConnection.createDataChannel("");
peerConnection.createOffer()
.then((offer) => peerConnection.setLocalDescription(offer))
.catch((err) => reject(err));
});
};
// Использование функции
getLocalIP().then((ip) => {
console.log("Local IP Address:", ip);
setIpv5(ip as string)
}).catch((err) => {
setIpv5("error")
console.error("Error getting IP address:", err);
});
useEffect(() => {
const initializePeer = () => {
const newPeer = new Peer('',{ debug: 3 });
console.log(ipv5)
const newPeer = new Peer( ipv5 ,{ debug: 3 });
// const newPeer = new Peer(,{ debug: 3 });
setPeer(newPeer);