Skip to content

Commit

Permalink
Allow the creation of Pusher clients with optional encryption.
Browse files Browse the repository at this point in the history
The previous API is deprecated but remains for compatibility; these methods default to no encryption.

See issues pusher#4 and pusher#8.
  • Loading branch information
lukeredpath committed Oct 31, 2011
1 parent 7e2ca3d commit 788468b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
26 changes: 26 additions & 0 deletions Library/PTPusher.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -123,21 +123,47 @@ extern NSString *const PTPusherErrorUnderlyingEventKey;
a delegate using the delegate property after this method returns, it may not be notified a delegate using the delegate property after this method returns, it may not be notified
of connection events. 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 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 delegate The delegate for this instance
*/ */
+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate; + (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. /** 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 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. 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 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 connectAutomatically If YES, the connection will be connected on initialisation.
*/ */
+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically; + (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 /// @name Managing the connection
///------------------------------------------------------------------------------------/ ///------------------------------------------------------------------------------------/
Expand Down
22 changes: 17 additions & 5 deletions Library/PTPusher.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
#import "PTPusherErrors.h" #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 PTPusherEventReceivedNotification = @"PTPusherEventReceivedNotification";
NSString *const PTPusherEventUserInfoKey = @"PTPusherEventUserInfoKey"; NSString *const PTPusherEventUserInfoKey = @"PTPusherEventUserInfoKey";
NSString *const PTPusherErrorDomain = @"PTPusherErrorDomain"; NSString *const PTPusherErrorDomain = @"PTPusherErrorDomain";
NSString *const PTPusherErrorUnderlyingEventKey = @"PTPusherErrorUnderlyingEventKey"; 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]; return [NSURL URLWithString:URLString];
} }


Expand Down Expand Up @@ -75,15 +77,25 @@ - (id)initWithConnection:(PTPusherConnection *)connection connectAutomatically:(


+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate + (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.delegate = delegate;
[pusher connect]; [pusher connect];
return pusher; return pusher;
} }


+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically + (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]; PTPusher *pusher = [[self alloc] initWithConnection:connection connectAutomatically:connectAutomatically];
[connection release]; [connection release];
return [pusher autorelease]; return [pusher autorelease];
Expand Down
4 changes: 3 additions & 1 deletion Sample/Classes/PusherEventsAppDelegate.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#import "PTPusherEvent.h" #import "PTPusherEvent.h"
#import "NSMutableURLRequest+BasicAuth.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 // this is not included in the source
// you must create this yourself and define PUSHER_API_KEY in it // you must create this yourself and define PUSHER_API_KEY in it
Expand All @@ -27,7 +29,7 @@ @implementation PusherEventsAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application - (void)applicationDidFinishLaunching:(UIApplication *)application
{ {
// establish a new pusher instance // 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 // we want the connection to automatically reconnect if it dies
self.pusher.reconnectAutomatically = YES; self.pusher.reconnectAutomatically = YES;
Expand Down
2 changes: 1 addition & 1 deletion Vendor/ZTWebSocket/ZTWebSocket.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ -(id)initWithURLString:(NSString *)urlString delegate:(id<ZTWebSocketDelegate>)a
self.delegate = aDelegate; self.delegate = aDelegate;
secureSocket = secure; secureSocket = secure;
url = [[NSURL URLWithString:urlString] retain]; 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]]; [NSException raise:ZTWebSocketException format:[NSString stringWithFormat:@"Unsupported protocol %@",url.scheme]];
} }
socket = [[AsyncSocket alloc] initWithDelegate:self]; socket = [[AsyncSocket alloc] initWithDelegate:self];
Expand Down

0 comments on commit 788468b

Please sign in to comment.