-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
71 lines (67 loc) · 2.02 KB
/
test.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
const io = require('socket.io-client')
const { RTCPeerConnection } = require('wrtc')
const iceServers = [
{
urls: [
'stun:stun.l.google.com:19302',
'stun:stun1.l.google.com:19302',
'stun:stun2.l.google.com:19302',
'stun:stun3.l.google.com:19302',
'stun:stun4.l.google.com:19302',
'stun:stun.services.mozilla.com',
],
},
{
urls: 'turn:195.201.137.198:3008',
credential: 'webrtc',
username: 'webrtc',
},
]
const WEBRTC_MESSAGE = 'signal'
const channel = io.connect('http://localhost:8080/ws')
channel.on('connect', () => console.log('connected'))
channel.on('disconnect', () => console.log('disconnected'))
channel.on('error', e => console.log('error', e))
channel.on(WEBRTC_MESSAGE, data => onmessage(data))
const send = data => {
console.log('send:', data)
channel.emit(WEBRTC_MESSAGE, data)
}
const configuration = { iceServers }
const offerOptions = { offerToReceiveAudio: false, offerToReceiveVideo: true }
const pc = new RTCPeerConnection(configuration)
pc.onicecandidate = ({ candidate }) => send({ candidate, id: channel.id })
pc.ontrack = event => {
// event.streams[0] will contain the media stream
console.log('onTrack')
}
const start = async () => {
try {
await pc.setLocalDescription(await pc.createOffer(offerOptions))
send({ desc: pc.localDescription, id: channel.id })
} catch (err) {
console.error(err)
}
}
const onmessage = async ({ desc, candidate, id }) => {
try {
console.log('onmessage:', id, desc, candidate)
if (desc) {
if (desc.type === 'offer') {
// this is pure client, it should not handle offer message
console.log('ignore offer')
} else if (desc.type === 'answer') {
// client should handle sdp answer
await pc.setRemoteDescription(desc)
} else {
console.log('invalid sdp')
}
} else if (candidate) {
await pc.addIceCandidate(candidate)
}
} catch (err) {
console.error(err)
}
}
// add some delay before web socket is connected
setTimeout(start, 1000)