Skip to content

Commit 25e5692

Browse files
committed
Cleaned up debugLogger code and added a test for it
1 parent e741d14 commit 25e5692

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

Source/PusherConnection.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ public class PusherConnection {
104104
sendClientEvent(event, data: data, channelName: channelName)
105105
} else {
106106
let dataString = JSONStringify(["event": event, "data": data])
107-
if let debugLogger = self.options.debugLogger {
108-
debugLogger("[PUSHER DEBUG] sendEvent \(dataString)")
109-
}
107+
self.options.debugLogger?("[PUSHER DEBUG] sendEvent \(dataString)")
110108
self.socket.writeString(dataString)
111109
}
112110
}
@@ -122,9 +120,7 @@ public class PusherConnection {
122120
if let cName = channelName {
123121
if isPresenceChannel(cName) || isPrivateChannel(cName) {
124122
let dataString = JSONStringify(["event": event, "data": data, "channel": cName])
125-
if let debugLogger = self.options.debugLogger {
126-
debugLogger("[PUSHER DEBUG] sendClientEvent \(dataString)")
127-
}
123+
self.options.debugLogger?("[PUSHER DEBUG] sendClientEvent \(dataString)")
128124
self.socket.writeString(dataString)
129125
} else {
130126
print("You must be subscribed to a private or presence channel to send client events")
@@ -347,7 +343,7 @@ public class PusherConnection {
347343
print("Unable to parse string from WebSocket: \(string)")
348344
}
349345
} catch let error as NSError {
350-
print(error.localizedDescription)
346+
print("Error: \(error.localizedDescription)")
351347
}
352348
return nil
353349
}
@@ -408,11 +404,14 @@ public class PusherConnection {
408404
- parameter jsonObject: The event-specific data related to the incoming event
409405
*/
410406
private func callGlobalCallbacks(eventName: String, jsonObject: Dictionary<String,AnyObject>) {
411-
if let channelName = jsonObject["channel"] as? String, eData = jsonObject["data"] as? String {
412-
if let globalChannel = self.globalChannel {
407+
if let globalChannel = self.globalChannel {
408+
if let eData = jsonObject["data"] as? String {
409+
let channelName = jsonObject["channel"] as! String?
413410
globalChannel.handleEvent(channelName, eventName: eventName, eventData: eData)
411+
} else if let eData = jsonObject["data"] as? [String: AnyObject] {
412+
globalChannel.handleErrorEvent(eventName, eventData: eData)
414413
}
415-
}
414+
}
416415
}
417416

418417
/**

Source/PusherGlobalChannel.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,32 @@ public class GlobalChannel: PusherChannel {
2323
/**
2424
Calls the appropriate callbacks for the given eventName in the scope of the global channel
2525

26-
- parameter channelName: The name of the channel that the received message was triggered to
26+
- parameter channelName: The name of the channel that the received message was triggered
27+
to, if relevant
2728
- parameter eventName: The name of the received event
2829
- parameter eventdata: The data associated with the received message
2930
*/
30-
internal func handleEvent(channelName: String, eventName: String, eventData: String) {
31+
internal func handleEvent(channelName: String?, eventName: String, eventData: String) {
3132
for (_, callback) in self.globalCallbacks {
32-
callback(["channel": channelName, "event": eventName, "data": eventData])
33+
if let channelName = channelName {
34+
callback(["channel": channelName, "event": eventName, "data": eventData])
35+
} else {
36+
callback(["event": eventName, "data": eventData])
37+
}
38+
}
39+
}
40+
41+
/**
42+
Calls the appropriate callbacks for the given eventName in the scope of the global channel
43+
44+
- parameter channelName: The name of the channel that the received message was triggered
45+
to, if relevant
46+
- parameter eventName: The name of the received event
47+
- parameter eventdata: The data associated with the received message
48+
*/
49+
internal func handleErrorEvent(eventName: String, eventData: [String:AnyObject]) {
50+
for (_, callback) in self.globalCallbacks {
51+
callback(["event": eventName, "data": eventData])
3352
}
3453
}
3554

Source/PusherWebsocketDelegate.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ extension PusherConnection: WebSocketDelegate {
1616
- parameter text: The message received over the websocket
1717
*/
1818
public func websocketDidReceiveMessage(ws: WebSocket, text: String) {
19+
self.options.debugLogger?("[PUSHER DEBUG] websocketDidReceiveMessage \(text)")
1920
if let pusherPayloadObject = getPusherEventJSONFromString(text), eventName = pusherPayloadObject["event"] as? String {
20-
if let debugLogger = self.options.debugLogger {
21-
debugLogger("[PUSHER DEBUG] websocketDidReceiveMessage \(text)")
22-
}
2321
self.handleEvent(eventName, jsonObject: pusherPayloadObject)
2422
} else {
25-
print("Unable to handle incoming Websocket message")
23+
print("Unable to handle incoming Websocket message \(text)")
2624
}
2725
}
2826

Tests/PusherIncomingEventHandlingTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ class HandlingIncomingEventsSpec: QuickSpec {
8383
expect(socket.objectGivenToCallback as? String).to(equal("{\"test\":\"test string\",\"and\":\"another\"}"))
8484
}
8585

86+
it("should handle receiving an error where the data part of the message isn't double encoded") {
87+
pusher = Pusher(key: key)
88+
socket.delegate = pusher.connection
89+
pusher.connection.socket = socket
90+
pusher.bind({ (message: AnyObject?) in
91+
if let message = message as? [String: AnyObject], eventName = message["event"] as? String where eventName == "pusher:error" {
92+
if let data = message["data"] as? [String: AnyObject], errorMessage = data["message"] as? String {
93+
socket.appendToCallbackCheckString(errorMessage)
94+
}
95+
}
96+
})
97+
98+
// pretend that we tried to subscribe to my-channel twice and got this error
99+
// back from Pusher
100+
pusher.connection.handleEvent("pusher:error", jsonObject: ["event": "pusher:error", "data": ["code": "<null>", "message": "Existing subscription to channel my-channel"]])
101+
102+
expect(socket.callbackCheckString).to(equal("Existing subscription to channel my-channel"))
103+
}
104+
86105
it("should pass incoming messages to the debugLogger if one is set") {
87106
let debugLogger = { (text: String) in socket.appendToCallbackCheckString(text) }
88107
pusher = Pusher(key: key, options: ["debugLogger": debugLogger])

0 commit comments

Comments
 (0)