Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Commit

Permalink
[Gateway] Fix heartbeat reconnect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Azoy committed Aug 30, 2020
1 parent 6cd52bc commit ca066c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/Sword/Gateway/GatewayHandler.swift
Expand Up @@ -33,7 +33,7 @@ extension Shard {

/// OP: 11
case .heartbeatACK:
self.wasAcked = true
self.heartbeatQueue.sync { self.acksMissed = 0 }

/// OP: 10
case .hello:
Expand Down
20 changes: 10 additions & 10 deletions Sources/Sword/Gateway/Heartbeat.swift
Expand Up @@ -22,19 +22,19 @@ extension Gateway {
return
}

guard self.acksMissed < 3 else {
print("[Sword] Did not receive ACK from server, reconnecting...")
self.reconnect()
return
}

self.acksMissed += 1

self.send(self.heartbeatPayload.encode(), presence: false)

self.heartbeatQueue.asyncAfter(
deadline: .now() + .milliseconds(interval)
) { [unowned self] in
guard self.wasAcked else {
print("[Sword] Did not receive ACK from server, reconnecting...")
self.reconnect()
return
}

self.wasAcked = false

self.send(self.heartbeatPayload.encode(), presence: false)

self.heartbeat(at: interval)
}
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sword/Gateway/Shard.swift
Expand Up @@ -61,8 +61,8 @@ class Shard: Gateway {
/// Parent class
unowned let sword: Sword

/// Whether or not this shard was last acked
var wasAcked = true
/// Number of missed heartbeat ACKs
var acksMissed = 0

// MARK: Initializer

Expand Down Expand Up @@ -259,6 +259,7 @@ class Shard: Gateway {
#endif

self.isConnected = false
self.acksMissed = 0

self.sword.log("Disconnected from gateway... Resuming session")

Expand Down Expand Up @@ -307,6 +308,7 @@ class Shard: Gateway {

self.isConnected = false
self.isReconnecting = false
self.acksMissed = 0

self.sword.log("Stopping gateway connection...")
}
Expand Down

0 comments on commit ca066c5

Please sign in to comment.