Skip to content

Commit da875e2

Browse files
committed
Add ability to manually pass in auth values for subscribe requests
1 parent db7b5c2 commit da875e2

File tree

6 files changed

+150
-75
lines changed

6 files changed

+150
-75
lines changed

Source/ObjectiveC.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@ public extension Pusher {
1111
return self.subscribe(channelName, onMemberAdded: nil, onMemberRemoved: nil)
1212
}
1313

14+
@objc public func subscribe(
15+
channelName: String,
16+
onMemberAdded: ((PusherPresenceChannelMember) -> ())? = nil,
17+
onMemberRemoved: ((PusherPresenceChannelMember) -> ())? = nil) -> PusherChannel {
18+
return self.subscribe(channelName, auth: nil, onMemberAdded: onMemberAdded, onMemberRemoved: onMemberRemoved)
19+
}
20+
1421
@objc public func subscribeToPresenceChannel(channelName: String) -> PusherPresenceChannel {
15-
return self.subscribeToPresenceChannel(channelName: channelName, onMemberAdded: nil, onMemberRemoved: nil)
22+
return self.subscribeToPresenceChannel(channelName: channelName, auth: nil, onMemberAdded: nil, onMemberRemoved: nil)
23+
}
24+
25+
@objc public func subscribeToPresenceChannel(
26+
channelName: String,
27+
onMemberAdded: ((PusherPresenceChannelMember) -> ())? = nil,
28+
onMemberRemoved: ((PusherPresenceChannelMember) -> ())? = nil) -> PusherPresenceChannel {
29+
return self.subscribeToPresenceChannel(channelName: channelName, auth: nil, onMemberAdded: onMemberAdded, onMemberRemoved: onMemberRemoved)
1630
}
1731

1832
@objc public convenience init(withAppKey key: String, options: PusherClientOptions) {

Source/PusherChannel.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public enum PusherChannelType {
1010
case `private`
1111
case presence
1212
case normal
13-
13+
1414
public init(name: String) {
1515
self = type(of: self).type(forName: name)
1616
}
17-
17+
1818
public static func type(forName name: String) -> PusherChannelType {
1919
if (name.components(separatedBy: "-")[0] == "presence") {
2020
return .presence
@@ -24,7 +24,7 @@ public enum PusherChannelType {
2424
return .normal
2525
}
2626
}
27-
27+
2828
public static func isPresenceChannel(name: String) -> Bool {
2929
return PusherChannelType(name: name) == .presence
3030
}
@@ -37,18 +37,22 @@ open class PusherChannel: NSObject {
3737
open weak var connection: PusherConnection?
3838
open var unsentEvents = [PusherEvent]()
3939
open let type: PusherChannelType
40+
public var auth: PusherAuth?
4041

4142
/**
4243
Initializes a new PusherChannel with a given name and conenction
4344

4445
- parameter name: The name of the channel
4546
- parameter connection: The connection that this channel is relevant to
47+
- parameter auth: A PusherAuth value if subscription is being made to an
48+
authenticated channel without using the default auth methods
4649

4750
- returns: A new PusherChannel instance
4851
*/
49-
public init(name: String, connection: PusherConnection) {
52+
public init(name: String, connection: PusherConnection, auth: PusherAuth? = nil) {
5053
self.name = name
5154
self.connection = connection
55+
self.auth = auth
5256
self.type = PusherChannelType(name: name)
5357
}
5458

Source/PusherChannels.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ open class PusherChannels: NSObject {
1515

1616
- parameter name: The name of the channel to create
1717
- parameter connection: The connection associated with the channel being created
18+
- parameter auth: A PusherAuth value if subscription is being made to an
19+
authenticated channel without using the default auth methods
1820
- parameter onMemberAdded: A function that will be called with information about the
1921
member who has just joined the presence channel
2022
- parameter onMemberRemoved: A function that will be called with information about the
@@ -25,6 +27,7 @@ open class PusherChannels: NSObject {
2527
internal func add(
2628
name: String,
2729
connection: PusherConnection,
30+
auth: PusherAuth? = nil,
2831
onMemberAdded: ((PusherPresenceChannelMember) -> ())? = nil,
2932
onMemberRemoved: ((PusherPresenceChannelMember) -> ())? = nil) -> PusherChannel {
3033
if let channel = self.channels[name] {
@@ -35,11 +38,12 @@ open class PusherChannels: NSObject {
3538
newChannel = PusherPresenceChannel(
3639
name: name,
3740
connection: connection,
41+
auth: auth,
3842
onMemberAdded: onMemberAdded,
3943
onMemberRemoved: onMemberRemoved
4044
)
4145
} else {
42-
newChannel = PusherChannel(name: name, connection: connection)
46+
newChannel = PusherChannel(name: name, connection: connection, auth: auth)
4347
}
4448
self.channels[name] = newChannel
4549
return newChannel
@@ -52,6 +56,8 @@ open class PusherChannels: NSObject {
5256

5357
- parameter channelName: The name of the channel to create
5458
- parameter connection: The connection associated with the channel being created
59+
- parameter auth: A PusherAuth value if subscription is being made to an
60+
authenticated channel without using the default auth methods
5561
- parameter onMemberAdded: A function that will be called with information about the
5662
member who has just joined the presence channel
5763
- parameter onMemberRemoved: A function that will be called with information about the
@@ -62,6 +68,7 @@ open class PusherChannels: NSObject {
6268
internal func addPresence(
6369
channelName: String,
6470
connection: PusherConnection,
71+
auth: PusherAuth? = nil,
6572
onMemberAdded: ((PusherPresenceChannelMember) -> ())? = nil,
6673
onMemberRemoved: ((PusherPresenceChannelMember) -> ())? = nil) -> PusherPresenceChannel {
6774
if let channel = self.channels[channelName] as? PusherPresenceChannel {
@@ -70,6 +77,7 @@ open class PusherChannels: NSObject {
7077
let newChannel = PusherPresenceChannel(
7178
name: channelName,
7279
connection: connection,
80+
auth: auth,
7381
onMemberAdded: onMemberAdded,
7482
onMemberRemoved: onMemberRemoved
7583
)

0 commit comments

Comments
 (0)