Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ee104bb5e | ||
|
|
90bbc9521f | ||
|
|
7a1a0d91f9 | ||
|
|
6cb372e3b9 | ||
|
|
10ffe54b08 | ||
|
|
08d100b9ca | ||
|
|
1b05418674 | ||
|
|
75a4adfbbd |
No files matched your search
@@ -110,8 +110,9 @@ Methods
|
||||
7. `connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection.
|
||||
8. `connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
|
||||
9. `close(#fast:Bool)` - Closes the socket. Once a socket is closed it should not be reopened. Pass true to fast if you're closing from a background task.
|
||||
10. `reconnect()` - Causes the client to reconnect to the server.
|
||||
|
||||
Events
|
||||
Client Events
|
||||
------
|
||||
1. `connect` - Emitted when on a successful connection.
|
||||
2. `disconnect` - Emitted when the connection is closed.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "Socket.IO-Client-Swift"
|
||||
s.version = "2.2.0"
|
||||
s.version = "2.2.2"
|
||||
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.2.0' }
|
||||
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.2.2' }
|
||||
s.source_files = "SocketIOClientSwift/**/*.swift"
|
||||
s.requires_arc = true
|
||||
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
|
||||
|
||||
@@ -95,9 +95,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
}
|
||||
|
||||
public init(client:SocketEngineClient, sessionDelegate:NSURLSessionDelegate?) {
|
||||
self.client = client
|
||||
self.session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(),
|
||||
delegate: sessionDelegate, delegateQueue: self.workQueue)
|
||||
self.client = client
|
||||
self.session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(),
|
||||
delegate: sessionDelegate, delegateQueue: self.workQueue)
|
||||
}
|
||||
|
||||
public convenience init(client:SocketEngineClient, opts:NSDictionary?) {
|
||||
@@ -122,7 +122,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
self.write("", withType: PacketType.CLOSE, withData: nil)
|
||||
self.ws?.disconnect()
|
||||
self.stopPolling()
|
||||
|
||||
|
||||
if fast || self.polling {
|
||||
self.client?.engineDidClose("Disconnect")
|
||||
}
|
||||
@@ -151,7 +151,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
return ("", "")
|
||||
}
|
||||
|
||||
var url = "\(self.client!.socketURL)\(self.socketPath)/socket.io/?transport="
|
||||
let path = self.socketPath == "" ? "/socket.io" : self.socketPath
|
||||
|
||||
var url = "\(self.client!.socketURL)\(path)/?transport="
|
||||
var urlPolling:String
|
||||
var urlWebSocket:String
|
||||
|
||||
@@ -238,33 +240,32 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
SocketLogger.log("Doing polling request", client: self)
|
||||
|
||||
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
||||
if self == nil {
|
||||
return
|
||||
} else if err != nil {
|
||||
if self!.polling {
|
||||
self?.handlePollingFailed(err.localizedDescription)
|
||||
} else {
|
||||
NSLog(err.localizedDescription)
|
||||
if let this = self {
|
||||
if err != nil {
|
||||
if this.polling {
|
||||
this.handlePollingFailed(err.localizedDescription)
|
||||
} else {
|
||||
NSLog(err.localizedDescription)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
SocketLogger.log("Got polling response", client: self!)
|
||||
|
||||
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
||||
dispatch_async(self!.parseQueue) {[weak self] in
|
||||
self?.parsePollingMessage(str)
|
||||
SocketLogger.log("Got polling response", client: this)
|
||||
|
||||
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
||||
dispatch_async(this.parseQueue) {[weak this] in
|
||||
this?.parsePollingMessage(str)
|
||||
}
|
||||
}
|
||||
|
||||
this.waitingForPoll = false
|
||||
|
||||
if this.fastUpgrade {
|
||||
this.doFastUpgrade()
|
||||
return
|
||||
} else if !this.closed && this.polling {
|
||||
this.doPoll()
|
||||
}
|
||||
}
|
||||
|
||||
self?.waitingForPoll = false
|
||||
|
||||
if self!.fastUpgrade {
|
||||
self?.doFastUpgrade()
|
||||
return
|
||||
} else if !self!.closed && self!.polling {
|
||||
self?.doPoll()
|
||||
}}.resume()
|
||||
}
|
||||
|
||||
@@ -272,18 +273,16 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
SocketLogger.log("Flushing probe wait", client: self)
|
||||
|
||||
dispatch_async(self.emitQueue) {[weak self] in
|
||||
if self == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for waiter in self!.probeWait {
|
||||
self?.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
||||
}
|
||||
|
||||
self?.probeWait.removeAll(keepCapacity: false)
|
||||
|
||||
if self?.postWait.count != 0 {
|
||||
self?.flushWaitingForPostToWebSocket()
|
||||
if let this = self {
|
||||
for waiter in this.probeWait {
|
||||
this.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
||||
}
|
||||
|
||||
this.probeWait.removeAll(keepCapacity: false)
|
||||
|
||||
if this.postWait.count != 0 {
|
||||
this.flushWaitingForPostToWebSocket()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,21 +326,22 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
SocketLogger.log("POSTing: \(postStr)", client: self)
|
||||
|
||||
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
||||
if self == nil {
|
||||
return
|
||||
} else if err != nil && self!.polling {
|
||||
self?.handlePollingFailed(err.localizedDescription)
|
||||
return
|
||||
} else if err != nil {
|
||||
NSLog(err.localizedDescription)
|
||||
return
|
||||
}
|
||||
|
||||
self?.waitingForPost = false
|
||||
dispatch_async(self!.emitQueue) {[weak self] in
|
||||
if !self!.fastUpgrade {
|
||||
self?.flushWaitingForPost()
|
||||
self?.doPoll()
|
||||
if let this = self {
|
||||
if err != nil && this.polling {
|
||||
this.handlePollingFailed(err.localizedDescription)
|
||||
return
|
||||
} else if err != nil {
|
||||
NSLog(err.localizedDescription)
|
||||
return
|
||||
}
|
||||
|
||||
this.waitingForPost = false
|
||||
|
||||
dispatch_async(this.emitQueue) {[weak this] in
|
||||
if !(this?.fastUpgrade ?? true) {
|
||||
this?.flushWaitingForPost()
|
||||
this?.doPoll()
|
||||
}
|
||||
}
|
||||
}}.resume()
|
||||
}
|
||||
@@ -466,12 +466,10 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
}
|
||||
|
||||
private func parseEngineData(data:NSData) {
|
||||
if self.client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||
self?.client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
||||
if let client = self.client {
|
||||
dispatch_async(client.handleQueue) {[weak self] in
|
||||
self?.client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,12 +486,10 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
// Remove message type
|
||||
message.removeAtIndex(message.startIndex)
|
||||
|
||||
if self.client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||
self?.client?.parseSocketMessage(message)
|
||||
if let client = self.client {
|
||||
dispatch_async(client.handleQueue) {[weak self] in
|
||||
self?.client?.parseSocketMessage(message)
|
||||
}
|
||||
}
|
||||
} else if type == PacketType.NOOP {
|
||||
self.doPoll()
|
||||
@@ -553,11 +549,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
end: advance(message.startIndex, 2)))
|
||||
|
||||
if let data = NSData(base64EncodedString: message,
|
||||
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
|
||||
where self.client != nil {
|
||||
// println("sending \(data)")
|
||||
|
||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters), client = self.client {
|
||||
dispatch_async(client.handleQueue) {[weak self] in
|
||||
self?.client?.parseBinaryData(data)
|
||||
}
|
||||
}
|
||||
@@ -633,13 +626,11 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
|
||||
self.pingTimer?.invalidate()
|
||||
dispatch_async(dispatch_get_main_queue()) {[weak self] in
|
||||
if self == nil {
|
||||
return
|
||||
if let this = self {
|
||||
this.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(this.pingInterval!),
|
||||
target: this,
|
||||
selector: Selector("sendPing"), userInfo: nil, repeats: true)
|
||||
}
|
||||
|
||||
self?.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self!.pingInterval!),
|
||||
target: self!,
|
||||
selector: Selector("sendPing"), userInfo: nil, repeats: true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,16 +654,14 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
*/
|
||||
public func write(msg:String, withType type:PacketType, withData data:ContiguousArray<NSData>?) {
|
||||
dispatch_async(self.emitQueue) {[weak self] in
|
||||
if self == nil || !self!.connected {
|
||||
return
|
||||
}
|
||||
|
||||
if self!.websocket {
|
||||
SocketLogger.log("Writing ws: \(msg):\(data)", client: self!)
|
||||
self?.sendWebSocketMessage(msg, withType: type, datas: data)
|
||||
} else {
|
||||
SocketLogger.log("Writing poll: \(msg):\(data)", client: self!)
|
||||
self?.sendPollMessage(msg, withType: type, datas: data)
|
||||
if let this = self where this.connected {
|
||||
if this.websocket {
|
||||
SocketLogger.log("Writing ws: \(msg):\(data)", client: this)
|
||||
this.sendWebSocketMessage(msg, withType: type, datas: data)
|
||||
} else {
|
||||
SocketLogger.log("Writing poll: \(msg):\(data)", client: this)
|
||||
this.sendPollMessage(msg, withType: type, datas: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,21 +195,19 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
|
||||
private func createOnAck(event:String, items:[AnyObject]) -> OnAckCallback {
|
||||
return {[weak self, ack = ++self.currentAck] timeout, callback in
|
||||
if self == nil {
|
||||
return
|
||||
}
|
||||
|
||||
self?.ackHandlers.addAck(ack, callback: callback)
|
||||
|
||||
dispatch_async(self!.emitQueue) {
|
||||
self?._emit(event, items, ack: ack)
|
||||
}
|
||||
|
||||
if timeout != 0 {
|
||||
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
|
||||
if let this = self {
|
||||
this.ackHandlers.addAck(ack, callback: callback)
|
||||
|
||||
dispatch_after(time, dispatch_get_main_queue()) {
|
||||
self?.ackHandlers.timeoutAck(ack)
|
||||
dispatch_async(this.emitQueue) {[weak this] in
|
||||
this?._emit(event, items, ack: ack)
|
||||
}
|
||||
|
||||
if timeout != 0 {
|
||||
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
|
||||
|
||||
dispatch_after(time, dispatch_get_main_queue()) {[weak this] in
|
||||
this?.ackHandlers.timeoutAck(ack)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -337,22 +335,21 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
// If the server wants to know that the client received data
|
||||
func emitAck(ack:Int, withData args:[AnyObject]) {
|
||||
dispatch_async(self.emitQueue) {[weak self] in
|
||||
if self == nil || !self!.connected {
|
||||
return
|
||||
}
|
||||
|
||||
let packet = SocketPacket(type: nil, data: args, nsp: self!.nsp, id: ack)
|
||||
let str:String
|
||||
|
||||
SocketParser.parseForEmit(packet)
|
||||
str = packet.createAck()
|
||||
|
||||
SocketLogger.log("Emitting Ack: \(str)", client: self!)
|
||||
|
||||
if packet.type == SocketPacket.PacketType.BINARY_ACK {
|
||||
self?.engine?.send(str, withData: packet.binary)
|
||||
} else {
|
||||
self?.engine?.send(str, withData: nil)
|
||||
if let this = self where this.connected {
|
||||
let packet = SocketPacket(type: nil, data: args, nsp: this.nsp, id: ack)
|
||||
let str:String
|
||||
|
||||
SocketParser.parseForEmit(packet)
|
||||
str = packet.createAck()
|
||||
|
||||
SocketLogger.log("Emitting Ack: \(str)", client: this)
|
||||
|
||||
if packet.type == SocketPacket.PacketType.BINARY_ACK {
|
||||
this.engine?.send(str, withData: packet.binary)
|
||||
} else {
|
||||
this.engine?.send(str, withData: nil)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -484,12 +481,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
self._reconnecting = true
|
||||
|
||||
dispatch_async(dispatch_get_main_queue()) {[weak self] in
|
||||
if self == nil {
|
||||
return
|
||||
if let this = self {
|
||||
this.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(this.reconnectWait),
|
||||
target: this, selector: "tryReconnect", userInfo: nil, repeats: true)
|
||||
}
|
||||
|
||||
self?.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(self!.reconnectWait),
|
||||
target: self!, selector: "tryReconnect", userInfo: nil, repeats: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ class SocketParser {
|
||||
var err:NSError?
|
||||
let stringData = data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
|
||||
let parsed:AnyObject? = NSJSONSerialization.JSONObjectWithData(stringData!,
|
||||
options: NSJSONReadingOptions.AllowFragments, error: &err)
|
||||
options: NSJSONReadingOptions.MutableContainers, error: &err)
|
||||
|
||||
if err != nil {
|
||||
// println(err)
|
||||
|
||||
Reference in new issue
Block a user