Permalink
Browse files

Allow the creation of Pusher clients with optional encryption.

The previous API is deprecated but remains for compatibility; these methods default to no encryption.

See issues #4 and #8.
  • Loading branch information...
1 parent 7e2ca3d commit 788468bc173e1bb57646a3ff8ace551df10a4249 @lukeredpath lukeredpath committed Oct 31, 2011
Showing with 47 additions and 7 deletions.
  1. +26 −0 Library/PTPusher.h
  2. +17 −5 Library/PTPusher.m
  3. +3 −1 Sample/Classes/PusherEventsAppDelegate.m
  4. +1 −1 Vendor/ZTWebSocket/ZTWebSocket.m
View
@@ -123,21 +123,47 @@ extern NSString *const PTPusherErrorUnderlyingEventKey;
a delegate using the delegate property after this method returns, it may not be notified
of connection events.
+ @deprecated
@param key Your application's API key. It can be found in the API Access section of your application within the Pusher user dashboard.
@param delegate The delegate for this instance
*/
+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate;
+/** Returns a new PTPusher instance with a connection configured with the given key.
+
+ Instances created using this method will connect automatically. Specify the delegate here
+ to ensure that it is notified about the connection status during connection. If you assign
+ a delegate using the delegate property after this method returns, it may not be notified
+ of connection events.
+
+ @param key Your application's API key. It can be found in the API Access section of your application within the Pusher user dashboard.
+ @param delegate The delegate for this instance
+ @param isEncrypted If yes, a secure connection over SSL will be established.
+ */
++ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate encrypted:(BOOL)isEncrypted;
+
/** Initialises a new PTPusher instance with a connection configured with the given key.
If you intend to set a delegate for this instance, you are recommended to set connectAutomatically
to NO, set the delegate then manually call connect.
+ @deprecated
@param key Your application's API key. It can be found in the API Access section of your application within the Pusher user dashboard.
@param connectAutomatically If YES, the connection will be connected on initialisation.
*/
+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically;
+/** Initialises a new PTPusher instance with a connection configured with the given key.
+
+ If you intend to set a delegate for this instance, you are recommended to set connectAutomatically
+ to NO, set the delegate then manually call connect.
+
+ @param key Your application's API key. It can be found in the API Access section of your application within the Pusher user dashboard.
+ @param connectAutomatically If YES, the connection will be connected on initialisation.
+ @param isEncrypted If yes, a secure connection over SSL will be established.
+ */
++ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically encrypted:(BOOL)isEncrypted;
+
///------------------------------------------------------------------------------------/
/// @name Managing the connection
///------------------------------------------------------------------------------------/
View
@@ -16,16 +16,18 @@
#import "PTPusherErrors.h"
-NSURL *PTPusherConnectionURL(NSString *host, int port, NSString *key, NSString *clientID);
+NSURL *PTPusherConnectionURL(NSString *host, NSString *key, NSString *clientID, BOOL secure);
NSString *const PTPusherEventReceivedNotification = @"PTPusherEventReceivedNotification";
NSString *const PTPusherEventUserInfoKey = @"PTPusherEventUserInfoKey";
NSString *const PTPusherErrorDomain = @"PTPusherErrorDomain";
NSString *const PTPusherErrorUnderlyingEventKey = @"PTPusherErrorUnderlyingEventKey";
-NSURL *PTPusherConnectionURL(NSString *host, int port, NSString *key, NSString *clientID)
+NSURL *PTPusherConnectionURL(NSString *host, NSString *key, NSString *clientID, BOOL secure)
{
- NSString *URLString = [NSString stringWithFormat:@"ws://%@:%d/app/%@?client=%@", host, port, key, clientID];
+ int port = ((secure == YES) ? 443 : 80);
+ NSString *scheme = ((secure == YES) ? @"wss" : @"ws");
+ NSString *URLString = [NSString stringWithFormat:@"%@://%@:%d/app/%@?client=%@", scheme, host, port, key, clientID];
return [NSURL URLWithString:URLString];
}
@@ -75,15 +77,25 @@ - (id)initWithConnection:(PTPusherConnection *)connection connectAutomatically:(
+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate
{
- PTPusher *pusher = [self pusherWithKey:key connectAutomatically:NO];
+ return [self pusherWithKey:key delegate:delegate encrypted:NO];
+}
+
++ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate encrypted:(BOOL)isEncrypted
+{
+ PTPusher *pusher = [self pusherWithKey:key connectAutomatically:NO encrypted:isEncrypted];
pusher.delegate = delegate;
[pusher connect];
return pusher;
}
+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically
{
- PTPusherConnection *connection = [[PTPusherConnection alloc] initWithURL:PTPusherConnectionURL(@"ws.pusherapp.com", 443, key, @"libpusher") secure:YES];
+ return [self pusherWithKey:key connectAutomatically:connectAutomatically encrypted:NO];
+}
+
++ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically encrypted:(BOOL)isEncrypted
+{
+ PTPusherConnection *connection = [[PTPusherConnection alloc] initWithURL:PTPusherConnectionURL(@"ws.pusherapp.com", key, @"libPusher", isEncrypted) secure:isEncrypted];
PTPusher *pusher = [[self alloc] initWithConnection:connection connectAutomatically:connectAutomatically];
[connection release];
return [pusher autorelease];
@@ -12,6 +12,8 @@
#import "PTPusherEvent.h"
#import "NSMutableURLRequest+BasicAuth.h"
+// change this to switch between secure/non-secure connections
+#define kUSE_ENCRYPTED_CHANNELS YES
// this is not included in the source
// you must create this yourself and define PUSHER_API_KEY in it
@@ -27,7 +29,7 @@ @implementation PusherEventsAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// establish a new pusher instance
- self.pusher = [PTPusher pusherWithKey:PUSHER_API_KEY delegate:self];
+ self.pusher = [PTPusher pusherWithKey:PUSHER_API_KEY delegate:self encrypted:kUSE_ENCRYPTED_CHANNELS];
// we want the connection to automatically reconnect if it dies
self.pusher.reconnectAutomatically = YES;
@@ -33,7 +33,7 @@ -(id)initWithURLString:(NSString *)urlString delegate:(id<ZTWebSocketDelegate>)a
self.delegate = aDelegate;
secureSocket = secure;
url = [[NSURL URLWithString:urlString] retain];
- if (![url.scheme isEqualToString:@"ws"]) {
+ if (![url.scheme hasPrefix:@"ws"]) {
[NSException raise:ZTWebSocketException format:[NSString stringWithFormat:@"Unsupported protocol %@",url.scheme]];
}
socket = [[AsyncSocket alloc] initWithDelegate:self];

0 comments on commit 788468b

Please sign in to comment.