diff --git a/README.md b/README.md index 4933e3c..405e951 100644 --- a/README.md +++ b/README.md @@ -78,12 +78,18 @@ Install pods: $ pod install ``` -Import in your swift file: +Import the module: +Swift: ```swift import Socket_IO_Client_Swift ``` +Objective-C: +```Objective-C +#import +``` + ##API Constructors ----------- @@ -104,6 +110,7 @@ Options - `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. - `path: String` - If the server uses a custom path. ex: `"/swift"`. Default is `""` +- `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil. Methods ------- diff --git a/Socket.IO-Client-Swift.podspec b/Socket.IO-Client-Swift.podspec index b2e4519..1848cb9 100644 --- a/Socket.IO-Client-Swift.podspec +++ b/Socket.IO-Client-Swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Socket.IO-Client-Swift" - s.version = "2.3.6" + s.version = "2.3.7" 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.6' } + s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.3.7' } s.source_files = "SocketIOClientSwift/**/*.swift" s.requires_arc = true # s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files diff --git a/SocketIOClientSwift/SocketEngine.swift b/SocketIOClientSwift/SocketEngine.swift index ab390b5..2437c1a 100644 --- a/SocketIOClientSwift/SocketEngine.swift +++ b/SocketIOClientSwift/SocketEngine.swift @@ -37,6 +37,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { private var closed = false private var _connected = false + private var extraHeaders:[String: String]? private var fastUpgrade = false private var forcePolling = false private var forceWebsockets = false @@ -109,6 +110,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { cookies = opts?["cookies"] as? [NSHTTPCookie] log = opts?["log"] as? Bool ?? false socketPath = opts?["path"] as? String ?? "" + extraHeaders = opts?["extraHeaders"] as? [String: String] } deinit { @@ -194,6 +196,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { private func createWebsocket(andConnect connect:Bool) { ws = WebSocket(url: NSURL(string: urlWebSocket! + "&sid=\(sid)")!, cookies: cookies) + + if extraHeaders != nil { + for (headerName, value) in extraHeaders! { + ws?.headers[headerName] = value + } + } + ws?.queue = handleQueue ws?.delegate = self @@ -228,7 +237,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!) req.allHTTPHeaderFields = headers } - + + if extraHeaders != nil { + for (headerName, value) in extraHeaders! { + req.setValue(value, forHTTPHeaderField: headerName) + } + } + doRequest(req) } @@ -476,7 +491,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!) reqPolling.allHTTPHeaderFields = headers } - + + if extraHeaders != nil { + for (headerName, value) in extraHeaders! { + reqPolling.setValue(value, forHTTPHeaderField: headerName) + } + } + doRequest(reqPolling) }