Initial Swift4 work

This commit is contained in:
Erik 2017-05-20 09:27:01 -04:00 committed by Erik Little
parent e9296fd65a
commit cd3eb37ac8
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
6 changed files with 34 additions and 2 deletions

View File

@ -65,6 +65,7 @@ public final class SocketAckEmitter : NSObject {
/// Call to ack receiving this event. /// Call to ack receiving this event.
/// ///
/// - parameter items: An array of items to send when acking. Use `[]` to send nothing. /// - parameter items: An array of items to send when acking. Use `[]` to send nothing.
@objc
public func with(_ items: [Any]) { public func with(_ items: [Any]) {
guard ackNum != -1 else { return } guard ackNum != -1 else { return }
@ -104,6 +105,7 @@ public final class OnAckCallback : NSObject {
/// - parameter after: The number of seconds before this emit times out if an ack hasn't been received. /// - parameter after: The number of seconds before this emit times out if an ack hasn't been received.
/// - parameter callback: The callback called when an ack is received, or when a timeout happens. /// - parameter callback: The callback called when an ack is received, or when a timeout happens.
/// To check for timeout, use `SocketAckStatus`'s `noAck` case. /// To check for timeout, use `SocketAckStatus`'s `noAck` case.
@objc
public func timingOut(after seconds: Double, callback: @escaping AckCallback) { public func timingOut(after seconds: Double, callback: @escaping AckCallback) {
guard let socket = self.socket, ackNumber != -1 else { return } guard let socket = self.socket, ackNumber != -1 else { return }

View File

@ -29,9 +29,11 @@ public final class SocketAnyEvent : NSObject {
// MARK: Properties // MARK: Properties
/// The event name. /// The event name.
@objc
public let event: String public let event: String
/// The data items for this event. /// The data items for this event.
@objc
public let items: [Any]? public let items: [Any]?
/// The description of this event. /// The description of this event.

View File

@ -54,25 +54,31 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
} }
/// If `true` then every time `connect` is called, a new engine will be created. /// If `true` then every time `connect` is called, a new engine will be created.
@objc
public var forceNew = false public var forceNew = false
/// The queue that all interaction with the client should occur on. This is the queue that event handlers are /// The queue that all interaction with the client should occur on. This is the queue that event handlers are
/// called on. /// called on.
@objc
public var handleQueue = DispatchQueue.main public var handleQueue = DispatchQueue.main
/// The namespace for this client. /// The namespace for this client.
@objc
public var nsp = "/" public var nsp = "/"
/// The configuration for this client. /// The configuration for this client.
public var config: SocketIOClientConfiguration public var config: SocketIOClientConfiguration
/// If `true`, this client will try and reconnect on any disconnects. /// If `true`, this client will try and reconnect on any disconnects.
@objc
public var reconnects = true public var reconnects = true
/// The number of seconds to wait before attempting to reconnect. /// The number of seconds to wait before attempting to reconnect.
@objc
public var reconnectWait = 10 public var reconnectWait = 10
/// The session id of this client. /// The session id of this client.
@objc
public var sid: String? { public var sid: String? {
return engine?.sid return engine?.sid
} }
@ -81,6 +87,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// ///
/// If changed after calling `init`, `forceNew` must be set to `true`, or it will only connect to the url set in the /// If changed after calling `init`, `forceNew` must be set to `true`, or it will only connect to the url set in the
/// init. /// init.
@objc
public var socketURL: URL public var socketURL: URL
var ackHandlers = SocketAckManager() var ackHandlers = SocketAckManager()
@ -141,6 +148,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// ///
/// - parameter socketURL: The url of the socket.io server. /// - parameter socketURL: The url of the socket.io server.
/// - parameter config: The config for this socket. /// - parameter config: The config for this socket.
@objc
public convenience init(socketURL: NSURL, config: NSDictionary?) { public convenience init(socketURL: NSURL, config: NSDictionary?) {
self.init(socketURL: socketURL as URL, config: config?.toSocketConfiguration() ?? []) self.init(socketURL: socketURL as URL, config: config?.toSocketConfiguration() ?? [])
} }
@ -163,6 +171,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
} }
/// Connect to the server. /// Connect to the server.
@objc
open func connect() { open func connect() {
connect(timeoutAfter: 0, withHandler: nil) connect(timeoutAfter: 0, withHandler: nil)
} }
@ -172,6 +181,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection /// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
/// has failed. Pass 0 to never timeout. /// has failed. Pass 0 to never timeout.
/// - parameter withHandler: The handler to call when the client fails to connect. /// - parameter withHandler: The handler to call when the client fails to connect.
@objc
open func connect(timeoutAfter: Double, withHandler handler: (() -> ())?) { open func connect(timeoutAfter: Double, withHandler handler: (() -> ())?) {
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)") assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
@ -229,6 +239,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
} }
/// Disconnects the socket. /// Disconnects the socket.
@objc
open func disconnect() { open func disconnect() {
DefaultSocketLogger.Logger.log("Closing socket", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Closing socket", type: SocketIOClient.logType)
@ -257,6 +268,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// ///
/// - parameter event: The event to send. /// - parameter event: The event to send.
/// - parameter with: The items to send with this event. May be left out. /// - parameter with: The items to send with this event. May be left out.
@objc
open func emit(_ event: String, with items: [Any]) { open func emit(_ event: String, with items: [Any]) {
guard status == .connected else { guard status == .connected else {
handleClientEvent(.error, data: ["Tried emitting \(event) when not connected"]) handleClientEvent(.error, data: ["Tried emitting \(event) when not connected"])
@ -314,6 +326,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter event: The event to send. /// - parameter event: The event to send.
/// - parameter with: The items to send with this event. Use `[]` to send nothing. /// - parameter with: The items to send with this event. Use `[]` to send nothing.
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent. /// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
@objc
open func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback { open func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback {
return createOnAck([event] + items) return createOnAck([event] + items)
} }
@ -405,6 +418,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter data: the data associated with this event. /// - parameter data: the data associated with this event.
/// - parameter isInternalMessage: If `true` event handlers for this event will be called regardless of status. /// - parameter isInternalMessage: If `true` event handlers for this event will be called regardless of status.
/// - parameter withAck: The ack number for this event. May be left out. /// - parameter withAck: The ack number for this event. May be left out.
@objc
open func handleEvent(_ event: String, data: [Any], isInternalMessage: Bool, withAck ack: Int = -1) { open func handleEvent(_ event: String, data: [Any], isInternalMessage: Bool, withAck ack: Int = -1) {
guard status == .connected || isInternalMessage else { return } guard status == .connected || isInternalMessage else { return }
@ -422,6 +436,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
} }
/// Leaves nsp and goes back to the default namespace. /// Leaves nsp and goes back to the default namespace.
@objc
open func leaveNamespace() { open func leaveNamespace() {
if nsp != "/" { if nsp != "/" {
engine?.send("1\(nsp)", withData: []) engine?.send("1\(nsp)", withData: [])
@ -434,6 +449,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// **Do not use this to join the default namespace.** Instead call `leaveNamespace`. /// **Do not use this to join the default namespace.** Instead call `leaveNamespace`.
/// ///
/// - parameter namespace: The namespace to join. /// - parameter namespace: The namespace to join.
@objc
open func joinNamespace(_ namespace: String) { open func joinNamespace(_ namespace: String) {
nsp = namespace nsp = namespace
@ -457,6 +473,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// If you wish to remove a specific event, call the `off(id:)` with the UUID received from its `on` call. /// If you wish to remove a specific event, call the `off(id:)` with the UUID received from its `on` call.
/// ///
/// - parameter event: The event to remove handlers for. /// - parameter event: The event to remove handlers for.
@objc
open func off(_ event: String) { open func off(_ event: String) {
DefaultSocketLogger.Logger.log("Removing handler for event: \(event)", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Removing handler for event: \(event)", type: SocketIOClient.logType)
@ -468,6 +485,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// If you want to remove all events for an event, call the off `off(_:)` method with the event name. /// If you want to remove all events for an event, call the off `off(_:)` method with the event name.
/// ///
/// - parameter id: The UUID of the handler you wish to remove. /// - parameter id: The UUID of the handler you wish to remove.
@objc
open func off(id: UUID) { open func off(id: UUID) {
DefaultSocketLogger.Logger.log("Removing handler with id: \(id)", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Removing handler with id: \(id)", type: SocketIOClient.logType)
@ -479,6 +497,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter event: The event name for this handler. /// - parameter event: The event name for this handler.
/// - parameter callback: The callback that will execute when this event is received. /// - parameter callback: The callback that will execute when this event is received.
/// - returns: A unique id for the handler that can be used to remove it. /// - returns: A unique id for the handler that can be used to remove it.
@objc
@discardableResult @discardableResult
open func on(_ event: String, callback: @escaping NormalCallback) -> UUID { open func on(_ event: String, callback: @escaping NormalCallback) -> UUID {
DefaultSocketLogger.Logger.log("Adding handler for event: \(event)", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Adding handler for event: \(event)", type: SocketIOClient.logType)
@ -527,6 +546,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter event: The event name for this handler. /// - parameter event: The event name for this handler.
/// - parameter callback: The callback that will execute when this event is received. /// - parameter callback: The callback that will execute when this event is received.
/// - returns: A unique id for the handler that can be used to remove it. /// - returns: A unique id for the handler that can be used to remove it.
@objc
@discardableResult @discardableResult
open func once(_ event: String, callback: @escaping NormalCallback) -> UUID { open func once(_ event: String, callback: @escaping NormalCallback) -> UUID {
DefaultSocketLogger.Logger.log("Adding once handler for event: \(event)", type: SocketIOClient.logType) DefaultSocketLogger.Logger.log("Adding once handler for event: \(event)", type: SocketIOClient.logType)
@ -547,6 +567,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// Adds a handler that will be called on every event. /// Adds a handler that will be called on every event.
/// ///
/// - parameter handler: The callback that will execute whenever an event is received. /// - parameter handler: The callback that will execute whenever an event is received.
@objc
open func onAny(_ handler: @escaping (SocketAnyEvent) -> ()) { open func onAny(_ handler: @escaping (SocketAnyEvent) -> ()) {
anyHandler = handler anyHandler = handler
} }
@ -570,6 +591,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// Tries to reconnect to the server. /// Tries to reconnect to the server.
/// ///
/// This will cause a `disconnect` event to be emitted, as well as an `reconnectAttempt` event. /// This will cause a `disconnect` event to be emitted, as well as an `reconnectAttempt` event.
@objc
open func reconnect() { open func reconnect() {
guard !reconnecting else { return } guard !reconnecting else { return }
@ -578,6 +600,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// Removes all handlers. /// Removes all handlers.
/// Can be used after disconnecting to break any potential remaining retain cycles. /// Can be used after disconnecting to break any potential remaining retain cycles.
@objc
open func removeAllHandlers() { open func removeAllHandlers() {
handlers.removeAll(keepingCapacity: false) handlers.removeAll(keepingCapacity: false)
} }

View File

@ -223,7 +223,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
private func handleBase64(message: String) { private func handleBase64(message: String) {
// binary in base64 string // binary in base64 string
let noPrefix = message[message.index(message.startIndex, offsetBy: 2)..<message.endIndex] let noPrefix = String(message[message.index(message.startIndex, offsetBy: 2)..<message.endIndex])!
if let data = Data(base64Encoded: noPrefix, options: .ignoreUnknownCharacters) { if let data = Data(base64Encoded: noPrefix, options: .ignoreUnknownCharacters) {
client?.parseEngineBinaryData(data) client?.parseEngineBinaryData(data)

View File

@ -47,6 +47,7 @@ open class SocketClientManager : NSObject {
// MARK: Properties. // MARK: Properties.
/// The shared manager. /// The shared manager.
@objc
open static let sharedManager = SocketClientManager() open static let sharedManager = SocketClientManager()
private var sockets = [String: SocketIOClient]() private var sockets = [String: SocketIOClient]()
@ -70,6 +71,7 @@ open class SocketClientManager : NSObject {
/// ///
/// - parameter socket: The socket to add. /// - parameter socket: The socket to add.
/// - parameter labeledAs: The label for this socket. /// - parameter labeledAs: The label for this socket.
@objc
open func addSocket(_ socket: SocketIOClient, labeledAs label: String) { open func addSocket(_ socket: SocketIOClient, labeledAs label: String) {
sockets[label] = socket sockets[label] = socket
} }
@ -78,6 +80,7 @@ open class SocketClientManager : NSObject {
/// ///
/// - parameter withLabel: The label of the socket to remove. /// - parameter withLabel: The label of the socket to remove.
/// - returns: The socket for the given label, if one was present. /// - returns: The socket for the given label, if one was present.
@objc
@discardableResult @discardableResult
open func removeSocket(withLabel label: String) -> SocketIOClient? { open func removeSocket(withLabel label: String) -> SocketIOClient? {
return sockets.removeValue(forKey: label) return sockets.removeValue(forKey: label)
@ -87,6 +90,7 @@ open class SocketClientManager : NSObject {
/// ///
/// - parameter socket: The socket to remove. /// - parameter socket: The socket to remove.
/// - returns: The socket if it was in the manager. /// - returns: The socket if it was in the manager.
@objc
@discardableResult @discardableResult
open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient? { open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient? {
var returnSocket: SocketIOClient? var returnSocket: SocketIOClient?
@ -99,6 +103,7 @@ open class SocketClientManager : NSObject {
} }
/// Removes all the sockets in the manager. /// Removes all the sockets in the manager.
@objc
open func removeSockets() { open func removeSockets() {
sockets.removeAll() sockets.removeAll()
} }

@ -1 +1 @@
Subproject commit f7e28f24ae20898da5804079319da52682bb9212 Subproject commit 4ae6fe995316a49e7ac4ee0aaba9b3a0ba7f774d