Skip to content

Commit ce4fb27

Browse files
committed
Adding helper constructors and support for Sec-WebSocket-Protocol
Also added some helper constructors
1 parent 934dc2c commit ce4fb27

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

SocketRocket/SRWebSocket.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ extern NSString *const SRWebSocketErrorDomain;
3636

3737
@property (nonatomic, readonly) SRReadyState readyState;
3838
@property (nonatomic, readonly, retain) NSURL *url;
39+
@property (nonatomic, readonly, copy) NSArray *protocols;
3940

41+
// Protocols should be an array of strings that turn into Sec-WebSocket-Protocol
42+
- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
4043
- (id)initWithURLRequest:(NSURLRequest *)request;
4144

45+
// Some helper constructors
46+
- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
47+
- (id)initWithURL:(NSURL *)url;
48+
49+
// SRWebSockets are intended one-time-use only. Open should be called once and only once
4250
- (void)open;
4351

4452
- (void)close;

SocketRocket/SRWebSocket.m

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ - (void)_SR_commonInit;
191191
- (void)_connectToHost:(NSString *)host port:(NSInteger)port;
192192

193193
@property (nonatomic) SRReadyState readyState;
194+
@property (nonatomic, copy) NSArray *protocols;
194195

195196
@end
196197

@@ -241,6 +242,7 @@ @implementation SRWebSocket {
241242
@synthesize delegate = _delegate;
242243
@synthesize url = _url;
243244
@synthesize readyState = _readyState;
245+
@synthesize protocols = _protocols;
244246

245247
static __strong NSData *CRLFCRLF;
246248

@@ -249,14 +251,16 @@ + (void)initialize;
249251
CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
250252
}
251253

252-
- (id)initWithURLRequest:(NSURLRequest *)request;
254+
- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
253255
{
254256
self = [super init];
255257
if (self) {
256258
assert(request.URL);
257259
_url = request.URL;
258260
NSString *scheme = [_url scheme];
259261

262+
self.protocols = protocols;
263+
260264
assert([scheme isEqualToString:@"ws"] || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]);
261265
_urlRequest = request;
262266

@@ -270,6 +274,22 @@ - (id)initWithURLRequest:(NSURLRequest *)request;
270274
return self;
271275
}
272276

277+
- (id)initWithURLRequest:(NSURLRequest *)request;
278+
{
279+
return [self initWithURLRequest:request protocols:nil];
280+
}
281+
282+
- (id)initWithURL:(NSURL *)url;
283+
{
284+
return [self initWithURL:url protocols:nil];
285+
}
286+
287+
- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
288+
{
289+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
290+
return [self initWithURLRequest:request protocols:protocols];
291+
}
292+
273293
- (void)_SR_commonInit;
274294
{
275295
_readyState = SR_CONNECTING;
@@ -418,6 +438,10 @@ - (void)didConnect
418438

419439
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.SR_origin);
420440

441+
if (self.protocols) {
442+
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"), (__bridge CFStringRef)[self.protocols componentsJoinedByString:@", "]);
443+
}
444+
421445
[_urlRequest.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
422446
CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__bridge CFStringRef)obj);
423447
}];
@@ -1413,3 +1437,4 @@ static inline int32_t validate_dispatch_data_partial_string(NSData *data) {
14131437

14141438
#endif
14151439

1440+

0 commit comments

Comments
 (0)