@@ -14,31 +14,48 @@ class ViewController: UIViewController, ConnectionStateChangeDelegate {
14
14
override func viewDidLoad( ) {
15
15
super. viewDidLoad ( )
16
16
17
+ // remove the debugLogger from the client options if you want to remove the
18
+ // debug logging, or just change the function below
19
+ let debugLogger = { ( text: String ) in debugPrint ( text) }
20
+
17
21
// Only use your secret here for testing or if you're sure that there's
18
22
// no security risk
19
- let pusher = Pusher ( key: " YOUR_APP_KEY " , options: [ " secret " : " YOUR_APP_SECRET " ] )
23
+ let pusher = Pusher ( key: " YOUR_APP_KEY " , options: [ " secret " : " YOUR_APP_SECRET " , " debugLogger " : debugLogger ] )
20
24
21
25
pusher. connection. stateChangeDelegate = self
22
26
pusher. connect ( )
23
27
28
+ pusher . bind ( { ( message: AnyObject ? ) in
29
+ if let message = message as? [ String : AnyObject ] , eventName = message [ " event " ] as? String where eventName == " pusher:error " {
30
+ if let data = message [ " data " ] as? [ String : AnyObject ] , errorMessage = data [ " message " ] as? String {
31
+ print ( " Error message: \( errorMessage) " )
32
+ }
33
+ }
34
+ } )
35
+
24
36
let onMemberAdded = { ( member: PresenceChannelMember ) in
25
37
print ( member)
26
38
}
39
+
27
40
let chan = pusher. subscribe ( " presence-channel " , onMemberAdded: onMemberAdded)
28
41
29
42
chan. bind ( " test-event " , callback: { ( data: AnyObject ? ) -> Void in
30
43
print ( data)
44
+ pusher. subscribe ( " presence-channel " , onMemberAdded: onMemberAdded)
45
+
31
46
if let data = data as? Dictionary < String , AnyObject > {
32
47
if let testVal = data [ " test " ] as? String {
33
48
print ( testVal)
34
49
}
35
50
}
36
51
} )
37
52
53
+ // triggers a client event
38
54
chan. trigger ( " client-test " , data: [ " test " : " some value " ] )
39
55
}
40
56
41
57
func connectionChange( old: ConnectionState, new: ConnectionState) {
58
+ // print the old and new connection states
42
59
print ( " old: \( old) -> new: \( new) " )
43
60
}
44
61
}
0 commit comments