Skip to content

Commit

Permalink
Merge pull request #39 from palavatv/reconnect
Browse files Browse the repository at this point in the history
Reconnect on lost websocket connection
  • Loading branch information
janlelis authored May 24, 2020
2 parents f8a4001 + bb5ed7e commit f9a64e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
defaultRtcUrl: 'ws://localhost:4233',
defaultStunUrl: 'stun:stun.palava.tv',
defaultJoinTimeout: 1500,
reconnectTimeout: 1000,
maximumPeers: 6,
defaultLocale: 'en',
supportedLocales: ['en', 'de'],
Expand Down
27 changes: 25 additions & 2 deletions src/views/Room.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default {
peers: [],
localPeer: null,
infoPage: null,
signalingConnectedBefore: false,
repeatedReconnect: false,
}
},
created() {
Expand Down Expand Up @@ -84,12 +86,12 @@ export default {
rtc.on("signaling_not_reachable", () => {
logger.error("signaling server not reachable")
this.uiState = [RoomError, { error: "connection_error" }]
this.reconnectRtc()
})
rtc.on("signaling_error", (error) => {
logger.error("signaling error", error)
this.uiState = [RoomError, { error: "connection_error" }]
this.reconnectRtc()
})
rtc.on("signaling_shutdown", (seconds) => {
Expand Down Expand Up @@ -124,6 +126,9 @@ export default {
logger.log(`room joined with ${room.getRemotePeers().length} other peers`)
const peers = this.rtc.room.getAllPeers()
this.signalingConnectedBefore = true
this.repeatedReconnect = false
if (peers.length > config.maximumPeers) {
this.uiState = [RoomError, { error: "room_full" }]
this.waiting = false
Expand Down Expand Up @@ -202,6 +207,24 @@ export default {
},
})
},
reconnectRtc() {
if (this.signalingConnectedBefore) {
// TODO: show "Palava server not reachable" or "Network not reachable" overlay
window.addEventListener('online', this.onlineEventListener)
if (navigator.onLine) window.dispatchEvent(new Event('online'))
} else {
this.uiState = [RoomError, { error: "connection_error" }]
}
},
onlineEventListener() {
if (this.repeatedReconnect) {
setTimeout(this.rtc.reconnect, config.reconnectTimeout)
} else {
this.repeatedReconnect = true
this.rtc.reconnect()
}
window.removeEventListener('online', this.onlineEventListener)
},
closeInfoScreen() {
this.infoPage = null
},
Expand Down

0 comments on commit f9a64e6

Please sign in to comment.