iOS SwiftSocket TCP网络库

最近开发项目中遇到个问题:

之前项目长连接用的GCDAsyncSocket库,但在iOS14上偶现崩溃,而且GCDAsyncSocket功能比较简单;

后面换成SwiftNIO,SwiftNIO也遇到问题,在iOS 11上偶现alloc崩溃,提交issue也没解决,而且SwiftNIO比较“笨重”,接口虽然先进,但对新手不大友好;

鉴于以上原因,我写了个Swift TCP网络库,使用简单,欢迎大家接入

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。