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

Commit 43fe21f

Browse files
author
Colden Prime
authored
Merge pull request #2 from IntrepidPursuits/cprime/reconnect
Surface connection failures through adapter
2 parents 835013d + 5678114 commit 43fe21f

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

Sources/SKRTMAPI/SKRTMAPI.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public protocol RTMWebSocket {
3939
public protocol RTMAdapter: class {
4040
func initialSetup(json: [String: Any], instance: SKRTMAPI)
4141
func notificationForEvent(_ event: Event, type: EventType, instance: SKRTMAPI)
42+
func connectionClosed(with error: Error, instance: SKRTMAPI)
4243
}
4344

4445
public protocol RTMDelegate: class {
@@ -53,8 +54,7 @@ public final class SKRTMAPI: RTMDelegate {
5354
public var adapter: RTMAdapter?
5455
public var token = "xoxp-SLACK_AUTH_TOKEN"
5556
internal var options: RTMOptions
56-
var connected = false
57-
public var connectionRetryInterval: TimeInterval? = nil
57+
public private(set) var connected = false
5858

5959
var ping: Double?
6060
var pong: Double?
@@ -87,7 +87,7 @@ public final class SKRTMAPI: RTMDelegate {
8787
success: {(response) in
8888
self.connectWithResponse(response)
8989
}, failure: { (error) in
90-
self.retryConnection()
90+
self.adapter?.connectionClosed(with: error, instance: self)
9191
}
9292
)
9393
} else {
@@ -98,23 +98,12 @@ public final class SKRTMAPI: RTMDelegate {
9898
success: {(response) in
9999
self.connectWithResponse(response)
100100
}, failure: { (error) in
101-
self.retryConnection()
101+
self.adapter?.connectionClosed(with: error, instance: self)
102102
}
103103
)
104104
}
105105
}
106106

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-
118107
public func disconnect() {
119108
rtm.disconnect()
120109
}
@@ -225,6 +214,8 @@ public final class SKRTMAPI: RTMDelegate {
225214
connected = false
226215
if options.reconnect {
227216
connect()
217+
} else {
218+
adapter?.connectionClosed(with: SlackError.rtmConnectionError, instance: self)
228219
}
229220
}
230221

0 commit comments

Comments
 (0)