@@ -184,10 +184,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
184184
185185 guard timeoutAfter != 0 else { return }
186186
187- let time = DispatchTime . now ( ) + Double( UInt64 ( timeoutAfter) * NSEC_PER_SEC) / Double( NSEC_PER_SEC)
188-
189- handleQueue. asyncAfter ( deadline: time) { [ weak self] in
190- guard let this = self , this. status != . connected && this. status != . disconnected else { return }
187+ handleQueue. asyncAfter ( deadline: DispatchTime . now ( ) + Double( timeoutAfter) ) { [ weak self] in
188+ guard let this = self , this. status == . connecting || this. status == . notConnected else { return }
191189
192190 this. status = . disconnected
193191 this. engine? . disconnect ( reason: " Connect timeout " )
@@ -232,13 +230,19 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
232230
233231 /// Send an event to the server, with optional data items.
234232 ///
233+ /// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
234+ /// will be emitted. The structure of the error data is `[eventName, items, theError]`
235+ ///
235236 /// - parameter event: The event to send.
236237 /// - parameter items: The items to send with this event. May be left out.
237238 open func emit( _ event: String , _ items: SocketData ... ) {
238239 do {
239240 emit ( event, with: try items. map ( { try $0. socketRepresentation ( ) } ) )
240- } catch {
241- fatalError ( " Error creating socketRepresentation for emit: \( event) , \( items) " )
241+ } catch let err {
242+ DefaultSocketLogger . Logger. error ( " Error creating socketRepresentation for emit: \( event) , \( items) " ,
243+ type: logType)
244+
245+ handleClientEvent ( . error, data: [ event, items, err] )
242246 }
243247 }
244248
@@ -260,6 +264,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
260264 /// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
261265 /// Check that your server's api will ack the event being sent.
262266 ///
267+ /// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
268+ /// will be emitted. The structure of the error data is `[eventName, items, theError]`
269+ ///
263270 /// Example:
264271 ///
265272 /// ```swift
@@ -274,8 +281,13 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
274281 open func emitWithAck( _ event: String , _ items: SocketData ... ) -> OnAckCallback {
275282 do {
276283 return emitWithAck ( event, with: try items. map ( { try $0. socketRepresentation ( ) } ) )
277- } catch {
278- fatalError ( " Error creating socketRepresentation for emit: \( event) , \( items) " )
284+ } catch let err {
285+ DefaultSocketLogger . Logger. error ( " Error creating socketRepresentation for emit: \( event) , \( items) " ,
286+ type: logType)
287+
288+ handleClientEvent ( . error, data: [ event, items, err] )
289+
290+ return OnAckCallback ( ackNumber: - 1 , items: [ ] , socket: self )
279291 }
280292 }
281293
@@ -426,7 +438,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
426438
427439 /// Removes handler(s) based on an event name.
428440 ///
429- /// If you wish to remove a specific event, call the `off(if :)` with the UUID received from its `on` call.
441+ /// If you wish to remove a specific event, call the `off(id :)` with the UUID received from its `on` call.
430442 ///
431443 /// - parameter event: The event to remove handlers for.
432444 open func off( _ event: String ) {
@@ -555,9 +567,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
555567 }
556568
557569 private func _tryReconnect( ) {
558- guard reconnecting else { return }
570+ guard reconnects && reconnecting && status != . disconnected else { return }
559571
560- if reconnectAttempts != - 1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects {
572+ if reconnectAttempts != - 1 && currentReconnectAttempt + 1 > reconnectAttempts {
561573 return didDisconnect ( reason: " Reconnect Failed " )
562574 }
563575
0 commit comments