Compare commits

...
24 Commits
Author SHA1 Message Date
Erik cd48abe4cd bump version 2015-04-21 12:06:45 -04:00
Erik acfbcb6fe1 Merge branch 'master' of https://github.com/socketio/socket.io-client-swift 2015-04-21 12:03:52 -04:00
Erik 47f5dcbbcd style 2015-04-21 12:03:48 -04:00
Erik Little a161ed1756 Merge pull request #58 from yannickl/master
[FIX] Plus signs (+) is not URL encoded #57
2015-04-21 12:02:41 -04:00
Yannick Loriot 3fda340ad1 [FIX] Plus signs (+) is not URL encoded #57 2015-04-21 17:30:19 +02:00
Erik e33309827f actually use capture list 2015-04-20 15:18:08 -04:00
Erik 1753707bf8 don't use implicitly unwrapped engine 2015-04-20 13:54:16 -04:00
Erik cc99eae737 Add messages for socket/engine deinit 2015-04-20 12:42:35 -04:00
Erik 446cf57978 remove newline 2015-04-17 17:26:50 -04:00
Erik 80cee820a4 tweaks 2015-04-17 10:52:55 -04:00
Erik e4c2da515b clarify non-cocoapods installing 2015-04-17 10:19:22 -04:00
Erik bb4d285928 make note about required xcode version 2015-04-17 10:01:11 -04:00
Erik 5e5ec2f6b6 bump version 2015-04-17 07:57:34 -04:00
Erik 54aac236b8 update readme 2015-04-17 07:56:59 -04:00
Erik 8b05f922a6 rename socketackmap, code style changes for #55 2015-04-17 07:49:48 -04:00
Erik Little f38cf9ed3a Merge pull request #55 from ramki982/master
Adding support for self signed certificates by passing in a NSURLSessionDelegate optional
2015-04-17 07:44:23 -04:00
ramki982 057be73990 Uncommenting the string extension 2015-04-17 16:44:56 +05:30
ram ea3bd02f6a Adding a NSURLSessionDelegate to socket client options and in turn to socket engine to allow support for cases like self-signed certificates 2015-04-17 16:39:21 +05:30
Erik Little 06f04a046d Merge pull request #37 from redgreenapp/master
set cookie headers in doPoll and flushWaitingForPost
2015-04-16 16:59:02 -04:00
Matt Renaud ae27d93464 set cookie headers in doPoll and flushWaitingForPost 2015-04-16 16:48:06 -04:00
Erik 406504b09d tweaks 2015-04-16 14:27:19 -04:00
Erik 269bb5a943 tweaks 2015-04-15 09:48:03 -04:00
Erik 4be6b58150 async logging, simplify acks 2015-04-14 17:13:39 -04:00
Erik acefc89b0d tweaks 2015-04-14 16:25:08 -04:00
12 changed files with 107 additions and 54 deletions

No files matched your search

+6 -1
View File
@@ -47,9 +47,13 @@ SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8
- Can be used from Objective-C
##Installation
Requires Swift 1.2/Xcode 6.3
If you need Swift 1.1/Xcode 6.2 use v1.5.2. (Pre-Swift 1.2 support is no longer maintained)
Manually (iOS 7+)
-----------------
1. Copy the SocketIOClientSwift folder into your Xcode project
1. Copy the SocketIOClientSwift folder into your Xcode project. (Make sure you add the files to your target(s))
2. If you plan on using this from Objective-C, read [this](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html) on exposing Swift code to Objective-C.
CocoaPods 0.36.0 or later (iOS 8+)
------------------
@@ -92,6 +96,7 @@ Options
- `nsp: String` Default is `"/"`
- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil.
- `log: Bool` If `true` socket will log debug messages. Default is false.
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
Methods
-------
+2 -2
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Socket.IO-Client-Swift"
s.version = "2.0.4"
s.version = "2.0.6"
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.0.4' }
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.0.6' }
s.source_files = "SocketIOClientSwift/**/*.swift"
s.requires_arc = true
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
@@ -1,5 +1,5 @@
//
// SocketAckMap.swift
// SocketAckManager.swift
// SocketIO-Swift
//
// Created by Erik Little on 4/3/15.
@@ -24,32 +24,51 @@
import Foundation
struct SocketAckMap {
private var acks = [Int: AckCallback]()
private var waiting = [Int: Bool]()
private struct SocketAck: Hashable, Equatable {
let ack:Int
var callback:AckCallback!
var hashValue:Int {
return ack.hashValue
}
init(ack:Int) {
self.ack = ack
}
init(ack:Int, callback:AckCallback) {
self.ack = ack
self.callback = callback
}
}
private func <(lhs:SocketAck, rhs:SocketAck) -> Bool {
return lhs.ack < rhs.ack
}
private func ==(lhs:SocketAck, rhs:SocketAck) -> Bool {
return lhs.ack == rhs.ack
}
struct SocketAckManager {
private var acks = Set<SocketAck>(minimumCapacity: 1)
mutating func addAck(ack:Int, callback:AckCallback) {
waiting[ack] = true
acks[ack] = callback
self.acks.insert(SocketAck(ack: ack, callback: callback))
}
mutating func executeAck(ack:Int, items:[AnyObject]?) {
let callback = acks[ack]
waiting[ack] = false
let callback = self.acks.remove(SocketAck(ack: ack))
dispatch_async(dispatch_get_main_queue()) {
callback?(items)
callback?.callback(items)
}
acks.removeValueForKey(ack)
}
mutating func timeoutAck(ack:Int) {
if waiting[ack] != nil && waiting[ack]! {
acks[ack]?(["NO ACK"])
}
let callback = self.acks.remove(SocketAck(ack: ack))
acks.removeValueForKey(ack)
waiting.removeValueForKey(ack)
dispatch_async(dispatch_get_main_queue()) {
callback?.callback(["NO ACK"])
}
}
}
+2 -2
View File
@@ -26,9 +26,9 @@ import Foundation
@objc public final class SocketAnyEvent {
public let event:String!
public var items:[AnyObject]?
public let items:NSArray?
init(event:String, items:[AnyObject]?) {
init(event:String, items:NSArray?) {
self.event = event
self.items = items
}
+29 -9
View File
@@ -93,14 +93,19 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
}
public init(client:SocketEngineClient, forcePolling:Bool,
forceWebsockets:Bool, withCookies cookies:[NSHTTPCookie]?, logging:Bool) {
forceWebsockets:Bool, withCookies cookies:[NSHTTPCookie]?, logging:Bool,
withSessionDelegate sessionDelegate:NSURLSessionDelegate?) {
self.client = client
self.forcePolling = forcePolling
self.forceWebsockets = forceWebsockets
self.cookies = cookies
self.log = logging
self.session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(),
delegate: nil, delegateQueue: self.workQueue)
delegate: sessionDelegate, delegateQueue: self.workQueue)
}
deinit {
SocketLogger.log("Engine is being deinit", client: self)
}
public func close(#fast:Bool) {
@@ -120,7 +125,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
var byteArray = [UInt8](count: 1, repeatedValue: 0x0)
byteArray[0] = 4
var mutData = NSMutableData(bytes: &byteArray, length: 1)
mutData.appendData(data)
return (mutData, nil)
} else {
var str = "b4"
@@ -149,15 +156,17 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
}
if params != nil {
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" ").invertedSet
for (key, value) in params! {
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
NSCharacterSet.URLHostAllowedCharacterSet())!
allowedCharacterSet)!
urlPolling += "&\(keyEsc)="
urlWebSocket += "&\(keyEsc)="
if value is String {
let valueEsc = (value as! String).stringByAddingPercentEncodingWithAllowedCharacters(
NSCharacterSet.URLHostAllowedCharacterSet())!
allowedCharacterSet)!
urlPolling += "\(valueEsc)"
urlWebSocket += "\(valueEsc)"
} else {
@@ -201,7 +210,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
self.waitingForPoll = true
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
if self.cookies != nil {
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
req.allHTTPHeaderFields = headers
}
self.doRequest(req)
}
@@ -285,7 +299,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
self.postWait.removeAll(keepCapacity: false)
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)")!)
if self.cookies != nil {
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
req.allHTTPHeaderFields = headers
}
req.HTTPMethod = "POST"
req.setValue("text/plain; charset=UTF-8", forHTTPHeaderField: "Content-Type")
@@ -358,6 +377,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
}
SocketLogger.log("Starting engine", client: self)
SocketLogger.log("Handshaking", client: self)
self.closed = false
let (urlPolling, urlWebSocket) = self.createURLs(opts)
@@ -397,11 +417,10 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
func testLength(length:String, inout n:Int) -> Bool {
if let num = length.toInt() {
n = num
return false
} else {
return true
}
return false
}
for var i = 0, l = str.length; i < l; i = i &+ 1 {
@@ -486,7 +505,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
message.removeAtIndex(message.startIndex)
let mesData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
if let json = NSJSONSerialization.JSONObjectWithData(mesData, options: NSJSONReadingOptions.AllowFragments,
if let json = NSJSONSerialization.JSONObjectWithData(mesData,
options: NSJSONReadingOptions.AllowFragments,
error: &err) as? NSDictionary, let sid = json["sid"] as? String {
self.sid = sid
self._connected = true
+1 -1
View File
@@ -26,7 +26,7 @@
import Foundation
func fixDoubleUTF8(inout name:String) {
let utf8 = name.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)!
let utf8 = name.dataUsingEncoding(NSISOLatin1StringEncoding)!
let latin1 = NSString(data: utf8, encoding: NSUTF8StringEncoding)!
name = latin1 as String
}
+13 -3
View File
@@ -42,10 +42,11 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
let reconnectAttempts:Int!
let logType = "SocketClient"
var ackHandlers = SocketAckMap()
var ackHandlers = SocketAckManager()
var currentAck = -1
var log = false
var waitingData = ContiguousArray<SocketPacket>()
var sessionDelegate:NSURLSessionDelegate?
public let socketURL:String
public let handleAckQueue = dispatch_queue_create("handleAckQueue", DISPATCH_QUEUE_SERIAL)
@@ -90,6 +91,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
// Set options
if opts != nil {
if let sessionDelegate = opts!["sessionDelegate"] as? NSURLSessionDelegate {
self.sessionDelegate = sessionDelegate
}
if let cookies = opts!["cookies"] as? [NSHTTPCookie] {
self.cookies = cookies
}
@@ -138,6 +143,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
self.init(socketURL: socketURL, opts: options)
}
deinit {
SocketLogger.log("Client is being deinit", client: self)
}
private func addEngine() {
SocketLogger.log("Adding engine", client: self)
@@ -145,7 +154,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
forcePolling: self.forcePolling,
forceWebsockets: self.forceWebsockets,
withCookies: self.cookies,
logging: self.log)
logging: self.log,
withSessionDelegate: self.sessionDelegate)
}
/**
@@ -337,7 +347,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
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 {
+14 -4
View File
@@ -30,15 +30,25 @@ protocol SocketLogClient {
}
final class SocketLogger {
private static let printQueue = dispatch_queue_create("printQueue", DISPATCH_QUEUE_SERIAL)
static func log(message:String, client:SocketLogClient, altType:String? = nil) {
if client.log {
NSLog("%@: %@", altType ?? client.logType, message)
if !client.log {
return
}
dispatch_async(printQueue) {[type = client.logType] in
NSLog("%@: %@", altType ?? type, message)
}
}
static func err(message:String, client:SocketLogClient, altType:String? = nil) {
if client.log {
NSLog("ERROR %@: %@", altType ?? client.logType, message)
if !client.log {
return
}
dispatch_async(printQueue) {[type = client.logType] in
NSLog("ERROR %@: %@", altType ?? type, message)
}
}
}
+2 -10
View File
@@ -77,22 +77,14 @@ final class SocketPacket: Printable {
}
func addData(data:NSData) -> Bool {
func checkDoEvent() -> Bool {
if self.placeholders == self.currentPlace {
return true
} else {
return false
}
}
if checkDoEvent() {
if self.placeholders == self.currentPlace {
return true
}
self.binary.append(data)
self.currentPlace++
if checkDoEvent() {
if self.placeholders == self.currentPlace {
self.currentPlace = 0
return true
} else {
-3
View File
@@ -242,10 +242,7 @@ class SocketParser {
}
}
// Handles binary data
static func parseBinaryData(data:NSData, socket:SocketIOClient) {
// NSLog(data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros))
if socket.waitingData.count == 0 {
SocketLogger.err("Got data when not remaking packet", client: socket, altType: "SocketParser")
return
+1 -1
View File
@@ -31,4 +31,4 @@ import Foundation
public typealias AckCallback = @objc_block (NSArray?) -> Void
public typealias AckEmitter = (AnyObject...) -> Void
public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
+1 -1
View File
@@ -152,7 +152,7 @@ public class SwiftRegex: NSObject, BooleanType {
pos = matchRange.location + matchRange.length
}
out.appendString(substring( NSRange(location:pos, length:targetRange.length-pos))!)
out.appendString(substring(NSRange(location:pos, length:targetRange.length-pos))!)
return out as String
}