Skip to content
This repository has been archived by the owner on Jan 6, 2019. It is now read-only.

Commit

Permalink
Use NSRunLoopCommonMondes as default run loop mode for ZTWebSocket, b…
Browse files Browse the repository at this point in the history
…ut allow user to supply their own run loop modes
  • Loading branch information
Esad Hajdarevic committed Feb 19, 2010
1 parent 000b556 commit 5284da5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
5 changes: 1 addition & 4 deletions samples/Chat/Classes/ChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ -(void)write:(NSString*)text {

-(void)viewDidLoad {
webSocket = [[ZTWebSocket alloc] initWithURLString:@"ws://localhost:10000/" delegate:self];
[webSocket open];
NSArray *runLoopModes = [NSArray arrayWithObjects:NSRunLoopCommonModes, nil];
[webSocket setRunLoopModes:runLoopModes];

[webSocket open];
[textField becomeFirstResponder];
}

Expand Down
9 changes: 4 additions & 5 deletions src/ZTWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@implementation ZTWebSocket

@synthesize delegate, url, origin, connected;
@synthesize delegate, url, origin, connected, runLoopModes;

#pragma mark Initializers

Expand All @@ -37,6 +37,7 @@ -(id)initWithURLString:(NSString *)urlString delegate:(id<ZTWebSocketDelegate>)a
[NSException raise:ZTWebSocketException format:[NSString stringWithFormat:@"Unsupported protocol %@",url.scheme]];
}
socket = [[AsyncSocket alloc] initWithDelegate:self];
self.runLoopModes = [NSArray arrayWithObjects:NSRunLoopCommonModes, nil];
}
return self;
}
Expand Down Expand Up @@ -81,17 +82,14 @@ -(void)_readNextMessage {

#pragma mark Public interface

- (BOOL)setRunLoopModes:(NSArray *)runLoopModes {
return [socket setRunLoopModes:runLoopModes];
}

-(void)close {
[socket disconnectAfterReadingAndWriting];
}

-(void)open {
if (!connected) {
[socket connectToHost:url.host onPort:[url.port intValue] withTimeout:5 error:nil];
if (runLoopModes) [socket setRunLoopModes:runLoopModes];
}
}

Expand Down Expand Up @@ -167,6 +165,7 @@ -(void)dealloc {
socket.delegate = nil;
[socket disconnect];
[socket release];
[runLoopModes release];
[url release];
[super dealloc];
}
Expand Down
5 changes: 3 additions & 2 deletions src/Zimt/ZTWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
AsyncSocket* socket;
BOOL connected;
NSString* origin;

NSArray* runLoopModes;
}

@property(nonatomic,assign) id<ZTWebSocketDelegate> delegate;
@property(nonatomic,readonly) NSURL* url;
@property(nonatomic,retain) NSString* origin;
@property(nonatomic,readonly) BOOL connected;
@property(nonatomic,retain) NSArray* runLoopModes;

+ (id)webSocketWithURLString:(NSString*)urlString delegate:(id<ZTWebSocketDelegate>)delegate;
- (id)initWithURLString:(NSString*)urlString delegate:(id<ZTWebSocketDelegate>)delegate;

- (BOOL)setRunLoopModes:(NSArray *)runLoopModes;

- (void)open;
- (void)close;
- (void)send:(NSString*)message;
Expand Down

0 comments on commit 5284da5

Please sign in to comment.