working poc

This commit is contained in:
Klemek
2025-03-14 10:04:05 +01:00
parent a1c477d834
commit 47e53ac500
3 changed files with 36 additions and 14 deletions
+1
View File
@@ -11,6 +11,7 @@ export default [
...globals.browser, ...globals.browser,
lucide: "readonly", lucide: "readonly",
Peer: "readonly", Peer: "readonly",
streamSaver: "readonly",
}, },
}, },
}, },
+2
View File
@@ -9,6 +9,8 @@
<link rel="stylesheet" href="material-colors.css" /> <link rel="stylesheet" href="material-colors.css" />
<script src="https://unpkg.com/lucide@0"></script> <script src="https://unpkg.com/lucide@0"></script>
<script src="https://unpkg.com/peerjs@1.5.4/dist/peerjs.min.js"></script> <script src="https://unpkg.com/peerjs@1.5.4/dist/peerjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web-streams-polyfill@2.0.2/dist/ponyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/streamsaver@2.0.3/StreamSaver.min.js"></script>
<script type="importmap"> <script type="importmap">
{ {
"imports": { "imports": {
+33 -14
View File
@@ -22,8 +22,9 @@ const app = createApp({
remoteId: null, remoteId: null,
connection: null, // TODO multiple connections connection: null, // TODO multiple connections
data: null, // TODO multiple file data: null, // TODO multiple file
streamEnqueue: null, fileStream: null,
streamClose: null, fileName: null,
fileSize: null,
}; };
}, },
computed: {}, computed: {},
@@ -52,6 +53,19 @@ const app = createApp({
initPeer() { initPeer() {
this.peer = new Peer({ this.peer = new Peer({
debug: 3, debug: 3,
host: "peer.klemek.fr",
port: "443",
secure: true,
config: {
iceServers: [
{ urls: ["stun:stun.l.google.com:19302"] },
{
urls: [`turn:klemek.fr:3478`, `turns:klemek.fr:5349`],
username: "username",
credential: "credential",
},
],
},
}); });
this.peer.on("open", this.peerOpen); this.peer.on("open", this.peerOpen);
this.peer.on("connection", this.peerConnection); this.peer.on("connection", this.peerConnection);
@@ -80,11 +94,11 @@ const app = createApp({
peerClose() { peerClose() {
console.log("peerClose"); console.log("peerClose");
this.peer = null; this.peer = null;
setTimeout(this.initPeer); // setTimeout(this.initPeer);
}, },
peerDisconnected() { peerDisconnected() {
console.log("peerDisconnected"); console.log("peerDisconnected");
this.peer.reconnect(); // this.peer.reconnect();
}, },
peerError(err) { peerError(err) {
console.log("peerError", err); console.log("peerError", err);
@@ -95,21 +109,22 @@ const app = createApp({
console.log(data.type); console.log(data.type);
switch (data.type) { switch (data.type) {
case "start": case "start":
ReadableStream({ this.fileName = data.fileName;
start(ctrl) { this.fileSize = data.fileSize;
this.streamEnqueue = (chunk) => ctrl.enqueue(chunk); this.fileStream = streamSaver
this.streamClose = () => ctrl.close(); .createWriteStream(this.fileName, {
}, size: this.fileSize,
}); })
.getWriter();
break; break;
case "chunk": case "chunk":
if (this.streamEnqueue) { if (this.fileStream) {
this.streamEnqueue(data.bytes); this.fileStream.write(new Uint8Array(data.bytes));
} }
break; break;
case "end": case "end":
if (this.streamClose) { if (this.fileStream) {
this.streamClose(); this.fileStream.close();
} }
break; break;
default: default:
@@ -131,6 +146,8 @@ const app = createApp({
if (!file) { if (!file) {
return; return;
} }
this.fileName = file.name;
this.fileSize = file.size;
this.data = null; this.data = null;
const reader = new FileReader(); const reader = new FileReader();
reader.onload = () => { reader.onload = () => {
@@ -150,6 +167,8 @@ const app = createApp({
start() { start() {
this.connection.send({ this.connection.send({
type: "start", type: "start",
fileName: this.fileName,
fileSize: this.fileSize,
}); });
console.log("start"); console.log("start");
for ( for (