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 items: The items to send with this event. May be left out.
|
||||
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
|
||||
@ -267,7 +271,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
||||
/// - 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.
|
||||
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
|
||||
|
||||
@ -25,20 +25,47 @@
|
||||
import Foundation
|
||||
|
||||
/// 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 {}
|
||||
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 representation of self that can sent over socket.io.
|
||||
func socketRepresentation() throws -> SocketData
|
||||
}
|
||||
|
||||
public extension SocketData {
|
||||
/// Default implementation. Only works for native Swift types and a few Foundation types.
|
||||
func socketRepresentation() -> SocketData {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
public typealias AckCallback = ([Any]) -> Void
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user