This commit is contained in:
Lukas Schmidt 2015-08-10 06:34:33 +02:00
commit f46629f850
4 changed files with 33 additions and 21 deletions

View File

@ -122,6 +122,7 @@ Options
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil. - `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 `""` - `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. - `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil.
- `handleQueue: dispatch_queue_t` - The dispatch queue that handlers are run on. Default is the main queue.
Methods Methods
------- -------

View File

@ -51,12 +51,10 @@ struct SocketEventHandler {
self.callBackObjectiveC = callback self.callBackObjectiveC = callback
} }
func executeCallback(items: [AnyObject]? = nil, withAck ack: Int? = nil, withAckType type: Int? = nil, func executeCallback(items:NSArray? = nil, withAck ack:Int? = nil, withAckType type:Int? = nil,
withSocket socket:SocketIOClient? = nil) { withSocket socket:SocketIOClient? = nil) {
dispatch_async(dispatch_get_main_queue()) {
self.callback != nil ? self.callback != nil ?
self.callback?(items, emitAckCallback(socket, num: ack)) self.callback?(items, emitAckCallback(socket, num: ack))
: self.callBackObjectiveC?(items, emitAckCallbackObjectiveC(socket, num: ack)) : self.callBackObjectiveC?(items, emitAckCallbackObjectiveC(socket, num: ack))
} }
} }
}

View File

@ -45,7 +45,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
public let socketURL: String public let socketURL: String
public let handleAckQueue = dispatch_queue_create("handleAckQueue", DISPATCH_QUEUE_SERIAL) public let handleAckQueue = dispatch_queue_create("handleAckQueue", DISPATCH_QUEUE_SERIAL)
public let handleQueue = dispatch_queue_create("handleQueue", DISPATCH_QUEUE_SERIAL) public let handleQueue: dispatch_queue_t!
public let emitQueue = dispatch_queue_create("emitQueue", DISPATCH_QUEUE_SERIAL) public let emitQueue = dispatch_queue_create("emitQueue", DISPATCH_QUEUE_SERIAL)
public var closed: Bool { public var closed: Bool {
return _closed return _closed
@ -111,6 +111,12 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
self.reconnectWait = abs(reconnectWait) self.reconnectWait = abs(reconnectWait)
} }
if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t {
self.handleQueue = handleQueue
} else {
self.handleQueue = dispatch_get_main_queue()
}
super.init() super.init()
} }
@ -373,19 +379,23 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
args: event, data ?? "") args: event, data ?? "")
if anyHandler != nil { if anyHandler != nil {
dispatch_async(dispatch_get_main_queue()) {[weak self] in dispatch_async(handleQueue) {[weak self] in
self?.anyHandler?(SocketAnyEvent(event: event, items: data)) self?.anyHandler?(SocketAnyEvent(event: event, items: data))
} }
} }
for handler in handlers where handler.event == event { for handler in handlers where handler.event == event {
if ack != nil { if ack != nil {
dispatch_async(handleQueue) {[weak self] in
handler.executeCallback(data, withAck: ack!, withSocket: self) handler.executeCallback(data, withAck: ack!, withSocket: self)
}
} else { } else {
dispatch_async(handleQueue) {
handler.executeCallback(data) handler.executeCallback(data)
} }
} }
} }
}
/** /**
Leaves nsp and goes back to / Leaves nsp and goes back to /
@ -427,6 +437,9 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
handlers.append(handler) handlers.append(handler)
} }
/**
Adds a handler for an event.
*/
public func onObjectiveC(event: String, callback: NormalCallbackObjectiveC) { public func onObjectiveC(event: String, callback: NormalCallbackObjectiveC) {
SocketLogger.log("Adding handler for event: %@", client: self, args: event) SocketLogger.log("Adding handler for event: %@", client: self, args: event)

View File

@ -388,7 +388,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
///validates the HTTP is a 101 as per the RFC spec ///validates the HTTP is a 101 as per the RFC spec
private func validateResponse(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool { private func validateResponse(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool {
let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, 0).takeRetainedValue() let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
CFHTTPMessageAppendBytes(response, buffer, bufferLen) CFHTTPMessageAppendBytes(response, buffer, bufferLen)
if CFHTTPMessageGetResponseStatusCode(response) != 101 { if CFHTTPMessageGetResponseStatusCode(response) != 101 {
return false return false
@ -854,7 +854,7 @@ public class Security {
} }
var policy: SecPolicyRef var policy: SecPolicyRef
if self.validatedDN { if self.validatedDN {
policy = SecPolicyCreateSSL(1, domain) policy = SecPolicyCreateSSL(true, domain)
} else { } else {
policy = SecPolicyCreateBasicX509() policy = SecPolicyCreateBasicX509()
} }