SwiftSocketConnection,无需痛苦地在 iOS 和 OS X 中处理 TCP
安装
Cocoapods
在 Podfile 中的目标中添加以下内容
pod 'SwiftSocketConnection', '0.1.0'
然后
pod install
在您的项目中
Carthage
将以下内容添加到 Cartfile
github "ymazdy/SwiftSocketConnection"
然后
carthage update
在您的项目中
如何使用
创建实例
let connection = SocketConnection("192.168.1.2", "8090")
创建实例时有很多选项可供使用
let connection = SocketConnection(
_ host: String, // the host you are trying to reach (ex: google.com, 192.168.1.2)
_ port: Int, // the port you are connecting to (ex: 80, 8080, 65000)
_ bufferSize: Int = 32768, // the max buffer size for input data (defaults to 32768 which is 32KB good for most type of incoming data excluding images)
connected: (() -> Void)? = nil, // a handler for when the connection is established and both incoming and outgoing channels are available
disconnected: (() -> Void)? = nil, // a handler for when the connection is disconnected
autoReconnect: Bool = true, // whether to autoReconnect when the client is disconnected
readTimeout: Double = 0.02, // timeout for all read operations in seconds (you will get each data packet after the timeout to make sure no other data is left)
sendDelay: Double = 0, // delay between each send event in seconds, the send request would go in a q and would be proccesed after each other on the delay
sendOnConnect: Bool = true, // send all the data in q when client is reconncted
retries: Int = 10 // retries for each reconnection attempts
)
关于连接和断开处理程序以及连接到主机
let connection = SocketConnection("192.168.1.2", "8090")
connection.onConnected {
print("Connected")
}
connection.onDisconnected {
print("disconnected")
}
connection.connect()
获取传入数据
在闭包中接收数据
let connection = SocketConnection("192.168.1.2", "8090")
connection.onData {(data: Data) in
print(data)
}
connection.connect()
在闭包中接收字符串
let connection = SocketConnection("192.168.1.2", "8090")
connection.onString {(str: String) in
print(str)
}
connection.connect()
接收直到特定分隔符的字符串
let connection = SocketConnection("192.168.1.2", "8090")
connection.until(until: "delimiter", timeout: 1.5, {(str: String) in
print(str)
}, _ failed: {
print("failed to get data")
})
connection.connect()
使用代理接收字符串
首先符合代理要求
class MyClass: NSObject, SocketConnectionDelegate {
}
然后在你的类中实现onString函数
func onString(string: String) {
print(string)
}
然后设置连接代理
let connection = SocketConnection("192.168.1.2", "8090")
connection.delegate = self
connection.connect()
发送数据
SwiftSocketConnection支持许多类型的输出数据
- 字符串
- 字节
- 字节数组
- 数据
- 整数
- 任意数组(基本上是任何类型的数据)
let connection = SocketConnection("192.168.1.2", "8090")
connection.onString {(str: String) in
print(str)
}
connection.onConnected {
let result1 = connection.send(int: 10)
let result2 = connection.send(data: Data())
let result3 = connection.send(byte: UInt8(1))
let result4 = connection.send(bytes: [UInt8(1), UInt8(2), UInt8(3)])
let result5 = connection.send(string: "hello")
let result6 = connection.send(array: [1, UInt8(1), "hello", Data(), 10])
}
connection.connect()
每个发送请求都返回枚举SocketResult
public enum SocketResult: Int {
case notConnected = -2 // socket is not connected and data would be sent upon connection (if the sendOnConnect parameter is set to true)
case waiting = -1 // data is added to q for sending after delay
case success = 1 // data was sent successfuly
case unknown = 2 // unknown error occured (Contact support at [email protected])
}
贡献
贡献始终以拉取请求的形式接受
支持
联系方式 [email protected]