Spaces:
Paused
Paused
Update templates/index.html
Browse files- templates/index.html +72 -88
templates/index.html
CHANGED
|
@@ -1,113 +1,97 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
-
<meta charset="UTF-8">
|
| 5 |
-
<title>P2P File
|
| 6 |
-
<style>
|
| 7 |
-
body { font-family: Arial; margin: 50px; }
|
| 8 |
-
#fileInput { display: none; }
|
| 9 |
-
button { padding: 10px 20px; margin: 10px; }
|
| 10 |
-
</style>
|
| 11 |
</head>
|
| 12 |
<body>
|
| 13 |
-
<h2>
|
| 14 |
-
<
|
| 15 |
-
<
|
| 16 |
-
<button id="
|
| 17 |
-
<
|
| 18 |
-
<
|
| 19 |
-
<
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if(!roomId){ alert("Enter room ID"); return; }
|
| 32 |
|
| 33 |
-
|
| 34 |
-
ws.onmessage = async (event) => {
|
| 35 |
const msg = JSON.parse(event.data);
|
| 36 |
-
if(msg.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
await localConnection.setRemoteDescription(new RTCSessionDescription(msg));
|
| 40 |
const answer = await localConnection.createAnswer();
|
| 41 |
await localConnection.setLocalDescription(answer);
|
| 42 |
-
ws.send(JSON.stringify(localConnection.localDescription));
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
try { await localConnection.addIceCandidate(msg.candidate); } catch(e){}
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
localConnection = new RTCPeerConnection();
|
| 51 |
-
setupDataChannel();
|
| 52 |
|
| 53 |
-
|
| 54 |
-
if(event.candidate){
|
| 55 |
-
ws.send(JSON.stringify({type:"candidate", candidate: event.candidate}));
|
| 56 |
-
}
|
| 57 |
-
};
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
ws.onopen = () => ws.send(JSON.stringify(offer));
|
| 63 |
-
});
|
| 64 |
-
};
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
dataChannel.onmessage = event => {
|
| 73 |
-
const received = event.data;
|
| 74 |
-
if(received instanceof Blob || typeof received === "string"){
|
| 75 |
-
const blob = received instanceof Blob ? received : new Blob([received]);
|
| 76 |
const a = document.createElement("a");
|
| 77 |
-
a.href =
|
| 78 |
a.download = "received_file";
|
| 79 |
a.click();
|
| 80 |
-
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
document.getElementById("sendBtn").onclick = () => {
|
| 86 |
-
document.getElementById("fileInput").click();
|
| 87 |
-
};
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
const reader = new FileReader();
|
| 93 |
-
let offset = 0;
|
| 94 |
-
|
| 95 |
-
reader.onload = e => {
|
| 96 |
-
dataChannel.send(e.target.result);
|
| 97 |
-
offset += e.target.result.byteLength;
|
| 98 |
-
if(offset < file.size){
|
| 99 |
-
readSlice(offset);
|
| 100 |
-
} else{
|
| 101 |
-
log("File sent!");
|
| 102 |
}
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
}
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
</body>
|
| 113 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>P2P File Transfer</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
+
<h2>Peer-to-Peer File Transfer</h2>
|
| 9 |
+
<button id="createBtn">Create Room</button>
|
| 10 |
+
<input type="text" id="roomInput" placeholder="Enter Room ID">
|
| 11 |
+
<button id="joinBtn">Join Room</button>
|
| 12 |
+
<br><br>
|
| 13 |
+
<input type="file" id="fileInput">
|
| 14 |
+
<script>
|
| 15 |
+
let localConnection;
|
| 16 |
+
let dataChannel;
|
| 17 |
+
let ws;
|
| 18 |
|
| 19 |
+
function randomRoomId() {
|
| 20 |
+
return Math.random().toString(36).substring(2, 8);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
document.getElementById("createBtn").onclick = () => {
|
| 24 |
+
const roomId = randomRoomId();
|
| 25 |
+
document.getElementById("roomInput").value = roomId;
|
| 26 |
+
joinRoom(roomId, true);
|
| 27 |
+
alert("Room created: " + roomId);
|
| 28 |
+
};
|
| 29 |
|
| 30 |
+
document.getElementById("joinBtn").onclick = () => {
|
| 31 |
+
const roomId = document.getElementById("roomInput").value.trim();
|
| 32 |
+
if (roomId) joinRoom(roomId, false);
|
| 33 |
+
else alert("Enter Room ID");
|
| 34 |
+
};
|
| 35 |
|
| 36 |
+
function joinRoom(roomId, isOfferer) {
|
| 37 |
+
ws = new WebSocket(`wss://${window.location.host}/ws/${roomId}`);
|
|
|
|
| 38 |
|
| 39 |
+
ws.onmessage = async (event) => {
|
|
|
|
| 40 |
const msg = JSON.parse(event.data);
|
| 41 |
+
if (msg.sdp) {
|
| 42 |
+
await localConnection.setRemoteDescription(msg.sdp);
|
| 43 |
+
if (msg.sdp.type === "offer") {
|
|
|
|
| 44 |
const answer = await localConnection.createAnswer();
|
| 45 |
await localConnection.setLocalDescription(answer);
|
| 46 |
+
ws.send(JSON.stringify({ sdp: localConnection.localDescription }));
|
| 47 |
+
}
|
| 48 |
+
} else if (msg.candidate) {
|
| 49 |
+
await localConnection.addIceCandidate(msg.candidate);
|
|
|
|
| 50 |
}
|
| 51 |
+
};
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
localConnection = new RTCPeerConnection();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
dataChannel = isOfferer
|
| 56 |
+
? localConnection.createDataChannel("fileChannel")
|
| 57 |
+
: null;
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if (!isOfferer) {
|
| 60 |
+
localConnection.ondatachannel = (event) => {
|
| 61 |
+
dataChannel = event.channel;
|
| 62 |
+
dataChannel.onmessage = (e) => {
|
| 63 |
+
const blob = new Blob([e.data]);
|
| 64 |
+
const url = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
const a = document.createElement("a");
|
| 66 |
+
a.href = url;
|
| 67 |
a.download = "received_file";
|
| 68 |
a.click();
|
| 69 |
+
};
|
| 70 |
+
};
|
| 71 |
+
} else {
|
| 72 |
+
dataChannel.onmessage = (e) => console.log("Received:", e.data);
|
| 73 |
+
}
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
localConnection.onicecandidate = (event) => {
|
| 76 |
+
if (event.candidate) {
|
| 77 |
+
ws.send(JSON.stringify({ candidate: event.candidate }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
+
};
|
| 80 |
|
| 81 |
+
if (isOfferer) {
|
| 82 |
+
localConnection.createOffer().then(async (offer) => {
|
| 83 |
+
await localConnection.setLocalDescription(offer);
|
| 84 |
+
ws.onopen = () => ws.send(JSON.stringify({ sdp: localConnection.localDescription }));
|
| 85 |
+
});
|
| 86 |
+
}
|
| 87 |
}
|
| 88 |
+
|
| 89 |
+
document.getElementById("fileInput").onchange = (event) => {
|
| 90 |
+
const file = event.target.files[0];
|
| 91 |
+
if (file && dataChannel) {
|
| 92 |
+
file.arrayBuffer().then((buffer) => dataChannel.send(buffer));
|
| 93 |
+
}
|
| 94 |
+
};
|
| 95 |
+
</script>
|
| 96 |
</body>
|
| 97 |
</html>
|