Compare commits

...
8 Commits
Author SHA1 Message Date
Erik 079063e396 bump version 2015-06-05 06:59:20 -04:00
Erik 33d5fd3f10 fix base64 data 2015-06-05 06:57:59 -04:00
Erik 3be5621263 remove comment 2015-05-31 11:09:25 -04:00
Erik 81ab71f519 extract methods 2015-05-31 11:07:48 -04:00
Erik bfeec57c04 don't need type 2015-05-29 10:30:59 -04:00
Erik 0b09a1357c use didset instead of computed variable 2015-05-29 10:29:26 -04:00
Erik e8dfe589ac redo pingtimeouts 2015-05-29 10:10:24 -04:00
Erik fcebc870c3 fix socket.io/socket.io-client-swift#87 2015-05-27 07:28:06 -04:00
3 changed files with 85 additions and 47 deletions

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Socket.IO-Client-Swift"
s.version = "2.3.2"
s.version = "2.3.3"
s.summary = "Socket.IO-client for iOS and OS X"
s.description = <<-DESC
Socket.IO-client for iOS and OS X.
@@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.author = { "Erik" => "nuclear.ace@gmail.com" }
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.3.2' }
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.3.3' }
s.source_files = "SocketIOClientSwift/**/*.swift"
s.requires_arc = true
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
+82 -44
View File
@@ -40,7 +40,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
private var fastUpgrade = false
private var forcePolling = false
private var forceWebsockets = false
private var pingInterval:Int?
private var pingTimer:NSTimer?
private var pingTimeout = 0 {
didSet {
pongsMissedMax = pingTimeout / (pingInterval ?? 25)
}
}
private var pongsMissed = 0
private var pongsMissedMax = 0
private var postWait = [String]()
private var _polling = true
private var probing = false
@@ -58,7 +66,6 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
weak var client:SocketEngineClient?
var cookies:[NSHTTPCookie]?
var log = false
var pingInterval:Int?
var polling:Bool {
return _polling
}
@@ -213,7 +220,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
if websocket || waitingForPoll || !connected {
return
}
waitingForPoll = true
let req = NSMutableURLRequest(URL: NSURL(string: urlPolling! + "&sid=\(sid)&b64=1")!)
@@ -350,13 +357,46 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
postWait.removeAll(keepCapacity: true)
}
private func handleClose() {
if polling {
client?.engineDidClose("Disconnect")
}
}
private func checkIfMessageIsBase64Binary(var message:String) {
if message.hasPrefix("b4") {
// binary in base64 string
message.removeRange(Range<String.Index>(start: message.startIndex,
end: advance(message.startIndex, 2)))
if let data = NSData(base64EncodedString: message,
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters), client = client {
dispatch_async(client.handleQueue) {[weak self] in
self?.client?.parseBinaryData(data)
}
}
}
}
private func handleMessage(message:String) {
if let client = client {
dispatch_async(client.handleQueue) {[weak client] in
client?.parseSocketMessage(message)
}
}
}
private func handleNOOP() {
doPoll()
}
private func handleOpen(openData:String) {
var err:NSError?
let mesData = openData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
if let json = NSJSONSerialization.JSONObjectWithData(mesData,
options: NSJSONReadingOptions.AllowFragments,
error: &err) as? NSDictionary, let sid = json["sid"] as? String {
error: &err) as? NSDictionary, sid = json["sid"] as? String {
self.sid = sid
_connected = true
@@ -364,8 +404,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
createWebsocket(andConnect: true)
}
if let pingInterval = json["pingInterval"] as? Int {
if let pingInterval = json["pingInterval"] as? Int, pingTimeout = json["pingTimeout"] as? Int {
self.pingInterval = pingInterval / 1000
self.pingTimeout = pingTimeout / 1000
}
} else {
client?.didError("Engine failed to handshake")
@@ -379,6 +420,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
}
}
private func handlePong(pongMessage:String) {
pongsMissed = 0
// We should upgrade
if pongMessage == "3probe" {
upgradeTransport()
}
}
// A poll failed, tell the client about it
private func handlePollingFailed(reason:String) {
_connected = false
@@ -460,18 +510,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
} else {
if length == "" || testLength(length, &n) {
SocketLogger.err("Parsing error: \(str)", client: self)
handlePollingFailed("Error parsing XHR message")
return
}
msg = String(strArray[i+1...i+n])
if let lengthInt = length.toInt() {
if lengthInt != count(msg) {
SocketLogger.err("parsing error: \(str)", client: self)
return
}
if let lengthInt = length.toInt() where lengthInt != count(msg) {
SocketLogger.err("parsing error: \(str)", client: self)
return
}
if count(msg) != 0 {
@@ -502,43 +549,26 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
fixDoubleUTF8(&message)
}
let type = PacketType(str: (message["^(\\d)"].groups()?[1]))
let type = PacketType(str: (message["^(\\d)"].groups()?[1])) ?? {
self.checkIfMessageIsBase64Binary(message)
return PacketType.NOOP
}()
if type == PacketType.MESSAGE {
// Remove message type
switch type {
case PacketType.MESSAGE:
message.removeAtIndex(message.startIndex)
if let client = client {
dispatch_async(client.handleQueue) {[weak client] in
client?.parseSocketMessage(message)
}
}
} else if type == PacketType.NOOP {
doPoll()
} else if type == PacketType.PONG {
// We should upgrade
if message == "3probe" {
upgradeTransport()
}
} else if type == PacketType.OPEN {
handleMessage(message)
case PacketType.NOOP:
handleNOOP()
case PacketType.PONG:
handlePong(message)
case PacketType.OPEN:
message.removeAtIndex(message.startIndex)
handleOpen(message)
} else if type == PacketType.CLOSE {
if polling {
client?.engineDidClose("Disconnect")
}
} else if message.hasPrefix("b4") {
// binary in base64 string
message.removeRange(Range<String.Index>(start: message.startIndex,
end: advance(message.startIndex, 2)))
if let data = NSData(base64EncodedString: message,
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters), client = client {
dispatch_async(client.handleQueue) {[weak self] in
self?.client?.parseBinaryData(data)
}
}
case PacketType.CLOSE:
handleClose()
default:
SocketLogger.log("Got unknown packet type", client: self)
}
}
@@ -557,7 +587,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
}
}
func sendPing() {
@objc private func sendPing() {
//Server is not responding
if pongsMissed > pongsMissedMax {
pingTimer?.invalidate()
client?.engineDidClose("Ping timeout")
return
}
++pongsMissed
write("", withType: PacketType.PING, withData: nil)
}
+1 -1
View File
@@ -359,7 +359,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
_connecting = false
if closed || !reconnects {
didDisconnect("Engine closed")
didDisconnect(reason)
} else if !reconnecting {
handleEvent("reconnect", data: [reason], isInternalMessage: true)
tryReconnect()