Skip to content

Commit 87dc115

Browse files
committed
Add otion of passing in cluster in options
1 parent b2068ad commit 87dc115

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

Source/PusherSwift.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,7 @@ public struct PusherClientOptions {
9090
public let authRequestCustomizer: (NSMutableURLRequest -> NSMutableURLRequest)?
9191

9292
public init(options: [String:Any]?) {
93-
let validKeys = ["encrypted", "attemptToReturnJSONObject", "authEndpoint", "secret", "userDataFetcher", "port", "host", "autoReconnect", "authRequestCustomizer"]
94-
95-
if let options = options {
96-
for (key, _) in options {
97-
if !validKeys.contains(key) {
98-
print("Invalid key in options: \(key)")
99-
}
100-
}
101-
}
102-
93+
let validKeys = ["encrypted", "attemptToReturnJSONObject", "authEndpoint", "secret", "userDataFetcher", "port", "host", "cluster", "autoReconnect", "authRequestCustomizer"]
10394
let defaults: [String:AnyObject?] = [
10495
"encrypted": true,
10596
"attemptToReturnJSONObject": true,
@@ -111,10 +102,29 @@ public struct PusherClientOptions {
111102
"host": "ws.pusherapp.com",
112103
"port": nil
113104
]
105+
106+
var mutableOptions = options
107+
108+
if let options = options {
109+
for (key, _) in options {
110+
if !validKeys.contains(key) {
111+
print("Invalid key in options: \(key)")
112+
}
113+
}
114+
115+
if let cluster = options["cluster"] {
116+
if let host = options["host"] {
117+
print("Both host (\(host)) and cluster (\(cluster)) passed as options - host takes precedence")
118+
} else {
119+
mutableOptions!["host"] = "ws-\(cluster).pusher.com"
120+
}
121+
}
122+
}
123+
124+
var optionsMergedWithDefaults: [String:Any?] = [:]
114125

115-
var optionsMergedWithDefaults: [String:Any] = [:]
116126
for (key, value) in defaults {
117-
if let options = options, optionsValue = options[key] {
127+
if let mutableOptions = mutableOptions, optionsValue = mutableOptions[key] {
118128
optionsMergedWithDefaults[key] = optionsValue
119129
} else {
120130
optionsMergedWithDefaults[key] = value

0 commit comments

Comments
 (0)