NWSocket 0.0.1

NWSocket 0.0.1

Hoang Nguyen维护。



NWSocket 0.0.1

  • 作者
  • Hoang Nguyen

NWSocket

NWSocket是一个符合SocketInterface的socket连接的Swift类。它提供了一个方便的接口来管理socket连接、处理连接事件以及执行读写操作。

功能

  • 连接状态:使用isConnected属性跟踪当前的连接状态。
  • 委托模式:实现SocketDelegate协议以接收各种socket事件的回调。
  • 可配置:自定义socket配置、调度队列和日志策略。

集成

CocoaPods 集成

要使用 CocoaPods 将 NetworkCompose 集成到您的 Xcode 项目中,请在您的 Podfile 中添加以下内容

pod 'NWSocket'

然后运行

pod install

通过 Swift 包管理器 (SPM) 集成

要使用 Swift 包管理器将 NetworkCompose 集成进来,请在您的 Package.swift 文件中添加以下内容

dependencies: [
    .package(url: "https://github.com/harryngict/NWSocket.git", from: "0.0.1")
],
targets: [
    .target(
        name: "YourTargetName",
        dependencies: ["NWSocket"]
    )
]

将 "YourTargetName" 替换为您的目标名称。然后,运行

swift package update

使用方法

初始化

final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
          let socket = Socket()
          socket.delegate = self
          do {
            try socket.connect(toHost: "your host", onPort: 1234)
          } catch {
            debugPrint("Error: \(error.localizedDescription)")
          }
    }
}

extension ViewController: SocketDelegate {
  func didReady(_ socket: SocketInterface, host: String, port: UInt16) {
    debugPrint("didReady: \(socket), host: \(host), port: \(port)")
  }

  func didReceive(_ socket: SocketInterface, data: Data) {
    debugPrint("didReceive: \(socket), data: \(data)")
  }

  func didDisconnect(_ socket: SocketInterface) {
    debugPrint("didDisconnect: \(socket)")

  }

  func didConnect(_ socket: SocketInterface, 
                  withError error: Error?,
                  withTag: String) {
    debugPrint("didConnect: \(socket), error: \(String(describing: error)), withTag: \(withTag)")

  }
}