refactor reconnect
This commit is contained in:
parent
6da9463c8d
commit
76a572e735
@ -28,7 +28,17 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
public let socketURL: NSURL
|
public let socketURL: NSURL
|
||||||
|
|
||||||
public private(set) var engine: SocketEngineSpec?
|
public private(set) var engine: SocketEngineSpec?
|
||||||
public private(set) var status = SocketIOClientStatus.NotConnected
|
public private(set) var status = SocketIOClientStatus.NotConnected {
|
||||||
|
didSet {
|
||||||
|
switch status {
|
||||||
|
case .Connected:
|
||||||
|
reconnecting = false
|
||||||
|
currentReconnectAttempt = 0
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public var forceNew = false
|
public var forceNew = false
|
||||||
public var nsp = "/"
|
public var nsp = "/"
|
||||||
@ -46,8 +56,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
private var anyHandler: ((SocketAnyEvent) -> Void)?
|
private var anyHandler: ((SocketAnyEvent) -> Void)?
|
||||||
private var currentReconnectAttempt = 0
|
private var currentReconnectAttempt = 0
|
||||||
private var handlers = [SocketEventHandler]()
|
private var handlers = [SocketEventHandler]()
|
||||||
private var reconnectTimer: NSTimer?
|
|
||||||
private var ackHandlers = SocketAckManager()
|
private var ackHandlers = SocketAckManager()
|
||||||
|
private var reconnecting = false
|
||||||
|
|
||||||
private(set) var currentAck = -1
|
private(set) var currentAck = -1
|
||||||
private(set) var handleQueue = dispatch_get_main_queue()
|
private(set) var handleQueue = dispatch_get_main_queue()
|
||||||
@ -55,9 +65,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
|
|
||||||
var waitingPackets = [SocketPacket]()
|
var waitingPackets = [SocketPacket]()
|
||||||
|
|
||||||
/**
|
/// Type safe way to create a new SocketIOClient. opts can be omitted
|
||||||
Type safe way to create a new SocketIOClient. opts can be omitted
|
|
||||||
*/
|
|
||||||
public init(socketURL: NSURL, options: Set<SocketIOClientOption> = []) {
|
public init(socketURL: NSURL, options: Set<SocketIOClientOption> = []) {
|
||||||
self.options = options
|
self.options = options
|
||||||
self.socketURL = socketURL
|
self.socketURL = socketURL
|
||||||
@ -94,10 +102,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
||||||
Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
/// If using Swift it's recommended to use `init(socketURL: NSURL, options: Set<SocketIOClientOption>)`
|
||||||
If using Swift it's recommended to use `init(socketURL: NSURL, options: Set<SocketIOClientOption>)`
|
|
||||||
*/
|
|
||||||
public convenience init(socketURL: NSURL, options: NSDictionary?) {
|
public convenience init(socketURL: NSURL, options: NSDictionary?) {
|
||||||
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
|
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
|
||||||
}
|
}
|
||||||
@ -127,26 +133,17 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
return engine!
|
return engine!
|
||||||
}
|
}
|
||||||
|
|
||||||
private func clearReconnectTimer() {
|
|
||||||
reconnectTimer?.invalidate()
|
|
||||||
reconnectTimer = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
@available(*, deprecated=5.3, message="Please use disconnect()")
|
@available(*, deprecated=5.3, message="Please use disconnect()")
|
||||||
public func close() {
|
public func close() {
|
||||||
disconnect()
|
disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Connect to the server.
|
||||||
Connect to the server.
|
|
||||||
*/
|
|
||||||
public func connect() {
|
public func connect() {
|
||||||
connect(timeoutAfter: 0, withTimeoutHandler: nil)
|
connect(timeoutAfter: 0, withTimeoutHandler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Connect to the server. If we aren't connected after timeoutAfter, call handler
|
||||||
Connect to the server. If we aren't connected after timeoutAfter, call handler
|
|
||||||
*/
|
|
||||||
public func connect(timeoutAfter timeoutAfter: Int, withTimeoutHandler handler: (() -> Void)?) {
|
public func connect(timeoutAfter timeoutAfter: Int, withTimeoutHandler handler: (() -> Void)?) {
|
||||||
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
|
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
|
||||||
|
|
||||||
@ -203,8 +200,6 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
func didConnect() {
|
func didConnect() {
|
||||||
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
|
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
|
||||||
status = .Connected
|
status = .Connected
|
||||||
currentReconnectAttempt = 0
|
|
||||||
clearReconnectTimer()
|
|
||||||
|
|
||||||
// Don't handle as internal because something crazy could happen where
|
// Don't handle as internal because something crazy could happen where
|
||||||
// we disconnect before it's handled
|
// we disconnect before it's handled
|
||||||
@ -224,10 +219,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
handleEvent("disconnect", data: [reason], isInternalMessage: true)
|
handleEvent("disconnect", data: [reason], isInternalMessage: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Disconnects the socket. Only reconnect the same socket if you know what you're doing.
|
||||||
Disconnects the socket. Only reconnect the same socket if you know what you're doing.
|
/// Will turn off automatic reconnects.
|
||||||
Will turn off automatic reconnects.
|
|
||||||
*/
|
|
||||||
public func disconnect() {
|
public func disconnect() {
|
||||||
DefaultSocketLogger.Logger.log("Closing socket", type: logType)
|
DefaultSocketLogger.Logger.log("Closing socket", type: logType)
|
||||||
|
|
||||||
@ -235,16 +228,12 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
didDisconnect("Disconnect")
|
didDisconnect("Disconnect")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Send a message to the server
|
||||||
Send a message to the server
|
|
||||||
*/
|
|
||||||
public func emit(event: String, _ items: AnyObject...) {
|
public func emit(event: String, _ items: AnyObject...) {
|
||||||
emit(event, withItems: items)
|
emit(event, withItems: items)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Same as emit, but meant for Objective-C
|
||||||
Same as emit, but meant for Objective-C
|
|
||||||
*/
|
|
||||||
public func emit(event: String, withItems items: [AnyObject]) {
|
public func emit(event: String, withItems items: [AnyObject]) {
|
||||||
guard status == .Connected else {
|
guard status == .Connected else {
|
||||||
handleEvent("error", data: ["Tried emitting \(event) when not connected"], isInternalMessage: true)
|
handleEvent("error", data: ["Tried emitting \(event) when not connected"], isInternalMessage: true)
|
||||||
@ -301,10 +290,15 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
|
|
||||||
public func engineDidClose(reason: String) {
|
public func engineDidClose(reason: String) {
|
||||||
waitingPackets.removeAll()
|
waitingPackets.removeAll()
|
||||||
|
|
||||||
|
if status != .Closed {
|
||||||
|
status = .NotConnected
|
||||||
|
}
|
||||||
|
|
||||||
if status == .Closed || !reconnects {
|
if status == .Closed || !reconnects {
|
||||||
didDisconnect(reason)
|
didDisconnect(reason)
|
||||||
} else if status != .Reconnecting {
|
} else if !reconnecting {
|
||||||
|
reconnecting = true
|
||||||
tryReconnectWithReason(reason)
|
tryReconnectWithReason(reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -325,9 +319,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
ackHandlers.executeAck(ack, items: data)
|
ackHandlers.executeAck(ack, items: data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Causes an event to be handled. Only use if you know what you're doing.
|
||||||
Causes an event to be handled. Only use if you know what you're doing.
|
|
||||||
*/
|
|
||||||
public func handleEvent(event: String, data: [AnyObject], isInternalMessage: Bool, withAck ack: Int = -1) {
|
public func handleEvent(event: String, data: [AnyObject], isInternalMessage: Bool, withAck ack: Int = -1) {
|
||||||
guard status == .Connected || isInternalMessage else {
|
guard status == .Connected || isInternalMessage else {
|
||||||
return
|
return
|
||||||
@ -344,9 +336,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Leaves nsp and goes back to /
|
||||||
Leaves nsp and goes back to /
|
|
||||||
*/
|
|
||||||
public func leaveNamespace() {
|
public func leaveNamespace() {
|
||||||
if nsp != "/" {
|
if nsp != "/" {
|
||||||
engine?.send("1\(nsp)", withData: [])
|
engine?.send("1\(nsp)", withData: [])
|
||||||
@ -354,9 +344,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Joins namespace
|
||||||
Joins namespace
|
|
||||||
*/
|
|
||||||
public func joinNamespace(namespace: String) {
|
public func joinNamespace(namespace: String) {
|
||||||
nsp = namespace
|
nsp = namespace
|
||||||
|
|
||||||
@ -366,28 +354,22 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Removes handler(s) based on name
|
||||||
Removes handler(s)
|
|
||||||
*/
|
|
||||||
public func off(event: String) {
|
public func off(event: String) {
|
||||||
DefaultSocketLogger.Logger.log("Removing handler for event: %@", type: logType, args: event)
|
DefaultSocketLogger.Logger.log("Removing handler for event: %@", type: logType, args: event)
|
||||||
|
|
||||||
handlers = handlers.filter { $0.event != event }
|
handlers = handlers.filter { $0.event != event }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Removes a handler with the specified UUID gotten from an `on` or `once`
|
||||||
Removes a handler with the specified UUID gotten from an `on` or `once`
|
|
||||||
*/
|
|
||||||
public func off(id id: NSUUID) {
|
public func off(id id: NSUUID) {
|
||||||
DefaultSocketLogger.Logger.log("Removing handler with id: %@", type: logType, args: id)
|
DefaultSocketLogger.Logger.log("Removing handler with id: %@", type: logType, args: id)
|
||||||
|
|
||||||
handlers = handlers.filter { $0.id != id }
|
handlers = handlers.filter { $0.id != id }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Adds a handler for an event.
|
||||||
Adds a handler for an event.
|
/// Returns: A unique id for the handler
|
||||||
Returns: A unique id for the handler
|
|
||||||
*/
|
|
||||||
public func on(event: String, callback: NormalCallback) -> NSUUID {
|
public func on(event: String, callback: NormalCallback) -> NSUUID {
|
||||||
DefaultSocketLogger.Logger.log("Adding handler for event: %@", type: logType, args: event)
|
DefaultSocketLogger.Logger.log("Adding handler for event: %@", type: logType, args: event)
|
||||||
|
|
||||||
@ -397,10 +379,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
return handler.id
|
return handler.id
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Adds a single-use handler for an event.
|
||||||
Adds a single-use handler for an event.
|
/// Returns: A unique id for the handler
|
||||||
Returns: A unique id for the handler
|
|
||||||
*/
|
|
||||||
public func once(event: String, callback: NormalCallback) -> NSUUID {
|
public func once(event: String, callback: NormalCallback) -> NSUUID {
|
||||||
DefaultSocketLogger.Logger.log("Adding once handler for event: %@", type: logType, args: event)
|
DefaultSocketLogger.Logger.log("Adding once handler for event: %@", type: logType, args: event)
|
||||||
|
|
||||||
@ -417,9 +397,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
return handler.id
|
return handler.id
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Adds a handler that will be called on every event.
|
||||||
Adds a handler that will be called on every event.
|
|
||||||
*/
|
|
||||||
public func onAny(handler: (SocketAnyEvent) -> Void) {
|
public func onAny(handler: (SocketAnyEvent) -> Void) {
|
||||||
anyHandler = handler
|
anyHandler = handler
|
||||||
}
|
}
|
||||||
@ -443,44 +421,32 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Tries to reconnect to the server.
|
||||||
Tries to reconnect to the server.
|
|
||||||
*/
|
|
||||||
public func reconnect() {
|
public func reconnect() {
|
||||||
tryReconnectWithReason("manual reconnect")
|
tryReconnectWithReason("manual reconnect")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// 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.
|
|
||||||
*/
|
|
||||||
public func removeAllHandlers() {
|
public func removeAllHandlers() {
|
||||||
handlers.removeAll(keepCapacity: false)
|
handlers.removeAll(keepCapacity: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func tryReconnectWithReason(reason: String) {
|
private func tryReconnectWithReason(reason: String) {
|
||||||
if reconnectTimer == nil {
|
if reconnecting {
|
||||||
DefaultSocketLogger.Logger.log("Starting reconnect", type: logType)
|
DefaultSocketLogger.Logger.log("Starting reconnect", type: logType)
|
||||||
handleEvent("reconnect", data: [reason], isInternalMessage: true)
|
handleEvent("reconnect", data: [reason], isInternalMessage: true)
|
||||||
|
|
||||||
status = .Reconnecting
|
_tryReconnect()
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue()) {
|
|
||||||
self.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(self.reconnectWait),
|
|
||||||
target: self, selector: "_tryReconnect", userInfo: nil, repeats: true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func _tryReconnect() {
|
@objc private func _tryReconnect() {
|
||||||
if status == .Connected {
|
if !reconnecting {
|
||||||
clearReconnectTimer()
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects {
|
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects {
|
||||||
clearReconnectTimer()
|
|
||||||
didDisconnect("Reconnect Failed")
|
didDisconnect("Reconnect Failed")
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -492,6 +458,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
|
|
||||||
currentReconnectAttempt += 1
|
currentReconnectAttempt += 1
|
||||||
connect()
|
connect()
|
||||||
|
|
||||||
|
let dispatchAfter = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(reconnectWait) * NSEC_PER_SEC))
|
||||||
|
|
||||||
|
dispatch_after(dispatchAfter, dispatch_get_main_queue(), _tryReconnect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@objc public enum SocketIOClientStatus: Int, CustomStringConvertible {
|
@objc public enum SocketIOClientStatus: Int, CustomStringConvertible {
|
||||||
case NotConnected, Closed, Connecting, Connected, Reconnecting
|
case NotConnected, Closed, Connecting, Connected
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
switch self {
|
switch self {
|
||||||
@ -37,8 +37,6 @@ import Foundation
|
|||||||
return "Connecting"
|
return "Connecting"
|
||||||
case Connected:
|
case Connected:
|
||||||
return "Connected"
|
return "Connected"
|
||||||
case Reconnecting:
|
|
||||||
return "Reconnecting"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user