Skip to content
This repository was archived by the owner on Jan 28, 2019. It is now read-only.

Commit 835013d

Browse files
author
Colden Prime
authored
Merge pull request #1 from IntrepidPursuits/reconnect
Add ability to automatically reconnect after a connection failure
2 parents 0b66f47 + 73cb82b commit 835013d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Sources/SKRTMAPI/SKRTMAPI.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class SKRTMAPI: RTMDelegate {
5454
public var token = "xoxp-SLACK_AUTH_TOKEN"
5555
internal var options: RTMOptions
5656
var connected = false
57+
public var connectionRetryInterval: TimeInterval? = nil
5758

5859
var ping: Double?
5960
var pong: Double?
@@ -86,7 +87,7 @@ public final class SKRTMAPI: RTMDelegate {
8687
success: {(response) in
8788
self.connectWithResponse(response)
8889
}, failure: { (error) in
89-
print(error)
90+
self.retryConnection()
9091
}
9192
)
9293
} else {
@@ -97,12 +98,23 @@ public final class SKRTMAPI: RTMDelegate {
9798
success: {(response) in
9899
self.connectWithResponse(response)
99100
}, failure: { (error) in
100-
print(error)
101+
self.retryConnection()
101102
}
102103
)
103104
}
104105
}
105106

107+
private func retryConnection() {
108+
guard let connectionRetryInterval = self.connectionRetryInterval else {
109+
return
110+
}
111+
let retryInterval = Double(UInt64(connectionRetryInterval * Double(UInt64.nanosecondsPerSecond))) / Double(UInt64.nanosecondsPerSecond)
112+
let delay = DispatchTime.now() + retryInterval
113+
DispatchQueue.main.asyncAfter(deadline: delay) {
114+
self.connect()
115+
}
116+
}
117+
106118
public func disconnect() {
107119
rtm.disconnect()
108120
}

0 commit comments

Comments
 (0)