@@ -95,9 +95,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
9595 }
9696
9797 public init( client: SocketEngineClient, sessionDelegate: NSURLSessionDelegate? ) {
98- self . client = client
99- self . session = NSURLSession ( configuration: NSURLSessionConfiguration . ephemeralSessionConfiguration ( ) ,
100- delegate: sessionDelegate, delegateQueue: self . workQueue)
98+ self . client = client
99+ self . session = NSURLSession ( configuration: NSURLSessionConfiguration . ephemeralSessionConfiguration ( ) ,
100+ delegate: sessionDelegate, delegateQueue: self . workQueue)
101101 }
102102
103103 public convenience init( client: SocketEngineClient, opts: NSDictionary? ) {
@@ -122,7 +122,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
122122 self . write ( " " , withType: PacketType . CLOSE, withData: nil )
123123 self . ws? . disconnect ( )
124124 self . stopPolling ( )
125-
125+
126126 if fast || self . polling {
127127 self . client? . engineDidClose ( " Disconnect " )
128128 }
@@ -240,52 +240,49 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
240240 SocketLogger . log ( " Doing polling request " , client: self )
241241
242242 self . session. dataTaskWithRequest ( req) { [ weak self] data, res, err in
243- if self == nil {
244- return
245- } else if err != nil {
246- if self !. polling {
247- self ? . handlePollingFailed ( err. localizedDescription)
248- } else {
249- NSLog ( err. localizedDescription)
243+ if let this = self {
244+ if err != nil {
245+ if this. polling {
246+ this. handlePollingFailed ( err. localizedDescription)
247+ } else {
248+ NSLog ( err. localizedDescription)
249+ }
250+ return
250251 }
251252
252- return
253- }
254-
255- SocketLogger . log ( " Got polling response " , client: self !)
256-
257- if let str = NSString ( data: data, encoding: NSUTF8StringEncoding) as? String {
258- dispatch_async ( self !. parseQueue) { [ weak self] in
259- self ? . parsePollingMessage ( str)
253+ SocketLogger . log ( " Got polling response " , client: this)
254+
255+ if let str = NSString ( data: data, encoding: NSUTF8StringEncoding) as? String {
256+ dispatch_async ( this. parseQueue) { [ weak this] in
257+ this? . parsePollingMessage ( str)
258+ }
259+ }
260+
261+ this. waitingForPoll = false
262+
263+ if this. fastUpgrade {
264+ this. doFastUpgrade ( )
265+ return
266+ } else if !this. closed && this. polling {
267+ this. doPoll ( )
260268 }
261- }
262-
263- self ? . waitingForPoll = false
264-
265- if self !. fastUpgrade {
266- self ? . doFastUpgrade ( )
267- return
268- } else if !self !. closed && self !. polling {
269- self ? . doPoll ( )
270269 } } . resume ( )
271270 }
272271
273272 private func flushProbeWait( ) {
274273 SocketLogger . log ( " Flushing probe wait " , client: self )
275274
276275 dispatch_async ( self . emitQueue) { [ weak self] in
277- if self == nil {
278- return
279- }
280-
281- for waiter in self !. probeWait {
282- self ? . write ( waiter. msg, withType: waiter. type, withData: waiter. data)
283- }
284-
285- self ? . probeWait. removeAll ( keepCapacity: false )
286-
287- if self ? . postWait. count != 0 {
288- self ? . flushWaitingForPostToWebSocket ( )
276+ if let this = self {
277+ for waiter in this. probeWait {
278+ this. write ( waiter. msg, withType: waiter. type, withData: waiter. data)
279+ }
280+
281+ this. probeWait. removeAll ( keepCapacity: false )
282+
283+ if this. postWait. count != 0 {
284+ this. flushWaitingForPostToWebSocket ( )
285+ }
289286 }
290287 }
291288 }
@@ -329,21 +326,22 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
329326 SocketLogger . log ( " POSTing: \( postStr) " , client: self )
330327
331328 self . session. dataTaskWithRequest ( req) { [ weak self] data, res, err in
332- if self == nil {
333- return
334- } else if err != nil && self !. polling {
335- self ? . handlePollingFailed ( err. localizedDescription)
336- return
337- } else if err != nil {
338- NSLog ( err. localizedDescription)
339- return
340- }
341-
342- self ? . waitingForPost = false
343- dispatch_async ( self !. emitQueue) { [ weak self] in
344- if !self !. fastUpgrade {
345- self ? . flushWaitingForPost ( )
346- self ? . doPoll ( )
329+ if let this = self {
330+ if err != nil && this. polling {
331+ this. handlePollingFailed ( err. localizedDescription)
332+ return
333+ } else if err != nil {
334+ NSLog ( err. localizedDescription)
335+ return
336+ }
337+
338+ this. waitingForPost = false
339+
340+ dispatch_async ( this. emitQueue) { [ weak this] in
341+ if !( this? . fastUpgrade ?? true ) {
342+ this? . flushWaitingForPost ( )
343+ this? . doPoll ( )
344+ }
347345 }
348346 } } . resume ( )
349347 }
@@ -468,12 +466,10 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
468466 }
469467
470468 private func parseEngineData( data: NSData) {
471- if self . client == nil {
472- return
473- }
474-
475- dispatch_async ( self . client!. handleQueue) { [ weak self] in
476- self ? . client? . parseBinaryData ( data. subdataWithRange ( NSMakeRange ( 1 , data. length - 1 ) ) )
469+ if let client = self . client {
470+ dispatch_async ( client. handleQueue) { [ weak self] in
471+ self ? . client? . parseBinaryData ( data. subdataWithRange ( NSMakeRange ( 1 , data. length - 1 ) ) )
472+ }
477473 }
478474 }
479475
@@ -490,12 +486,10 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
490486 // Remove message type
491487 message. removeAtIndex ( message. startIndex)
492488
493- if self . client == nil {
494- return
495- }
496-
497- dispatch_async ( self . client!. handleQueue) { [ weak self] in
498- self ? . client? . parseSocketMessage ( message)
489+ if let client = self . client {
490+ dispatch_async ( client. handleQueue) { [ weak self] in
491+ self ? . client? . parseSocketMessage ( message)
492+ }
499493 }
500494 } else if type == PacketType . NOOP {
501495 self . doPoll ( )
@@ -555,11 +549,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
555549 end: advance ( message. startIndex, 2 ) ) )
556550
557551 if let data = NSData ( base64EncodedString: message,
558- options: NSDataBase64DecodingOptions . IgnoreUnknownCharacters)
559- where self . client != nil {
560- // println("sending \(data)")
561-
562- dispatch_async ( self . client!. handleQueue) { [ weak self] in
552+ options: NSDataBase64DecodingOptions . IgnoreUnknownCharacters) , client = self . client {
553+ dispatch_async ( client. handleQueue) { [ weak self] in
563554 self ? . client? . parseBinaryData ( data)
564555 }
565556 }
@@ -635,13 +626,11 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
635626
636627 self . pingTimer? . invalidate ( )
637628 dispatch_async ( dispatch_get_main_queue ( ) ) { [ weak self] in
638- if self == nil {
639- return
629+ if let this = self {
630+ this. pingTimer = NSTimer . scheduledTimerWithTimeInterval ( NSTimeInterval ( this. pingInterval!) ,
631+ target: this,
632+ selector: Selector ( " sendPing " ) , userInfo: nil , repeats: true )
640633 }
641-
642- self ? . pingTimer = NSTimer . scheduledTimerWithTimeInterval ( NSTimeInterval ( self !. pingInterval!) ,
643- target: self !,
644- selector: Selector ( " sendPing " ) , userInfo: nil , repeats: true )
645634 }
646635 }
647636
@@ -665,16 +654,14 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
665654 */
666655 public func write( msg: String, withType type: PacketType, withData data: ContiguousArray < NSData > ? ) {
667656 dispatch_async ( self . emitQueue) { [ weak self] in
668- if self == nil || !self !. connected {
669- return
670- }
671-
672- if self !. websocket {
673- SocketLogger . log ( " Writing ws: \( msg) : \( data) " , client: self !)
674- self ? . sendWebSocketMessage ( msg, withType: type, datas: data)
675- } else {
676- SocketLogger . log ( " Writing poll: \( msg) : \( data) " , client: self !)
677- self ? . sendPollMessage ( msg, withType: type, datas: data)
657+ if let this = self where this. connected {
658+ if this. websocket {
659+ SocketLogger . log ( " Writing ws: \( msg) : \( data) " , client: this)
660+ this. sendWebSocketMessage ( msg, withType: type, datas: data)
661+ } else {
662+ SocketLogger . log ( " Writing poll: \( msg) : \( data) " , client: this)
663+ this. sendPollMessage ( msg, withType: type, datas: data)
664+ }
678665 }
679666 }
680667 }
0 commit comments