@@ -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 ?
@@ -470,10 +478,10 @@ public class SocketEngine: NSObject, WebSocketDelegate {
470478 }
471479
472480 if self !. websocket {
473- // println ("sending ws: \(msg):\(datas)")
481+ // NSLog ("sending ws: \(msg):\(datas)")
474482 self ? . sendWebSocketMessage ( msg, withType: PacketType . MESSAGE, datas: datas)
475483 } else {
476- // println ("sending poll: \(msg):\(datas)")
484+ // NSLog ("sending poll: \(msg):\(datas)")
477485 self ? . sendPollMessage ( msg, withType: PacketType . MESSAGE, datas: datas)
478486 }
479487 }
0 commit comments