

dependencies: [
.package(url: "https://github.com/William-Weng/WWTcpConnection.git", .upToNextMajor(from: "1.0.3"))
]
函数 |
功能 |
state() |
当前连接状态 |
create(minimumLength:maximumLength:delegate:) |
建立连接 |
cancel() |
取消连接 |
sendMessage(_:using:) |
发送文字消息 (APP => TCP) |
sendData(_:) |
发送数据消息 (APP => TCP) |
函数 |
功能 |
connection(_:state:) |
网络连接状态 |
connection(_:error:) |
相关错误状态 |
connection(_:sendContent:state:) |
发送内容消息 |
connection(_:receiveData:state:) |
接收返回消息 |
import UIKit
import Network
import WWPrint
import WWTcpConnection
final class ViewController: UIViewController {
@IBOutlet weak var myTextField: UITextField!
@IBOutlet weak var myLabel: UILabel!
let connection = WWTcpConnection(host: "127.0.0.1", port: 8080)
override func viewDidLoad() {
super.viewDidLoad()
connection.create(delegate: self)
}
@IBAction func send(_ sender: UIBarButtonItem) {
connection.sendMessage(myTextField.text!)
}
}
extension ViewController: WWTcpConnectionDelegete {
func connection(_ connection: WWTcpConnection, state: NWConnection.State) {
wwPrint(state)
}
func connection(_ connection: WWTcpConnection, error: WWTcpConnection.ConnectionError?) {
wwPrint(error)
}
func connection(_ connection: WWTcpConnection, sendContent contentType: WWTcpConnection.ContentType, state: NWConnection.State) {
wwPrint(contentType)
}
func connection(_ connection: WWTcpConnection, receiveData data: Data, state: NWConnection.State) {
DispatchQueue.main.async { self.myLabel.text = String(data: data, encoding: .utf8) }
}
}