浏览代码

fix(timeouts): timeout open attempts based on NSURLRequest params

@anulman 9 年之前
父节点
当前提交
0ea20993d3
共有 1 个文件被更改,包括 10 次插入1 次删除
  1. 10 1
      SocketRocket/SRWebSocket.m

+ 10 - 1
SocketRocket/SRWebSocket.m

@@ -389,7 +389,16 @@ static __strong NSData *CRLFCRLF;
     NSAssert(_readyState == SR_CONNECTING, @"Cannot call -(void)open on SRWebSocket more than once");
 
     _selfRetain = self;
-    
+
+    if (_urlRequest.timeoutInterval > 0)
+    {
+        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, _urlRequest.timeoutInterval * NSEC_PER_SEC);
+        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
+            if (self.readyState == SR_CONNECTING)
+                [self _failWithError:[NSError errorWithDomain:@"com.squareup.SocketRocket" code:60 userInfo:@{NSLocalizedDescriptionKey: @"Timeout Connecting to Server"}]];
+        });
+    }
+
     [self openConnection];
 }