@@ -67,6 +67,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
6767 var connected : Bool {
6868 return self . _connected
6969 }
70+ var cookies : [ NSHTTPCookie ] ?
7071 var pingInterval : Int ?
7172 var polling : Bool {
7273 return self . _polling
@@ -79,9 +80,10 @@ public class SocketEngine: NSObject, WebSocketDelegate {
7980 }
8081 var ws : WebSocket ?
8182
82- init ( client: SocketIOClient , forcePolling: Bool = false ) {
83+ init ( client: SocketIOClient , forcePolling: Bool = false , withCookies cookies : [ NSHTTPCookie ] ? ) {
8384 self . client = client
8485 self . forcePolling = forcePolling
86+ self . cookies = cookies
8587 self . session = NSURLSession ( configuration: NSURLSessionConfiguration . ephemeralSessionConfiguration ( ) ,
8688 delegate: nil , delegateQueue: self . workQueue)
8789 }
@@ -288,7 +290,13 @@ public class SocketEngine: NSObject, WebSocketDelegate {
288290 let ( urlPolling, urlWebSocket) = self . createURLs ( params: opts)
289291 self . urlPolling = urlPolling
290292 self . urlWebSocket = urlWebSocket
291- let reqPolling = NSURLRequest ( URL: NSURL ( string: urlPolling + " &b64=1 " ) !)
293+ let reqPolling = NSMutableURLRequest ( URL: NSURL ( string: urlPolling + " &b64=1 " ) !)
294+
295+ if self . cookies != nil {
296+ let headers = NSHTTPCookie . requestHeaderFieldsWithCookies ( self . cookies!)
297+ reqPolling. allHTTPHeaderFields = headers
298+ }
299+
292300
293301 self . session. dataTaskWithRequest ( reqPolling) { [ weak self] data, res, err in
294302 var err2 : NSError ?
@@ -472,10 +480,10 @@ public class SocketEngine: NSObject, WebSocketDelegate {
472480 }
473481
474482 if self !. websocket {
475- // println ("sending ws: \(msg):\(datas)")
483+ // NSLog ("sending ws: \(msg):\(datas)")
476484 self ? . sendWebSocketMessage ( msg, withType: PacketType . MESSAGE, datas: datas)
477485 } else {
478- // println ("sending poll: \(msg):\(datas)")
486+ // NSLog ("sending poll: \(msg):\(datas)")
479487 self ? . sendPollMessage ( msg, withType: PacketType . MESSAGE, datas: datas)
480488 }
481489 }
@@ -588,4 +596,4 @@ public class SocketEngine: NSObject, WebSocketDelegate {
588596 public func websocketDidReceiveData( socket: WebSocket , data: NSData ) {
589597 self . parseEngineData ( data)
590598 }
591- }
599+ }
0 commit comments