updates
This commit is contained in:
@@ -53,11 +53,9 @@ export async function getMediaTracks(media: string, constraints?: MediaStreamCon
|
||||
export async function connect(url: string, media: string) {
|
||||
const { pc, localTracks } = await PeerConnection(media)
|
||||
|
||||
// document.getElementById(videoId).srcObject = new MediaStream(localTracks)
|
||||
let ws = new WebSocket('ws' + url.toString().substring(4))
|
||||
|
||||
const ws = new WebSocket('ws' + url.toString().substring(4))
|
||||
|
||||
ws.addEventListener('open', () => {
|
||||
ws.onopen = function () {
|
||||
pc.addEventListener('icecandidate', (ev) => {
|
||||
if (!ev.candidate) return
|
||||
const msg = { type: 'webrtc/candidate', value: ev.candidate.candidate }
|
||||
@@ -70,16 +68,31 @@ export async function connect(url: string, media: string) {
|
||||
const msg = { type: 'webrtc/offer', value: pc.localDescription?.sdp }
|
||||
ws.send(JSON.stringify(msg))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
ws.addEventListener('message', (ev) => {
|
||||
ws.onmessage = function (ev) {
|
||||
const msg = JSON.parse(ev.data)
|
||||
if (msg.type === 'webrtc/candidate') {
|
||||
pc.addIceCandidate({ candidate: msg.value, sdpMid: '0' })
|
||||
} else if (msg.type === 'webrtc/answer') {
|
||||
pc.setRemoteDescription({ type: 'answer', sdp: msg.value })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ws.onerror = function (ev) {
|
||||
console.log({ ev })
|
||||
}
|
||||
|
||||
ws.onclose = function () {
|
||||
setTimeout(function () {
|
||||
var ws2 = new WebSocket(ws.url)
|
||||
ws2.onopen = ws.onopen
|
||||
ws2.onmessage = ws.onmessage
|
||||
ws2.onclose = ws.onclose
|
||||
ws2.onerror = ws.onerror
|
||||
ws = ws2
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
return new MediaStream(localTracks)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user