Skip to content

Commit

Permalink
Merge pull request #31 from gahag/main
Browse files Browse the repository at this point in the history
Refactor `created` function from room.html
  • Loading branch information
Kevin Lewis authored Oct 27, 2020
2 parents 85db7ab + 59aeff0 commit f25f9e2
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions client/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,45 @@ <h2>Subscribers</h2>
this.resp = await fetch(`/api/session?room=${this.params.id}&code=${this.params.code}`).then(r => r.json())
const { apiKey, sessionId, token, locked, access } = this.resp
if(!access) window.location.replace(`/access-required?id=${this.params.id}&username=${this.params.username}`)
this.session = OT.initSession(apiKey, sessionId)

this.camera = OT.initPublisher('publishers', { mirror: false, name: this.params.username, insertMode: 'append' })
this.roomLocked = locked
this.setLock(this.roomLocked,(this.params.code ? this.params.code : ""))
this.session.connect(token, () => { this.session.publish(this.camera) })
this.session.on('streamCreated', event => {
this.subscribers.push(event.stream)
this.session.subscribe(event.stream, 'subscribers', { insertMode: 'append' })
})
this.session.on('streamDestroyed', event => {
this.subscribers = this.subscribers.filter(sub => sub.id != event.stream.id)
})
this.session.on('signal', event => {
switch (event.type) {
case 'signal:message':
this.messages.push(JSON.parse(event.data))
break
case 'signal:lock':
const lockData = JSON.parse(event.data)
this.setLock(lockData.lock,lockData.code)
break
}
})

this._initSession(apiKey, sessionId, token)
this._subscribeStreamCreated()
this._subscribeStreamDestroyed()
this._subscribeSignal()
},
methods: {
_initSession(apiKey, sessionId, token) {
this.session = OT.initSession(apiKey, sessionId)
this.session.connect(token, () => { this.session.publish(this.camera) })
},
_subscribeStreamCreated() {
this.session.on('streamCreated', event => {
this.subscribers.push(event.stream)
this.session.subscribe(event.stream, 'subscribers', { insertMode: 'append' })
})
},
_subscribeStreamDestroyed() {
this.session.on('streamDestroyed', event => {
this.subscribers = this.subscribers.filter(sub => sub.id != event.stream.id)
})
},
_subscribeSignal() {
this.session.on('signal', event => {
switch (event.type) {
case 'signal:message':
this.messages.push(JSON.parse(event.data))
break
case 'signal:lock':
const lockData = JSON.parse(event.data)
this.setLock(lockData.lock,lockData.code)
break
}
})
},
setLock(locked, code) {
this.inputAccessCode = code
this.roomLocked = locked
Expand Down

0 comments on commit f25f9e2

Please sign in to comment.