@@ -356,6 +356,35 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
356356 postWait. removeAll ( keepCapacity: true )
357357 }
358358
359+ private func handleOpen( openData: String) {
360+ var err : NSError ?
361+ let mesData = openData. dataUsingEncoding ( NSUTF8StringEncoding, allowLossyConversion: false ) !
362+
363+ if let json = NSJSONSerialization . JSONObjectWithData ( mesData,
364+ options: NSJSONReadingOptions . AllowFragments,
365+ error: & err) as? NSDictionary , let sid = json [ " sid " ] as? String {
366+ self . sid = sid
367+ _connected = true
368+
369+ if !forcePolling && !forceWebsockets {
370+ createWebsocket ( andConnect: true )
371+ }
372+
373+ if let pingInterval = json [ " pingInterval " ] as? Int {
374+ self . pingInterval = pingInterval / 1000
375+ }
376+ } else {
377+ client? . didError ( " Engine failed to handshake " )
378+ return
379+ }
380+
381+ startPingTimer ( )
382+
383+ if !forceWebsockets {
384+ doPoll ( )
385+ }
386+ }
387+
359388 // A poll failed, tell the client about it
360389 private func handlePollingFailed( reason: String) {
361390 _connected = false
@@ -498,34 +527,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
498527 upgradeTransport ( )
499528 }
500529 } else if type == PacketType . OPEN {
501- var err : NSError ?
502-
503530 message. removeAtIndex ( message. startIndex)
504- let mesData = message. dataUsingEncoding ( NSUTF8StringEncoding, allowLossyConversion: false ) !
505-
506- if let json = NSJSONSerialization . JSONObjectWithData ( mesData,
507- options: NSJSONReadingOptions . AllowFragments,
508- error: & err) as? NSDictionary , let sid = json [ " sid " ] as? String {
509- self . sid = sid
510- _connected = true
511-
512- if !forcePolling && !forceWebsockets {
513- createWebsocket ( andConnect: true )
514- }
515-
516- if let pingInterval = json [ " pingInterval " ] as? Int {
517- self . pingInterval = pingInterval / 1000
518- }
519- } else {
520- client? . didError ( " Engine failed to handshake " )
521- return
522- }
523-
524- startPingTimer ( )
525531
526- if !forceWebsockets {
527- doPoll ( )
528- }
532+ handleOpen ( message)
529533 } else if type == PacketType . CLOSE {
530534 if polling {
531535 client? . engineDidClose ( " Disconnect " )
0 commit comments