-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
119 lines (98 loc) · 3.72 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// get the users consent to use their camera
navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" }, audio: false})
// navigator.mediaDevices.getUserMedia({ video: true, audio: false})
.then((stream) => {
// importing libaries ("simple-peer" for peer-to-peer connections, "signalhub" for the exchange of signaling informaition)
document.getElementById("play").style.display = "none";
const Peer = require('simple-peer')
const peer = new Peer ({
initiator: location.hash == '#init',
trickle: false,
stream: stream
})
const signalhub = require('signalhub')
const hub = signalhub('video-livestream', [
'https://192.168.2.102:8080'
])
// an error has occurred
peer.on('error', (err) => document.write(err))
// when a signal is created -> if initiator = true at start, initiator = false after receiving the offer
peer.on('signal', (data) => {
if(location.hash == '#init') {
hub.broadcast('forward', JSON.stringify(data))
} else {
hub.broadcast('backward', JSON.stringify(data))
}
})
// listens to broadcasted changes from the peer.on('signal) -> uses offer to create awnser
hub.subscribe('forward').on('data', (data) => {
let otherID = JSON.parse(data)
if(location.hash != '#init') {
console.log('forward', otherID)
peer.signal(otherID)
}
})
hub.subscribe('backward').on('data', (data) => {
let otherID = JSON.parse(data)
if(location.hash == '#init') {
console.log('backward', otherID)
peer.signal(otherID)
}
})
peer.on('connected', () => console.log('IT WORKS :)'))
// video stream -> create video and play it
// const video = document.createElement('video')
// document.body.appendChild(video)
// video.srcObject = stream
// video.play()
})
.catch((stream) => {
// document.write(err)
console.log("Hallo es geht.")
const Peer = require('simple-peer')
const peer = new Peer ({
initiator: location.hash == '#init',
trickle: false,
})
const signalhub = require('signalhub')
const hub = signalhub('video-livestream', [
'https://192.168.2.102:8080'
])
peer.on('error', (err) => document.write(err))
// when a signal is created -> if initiator = true at start, initiator = false after receiving the offer
peer.on('signal', (data) => {
if(location.hash == '#init') {
hub.broadcast('forward', JSON.stringify(data))
} else {
hub.broadcast('backward', JSON.stringify(data))
}
})
// listens to broadcasted changes from the peer.on('signal) -> uses offer to create awnser
hub.subscribe('forward').on('data', (data) => {
let otherID = JSON.parse(data)
if(location.hash != '#init') {
console.log('forward', otherID)
peer.signal(otherID)
}
})
hub.subscribe('backward').on('data', (data) => {
let otherID = JSON.parse(data)
if(location.hash == '#init') {
console.log('backward', otherID)
peer.signal(otherID)
}
})
peer.on('connected', () => console.log('IT WORKS :)'))
// video stream -> create video and play it
peer.on('stream', (stream) => {
const video = document.createElement('video')
document.body.appendChild(video)
video.srcObject = stream
video.play()
// document.querySelector('button').addEventListener('click', function() {
// video.play()
// console.log("play");
// document.getElementById("play").style.display = "none";
// });
})
})