最近開發(fā)項(xiàng)目中遇到個(gè)問題:
之前項(xiàng)目長連接用的GCDAsyncSocket庫聘殖,但在iOS14上偶現(xiàn)崩潰,而且GCDAsyncSocket功能比較簡單摄凡;
后面換成SwiftNIO废赞,SwiftNIO也遇到問題弟劲,在iOS 11上偶現(xiàn)alloc崩潰,提交issue也沒解決镶摘,而且SwiftNIO比較“笨重”嗽桩,接口雖然先進(jìn),但對新手不大友好凄敢;
鑒于以上原因碌冶,我寫了個(gè)Swift TCP網(wǎng)絡(luò)庫,使用簡單涝缝,歡迎大家接入
https://github.com/dlleng/SwiftSocket
SwiftSocket
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
Installation
SwiftSocket is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'libSwiftSocket'
Task
let client = ClientChannel(observer: self)
client.eventLoop.execute {
//task
}
let timerTask = client.eventLoop.execute(timer: 1) {
//timer task
}
let delayTask = client.eventLoop.execute(after: 1) {
//delay task
}
Client
let client = ClientChannel(observer: self)
client.connect(host: "www.apple.com", port: 80)
extension XXX: ChannelObserver {
func channel(_ client: ClientChannel, didDisconnect error: ChannelError?) {
print("connect err: \(String(describing: error))")
}
func channel(_ client: ClientChannel, didConnect host: String, port: Int) {
print("connect \(host):\(port) successed ")
client.enableHeartBeat(interval: 10, resetOnRead: true, resetOnWrite: true)
print("\(client.localAddress)")
print("\(client.remoteAddress)")
}
func channel(_ client: ClientChannel, didRead buffer: ByteBuffer) {
let str = String(data: buffer.toData(), encoding: .utf8) ?? "NULL"
print("\(client) read: \(buffer.count) \(str)")
client.write(data: "rcv: \(str)".data(using: .utf8)!)
}
func channel(_ client: ClientChannel, didWrite buffer: ByteBuffer, userInfo: [String: Any]?) {
print("\(client) write: \(buffer.count)")
}
func channelHeartBeat(_ client: ClientChannel) {
print("should send heartbeat")
client.write(data: "heartbeat\n".data(using: .utf8)!)
}
}
Server
let server = ServerChannel(observer: self)
do {
try server.startServer(host: "0.0.0.0", port: 9999)
print("Start server successed")
} catch {
print(error)
}
extension XXX: ChannelObserver {
func channel(_ client: ClientChannel, didDisconnect error: ChannelError?) {
print("connect err: \(String(describing: error))")
}
func channel(_ client: ClientChannel, didConnect host: String, port: Int) {
print("connect \(host):\(port) successed ")
}
func channel(_ client: ClientChannel, didRead buffer: ByteBuffer) {
let str = String(data: buffer.toData(), encoding: .utf8) ?? "NULL"
print("\(client) read: \(buffer.count) \(str)")
client.write(data: "rcv: \(str)".data(using: .utf8)!)
}
func channel(_ client: ClientChannel, didWrite buffer: ByteBuffer, userInfo: [String: Any]?) {
print("\(client) write: \(buffer.count)")
}
func channel(_ server: ServerChannel, didAccept client: ClientChannel) {
print("didAccept : \(client)")
self.client = client
//if need
//client.enableHeartBeat(interval: 10, resetOnRead: true, resetOnWrite: true)
}
//if need
//func channelHeartBeat(_ client: ClientChannel) {
//print("Heartbeat")
//client.write(data: "heartbeat\n".data(using: .utf8)!)
//}
}
Author
dlleng, 2190931560@qq.com
License
SwiftSocket is available under the MIT license. See the LICENSE file for more info.