-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathchat.html
More file actions
115 lines (106 loc) · 2.93 KB
/
chat.html
File metadata and controls
115 lines (106 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Nostr Voice Chat</title></head>
<body>
<h2>Nostr Voice Chat Proof of Concept</h2>
<p>By hitting "send" you agree to broadcasting the recorded audio to the world
without any encryption. For more information read
<a href="https://github.com/Giszmo/Nostr-Voice-Chat">here</a>.</p>
<p>
<button type="button" id="record">Record</button>
<button type="button" id="stopRecord" disabled>Send</button>
<button type="button" id="dismiss" disabled>Dismiss</button>
</p>
<p>
<audio id=recordedAudio></audio>
</p>
<script type="module">
import { generatePrivateKey, getPublicKey, relayConnect, relayPool } from './nostr.js'
window.nostr = { generatePrivateKey, getPublicKey, relayConnect, relayPool }
</script>
<script class="containerScript">
window.addEventListener("load", async () => {
window.privKey = window.nostr.generatePrivateKey()
window.pubKey = window.nostr.getPublicKey(window.privKey)
window.pool = window.nostr.relayPool()
window.pool.setPrivateKey(window.privKey)
window.pool.addRelay('wss://relay.nostr.info', {read: true, write: true})
window.pool.sub({cb: async (event, relay) => {
let blob = await b64toBlob(event.content)
play(blob)
}, filter: {kinds: [21212]}})
})
navigator
.mediaDevices
.getUserMedia({audio:true})
.then(stream => {
handlerFunction(stream)
})
function handlerFunction(stream) {
rec = new MediaRecorder(stream)
rec.ondataavailable = e => {
audioChunks.push(e.data)
if (rec.state == "inactive") {
if (audioChunks[0] === -1) {
console.log("dismissed")
return
}
let blob = new Blob(audioChunks,{type:'audio/mp3'})
play(blob)
sendData(blob)
}
}
}
function play(blob) {
if (rec.state == "inactive") {
recordedAudio.src = URL.createObjectURL(blob)
recordedAudio.controls=true
recordedAudio.autoplay=true
}
}
function b64toBlob(base64) {
return fetch(base64).then(res => res.blob())
}
function blobToB64(blob) {
return new Promise((resolve, _) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.readAsDataURL(blob)
});
}
async function sendData(blob) {
let event = {
pubkey: window.pubKey,
created_at: Math.round(Date.now() / 1000),
kind: 21212,
tags: [],
content: await blobToB64(blob)
}
window.pool.publish(event)
}
record.onclick = e => {
record.style.backgroundColor = "blue"
record.disabled = true;
stopRecord.disabled=false;
dismiss.disabled=false
audioChunks = [];
rec.start();
}
stopRecord.onclick = e => {
record.style.backgroundColor = "green"
record.disabled = false;
stopRecord.disabled=true;
dismiss.disabled=true
rec.stop();
}
dismiss.onclick = e => {
audioChunks.push(-1) // HACK
record.style.backgroundColor = "red"
record.disabled = false;
stopRecord.disabled=true;
dismiss.disabled=true
rec.stop();
}
</script>
</body>
</html>