Skip to content

Commit

Permalink
fix(rejoin): did not rejoin when switching wifi networks
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed May 24, 2018
1 parent 5a6b4fc commit 0ded085
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/WebChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
this.signaling.close()
this.topology.leave()
this.clean()
this.setState(WebChannelState.LEFT)
}
}

Expand Down Expand Up @@ -238,6 +237,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
}

init(key: string, id: number = generateId()) {
log.webgroup('INIT')
this.id = id
this.myId = generateId()
this.members = [this.myId]
Expand All @@ -251,6 +251,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
}

private clean() {
log.webgroup('CLEAN')
if (this.rejoinTimer) {
global.clearTimeout(this.rejoinTimer)
this.rejoinTimer = undefined
Expand All @@ -260,6 +261,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
this.members = []
this.id = 0
this.myId = 0
this.setState(WebChannelState.LEFT)
}

private setState(state: WebChannelState): void {
Expand Down Expand Up @@ -294,7 +296,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
case TopologyState.LEFT:
switch (this.signaling.state) {
case SignalingState.CLOSED:
this.rejoinOrLeave()
this.rejoin()
break
case SignalingState.CONNECTING:
this.setState(WebChannelState.JOINING)
Expand Down Expand Up @@ -323,9 +325,9 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
switch (state) {
case SignalingState.CLOSED:
if (this.topology.state === TopologyState.LEFT) {
this.rejoinOrLeave()
} else if (this.topology.state === TopologyState.JOINED && this.rejoinEnabled) {
this.signaling.connect(this.key)
this.rejoin()
} else {
this.reconnectToSignaling()
}
break
case SignalingState.OPEN:
Expand Down Expand Up @@ -376,22 +378,38 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
this.signaling.connect(key)
}

private rejoinOrLeave() {
this.clean()
private rejoin() {
log.webgroup('rejoinOrLeave')
if (this.rejoinEnabled) {
this.init(this.key)
this.clean()
// this.init(this.key)
log.webgroup('rejoinOrLeave: set rejoinTimer ')
this.rejoinTimer = global.setTimeout(() => {
if (
this.state === WebChannelState.LEFT &&
isVisible() &&
isOnline() &&
this.rejoinEnabled
) {
log.webgroup('rejoinOrLeave: startJoin ')
this.startJoin()
} else {
log.webgroup('rejoinOrLeave: NOT startJoin ')
}
this.rejoinTimer = undefined
}, REJOIN_TIMEOUT)
}
}

private reconnectToSignaling() {
log.webgroup('reconnect: ')
if (this.rejoinEnabled && !this.rejoinTimer) {
this.rejoinTimer = global.setTimeout(() => {
if (isVisible() && isOnline() && this.rejoinEnabled) {
this.signaling.connect(this.key)
}
this.rejoinTimer = undefined
}, REJOIN_TIMEOUT)
} else {
this.setState(WebChannelState.LEFT)
}
}

Expand Down

0 comments on commit 0ded085

Please sign in to comment.