@@ -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
245247static __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