Skip to content

Commit f732aa4

Browse files
committed
Merge pull request #31 from Hitlabs/subscription-succeeded-event
Send the "pusher:subscription_succeeded" event
2 parents aaddc0b + 00a8401 commit f732aa4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Source/PusherSwift.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ public class PusherConnection: WebSocketDelegate {
273273
private func handleSubscriptionSucceededEvent(json: PusherEventJSON) {
274274
if let channelName = json["channel"] as? String, chan = self.channels.find(channelName) {
275275
chan.subscribed = true
276+
if let eData = json["data"] as? String {
277+
callGlobalCallbacks("pusher:subscription_succeeded", jsonObject: json)
278+
chan.handleEvent("pusher:subscription_succeeded", eventData: eData)
279+
}
276280
if isPresenceChannel(channelName) {
277281
if let presChan = self.channels.find(channelName) as? PresencePusherChannel {
278282
if let data = json["data"] as? String, dataJSON = getPusherEventJSONFromString(data) {
@@ -384,9 +388,9 @@ public class PusherConnection: WebSocketDelegate {
384388
}
385389

386390
private func callGlobalCallbacks(eventName: String, jsonObject: Dictionary<String,AnyObject>) {
387-
if let channelName = jsonObject["channel"] as? String, eName = jsonObject["event"] as? String, eData = jsonObject["data"] as? String {
391+
if let channelName = jsonObject["channel"] as? String, eData = jsonObject["data"] as? String {
388392
if let globalChannel = self.globalChannel {
389-
globalChannel.handleEvent(channelName, eventName: eName, eventData: eData)
393+
globalChannel.handleEvent(channelName, eventName: eventName, eventData: eData)
390394
}
391395
}
392396
}

Tests/PusherSwiftTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,31 @@ class PusherTopLevelApiSpec: QuickSpec {
424424
let testChannel = pusher.connection.channels.channels["test-channel"]
425425
expect(testChannel?.subscribed).to(beTruthy())
426426
}
427+
428+
it("subscription succeeded event sent to global channel") {
429+
let callback = { (data: AnyObject?) -> Void in
430+
if let eName = data?["event"] where eName == "pusher:subscription_succeeded" {
431+
socket.appendToCallbackCheckString("globalCallbackCalled")
432+
}
433+
}
434+
pusher.bind(callback)
435+
expect(socket.callbackCheckString).to(equal(""))
436+
pusher.subscribe("test-channel")
437+
expect(socket.callbackCheckString).to(equal("globalCallbackCalled"))
438+
}
439+
440+
it("subscription succeeded event sent to private channel") {
441+
let callback = { (data: AnyObject?) -> Void in
442+
if let eName = data?["event"] where eName == "pusher:subscription_succeeded" {
443+
socket.appendToCallbackCheckString("channelCallbackCalled")
444+
}
445+
}
446+
pusher.bind(callback)
447+
expect(socket.callbackCheckString).to(equal(""))
448+
let chan = pusher.subscribe("test-channel")
449+
chan.bind("pusher:subscription_succeeded", callback: callback)
450+
expect(socket.callbackCheckString).to(equal("channelCallbackCalled"))
451+
}
427452

428453
it("sets up the channel correctly") {
429454
let chan = pusher.subscribe("test-channel")

0 commit comments

Comments
 (0)