Implement #672
This commit is contained in:
parent
cc3d7f8b07
commit
af5e934d69
@ -234,7 +234,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
/// - parameter event: The event to send.
|
/// - parameter event: The event to send.
|
||||||
/// - parameter items: The items to send with this event. May be left out.
|
/// - parameter items: The items to send with this event. May be left out.
|
||||||
open func emit(_ event: String, _ items: SocketData...) {
|
open func emit(_ event: String, _ items: SocketData...) {
|
||||||
emit(event, with: items)
|
do {
|
||||||
|
emit(event, with: try items.map({ try $0.socketRepresentation() }))
|
||||||
|
} catch {
|
||||||
|
fatalError("Error creating socketRepresentation for emit: \(event), \(items)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as emit, but meant for Objective-C
|
/// Same as emit, but meant for Objective-C
|
||||||
@ -267,7 +271,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
/// - parameter items: The items to send with this event. May be left out.
|
/// - parameter items: The items to send with this event. May be left out.
|
||||||
/// - 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.
|
||||||
open func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
|
open func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
|
||||||
return emitWithAck(event, with: items)
|
do {
|
||||||
|
return emitWithAck(event, with: try items.map({ try $0.socketRepresentation() }))
|
||||||
|
} catch {
|
||||||
|
fatalError("Error creating socketRepresentation for emit: \(event), \(items)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as emitWithAck, but for Objective-C
|
/// Same as emitWithAck, but for Objective-C
|
||||||
|
|||||||
@ -25,20 +25,47 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
/// A marking protocol that says a type can be represented in a socket.io packet.
|
/// A marking protocol that says a type can be represented in a socket.io packet.
|
||||||
public protocol SocketData {}
|
///
|
||||||
|
/// Example:
|
||||||
|
///
|
||||||
|
/// ```swift
|
||||||
|
/// struct CustomData : SocketData {
|
||||||
|
/// let name: String
|
||||||
|
/// let age: Int
|
||||||
|
///
|
||||||
|
/// func socketRepresentation() -> SocketData {
|
||||||
|
/// return ["name": name, "age": age]
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// socket.emit("myEvent", CustomData(name: "Erik", age: 24))
|
||||||
|
/// ```
|
||||||
|
public protocol SocketData {
|
||||||
|
// MARK: Methods
|
||||||
|
|
||||||
extension Array : SocketData {}
|
/// A representation of self that can sent over socket.io.
|
||||||
extension Bool : SocketData {}
|
func socketRepresentation() throws -> SocketData
|
||||||
extension Dictionary : SocketData {}
|
}
|
||||||
extension Double : SocketData {}
|
|
||||||
extension Int : SocketData {}
|
public extension SocketData {
|
||||||
extension NSArray : SocketData {}
|
/// Default implementation. Only works for native Swift types and a few Foundation types.
|
||||||
extension Data : SocketData {}
|
func socketRepresentation() -> SocketData {
|
||||||
extension NSData : SocketData {}
|
return self
|
||||||
extension NSDictionary : SocketData {}
|
}
|
||||||
extension NSString : SocketData {}
|
}
|
||||||
extension NSNull : SocketData {}
|
|
||||||
extension String : SocketData {}
|
extension Array : SocketData { }
|
||||||
|
extension Bool : SocketData { }
|
||||||
|
extension Dictionary : SocketData { }
|
||||||
|
extension Double : SocketData { }
|
||||||
|
extension Int : SocketData { }
|
||||||
|
extension NSArray : SocketData { }
|
||||||
|
extension Data : SocketData { }
|
||||||
|
extension NSData : SocketData { }
|
||||||
|
extension NSDictionary : SocketData { }
|
||||||
|
extension NSString : SocketData { }
|
||||||
|
extension NSNull : SocketData { }
|
||||||
|
extension String : SocketData { }
|
||||||
|
|
||||||
/// A typealias for an ack callback.
|
/// A typealias for an ack callback.
|
||||||
public typealias AckCallback = ([Any]) -> Void
|
public typealias AckCallback = ([Any]) -> Void
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user