This repository was archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
87 lines (73 loc) · 2.1 KB
/
client.js
File metadata and controls
87 lines (73 loc) · 2.1 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
console.log("type in console: localStorage.debug = 'client'")
require('debug').enable('client')
// keep an eye on: https://github.com/webrtc/adapter
const DataChannel = require('./datachannel')
const io = require('socket.io-client')
const debug = require('debug')('client')
const socket = io.connect(process.argv[2] || getConnectionUrl())
let datachannels = []
this.myPeerId = undefined
function getConnectionUrl() {
if (iAmOnTheServer()) {
return 'https://uxmesh.k8s.zebbra.ch' //for this showcase we don't need a predefined uxmesh-server for the client
} else {
const urlParams = new URLSearchParams(window.location.search)
return urlParams.get('connectionUrl') || 'http://localhost:3001'
}
}
function iAmOnTheServer() {
return !(typeof window != 'undefined' && window.document)
}
socket.on('connect', () => {
debug('Connected to signalling server, Socket ID: %s', socket.id)
const report = () => {
if (datachannels.length > 0) {
datachannels = datachannels.filter(dc => {
if (dc.active === true) {
socket.emit('report', dc.getReport())
return true
} else {
debug('remove dc', dc, 'from datachannel list')
socket.emit('peerDown', dc.peerId)
dc = null
return false
}
})
}
}
setInterval(report, 5000)
})
socket.on('disconnect', () => {
debug('websocket closed, killing peers')
killAndClose()
})
socket.on('error', err => {
debug('websocket error', err)
})
socket.on('peer', data => {
debug('client onPeer', data)
let dc = new DataChannel(data, socket)
datachannels.push(dc)
})
socket.on('signal', data => {
storeMyPeerId(data.sourcePeerId)
for (let dc of datachannels) {
dc.socketSignal(data)
}
})
socket.on('kill', () => {
debug('kill requested, so killing all peers')
killAndClose()
})
function killAndClose() {
for (let dc of datachannels) {
dc.shutdown()
}
datachannels = []
}
function storeMyPeerId(myPeerId) {
if (typeof localStorage !== 'undefined') {
localStorage.setItem('myPeerId', myPeerId)
this.myPeerId = myPeerId
} else this.myPeerId = myPeerId
}