-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
94 lines (80 loc) · 2.2 KB
/
app.js
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
const showMyFaceButton = document.querySelector('#start')
const showFriendsFaceButton = document.querySelector('#receiver')
const showMyFaceVideo = document.querySelector('#emitter-video')
const showFriendsFaceVideo = document.querySelector('#receiver-video')
const offerTextarea = document.querySelector('#offer')
const yourId = Math.floor(Math.random() * 1000000000)
let p = null
import { database } from './firebase-init.js'
function readMessage(data) {
const msg = JSON.parse(data.val().message)
const sender = data.val().sender
if (sender != yourId) {
console.log(msg)
if (p === null) {
p = new SimplePeer({
initiator: false,
config: {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' },
{ urls: 'stun:stun.services.mozilla.com' },
{
urls: 'turn:numb.viagenie.ca',
credential: '****',
username: '****',
},
],
},
})
bindEvents(p)
}
p.signal(msg)
}
}
function bindEvents(p) {
p.on('error', err => {
console.log(err)
})
p.on('signal', data => {
const msg = database.push({
sender: yourId,
message: JSON.stringify(data),
})
msg.remove()
})
p.on('stream', stream => {
console.log('streaming')
console.log(stream)
showFriendsFaceVideo.srcObject = stream
})
}
database.on('child_added', readMessage)
showMyFaceButton.addEventListener('click', async e => {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
})
p = new SimplePeer({
initiator: true,
stream,
config: {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' },
{ urls: 'stun:stun.services.mozilla.com' },
{
urls: 'turn:numb.viagenie.ca',
credential: 'hiragana',
username: '[email protected]',
},
],
},
})
bindEvents(p)
showMyFaceVideo.srcObject = stream
} catch (e) {
console.log(e.message)
}
})