triflix commited on
Commit
0a02960
·
verified ·
1 Parent(s): f0a8a5e

Update templates/index.html

Browse files
Files changed (1) hide show
  1. 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 Share</title>
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>P2P File Share (One-to-One)</h2>
14
- <input type="text" id="roomInput" placeholder="Enter Room ID"/>
15
- <button id="joinBtn">Join Room</button>
16
- <button id="sendBtn" disabled>Send File</button>
17
- <input type="file" id="fileInput"/>
18
- <hr>
19
- <pre id="log"></pre>
 
 
 
20
 
21
- <script>
22
- let localConnection;
23
- let dataChannel;
24
- let ws;
25
- let logEl = document.getElementById("log");
 
 
 
 
 
26
 
27
- function log(msg){ logEl.textContent += msg + "\n"; }
 
 
 
 
28
 
29
- document.getElementById("joinBtn").onclick = async () => {
30
- const roomId = document.getElementById("roomInput").value.trim();
31
- if(!roomId){ alert("Enter room ID"); return; }
32
 
33
- ws = new WebSocket(`ws://${location.host}/ws/${roomId}`);
34
- ws.onmessage = async (event) => {
35
  const msg = JSON.parse(event.data);
36
- if(msg.type === "offer"){
37
- localConnection = new RTCPeerConnection();
38
- setupDataChannel();
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
- } else if(msg.type === "answer"){
44
- await localConnection.setRemoteDescription(new RTCSessionDescription(msg));
45
- } else if(msg.type === "candidate"){
46
- try { await localConnection.addIceCandidate(msg.candidate); } catch(e){}
47
  }
48
- };
49
-
50
- localConnection = new RTCPeerConnection();
51
- setupDataChannel();
52
 
53
- localConnection.onicecandidate = event => {
54
- if(event.candidate){
55
- ws.send(JSON.stringify({type:"candidate", candidate: event.candidate}));
56
- }
57
- };
58
 
59
- // Create offer if first peer
60
- localConnection.createOffer().then(offer => {
61
- localConnection.setLocalDescription(offer);
62
- ws.onopen = () => ws.send(JSON.stringify(offer));
63
- });
64
- };
65
 
66
- function setupDataChannel(){
67
- dataChannel = localConnection.createDataChannel("fileChannel");
68
- dataChannel.onopen = () => {
69
- log("Data channel open. You can send files!");
70
- document.getElementById("sendBtn").disabled = false;
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 = URL.createObjectURL(blob);
78
  a.download = "received_file";
79
  a.click();
80
- log("File received!");
81
- }
82
- };
83
- }
84
-
85
- document.getElementById("sendBtn").onclick = () => {
86
- document.getElementById("fileInput").click();
87
- };
88
 
89
- document.getElementById("fileInput").onchange = () => {
90
- const file = document.getElementById("fileInput").files[0];
91
- const chunkSize = 16*1024;
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
- function readSlice(o){
106
- const slice = file.slice(o, o+chunkSize);
107
- reader.readAsArrayBuffer(slice);
 
 
 
108
  }
109
- readSlice(0);
110
- };
111
- </script>
 
 
 
 
 
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>