Swift GCDAsyncSocketDelegate 示例
Swift example for GCDAsyncSocketDelegate
参考这个 post 我在从 GCDAsyncSocket 接收数据时遇到了一些问题,找不到有效的 Swift 示例。
import UIKit
import CocoaAsyncSocket
class DiscoveryViewControllerTest: UIViewController, GCDAsyncSocketDelegate{
let host = "192.168.55.1"
let port:UInt16 = 4000
let cmdDeviceInformation = "?0600\r";
let cmdDeviceIStandByeExit = "?060B\r";
let cmdDeviceIStandByeEnter = "?060A\r";
var mSocket: GCDAsyncSocket!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Started wifi scanning!\n")
mSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
do {
try mSocket.connect(toHost: host, onPort: port)
} catch let error {
print(error)
}
print("Connecting to instrument...!\n")
}
public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
print("didConnectToHost!\n");
let data = cmdDeviceIStandByeEnter.data(using: .utf8)
print("TX: ", terminator: " ")
print(data! as NSData)
mSocket.write(data!, withTimeout:10, tag: 0)
mSocket.readData(withTimeout: -1, tag: 0) //This line was missing!
}
public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
print("didWriteData");
}
public func socket(_ sock: GCDAsyncSocket, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
print("didReceiveData")
let rxData:Data = Data()
mSocket.readData(to: rxData, withTimeout: 5, buffer: nil, bufferOffset: 0, tag: 0)
print("RX: ", terminator: " ")
print(rxData as NSData)
}
public func socket(_ sock: GCDAsyncSocket, didRead: Data, withTag tag:CLong){
print("didRead!");
}
public func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
print("didDisconnect!")
}
}
连接和写入方法是运行,但从未调用"didReceive"方法。
控制台输出:
Started wifi scanning!
Connecting to instrument...!
didConnectToHost!
TX: <3f303630 410d> didWriteData
编辑
我解决了我的问题并将问题代码更改为随时可用的示例。
我发现了我的错误。线索是在 socket didConnectToHost()
中的 mSocket.write()
函数调用后面启用读取。完整的函数如下所示:
public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
print("didConnectToHost!\n");
let data = cmdDeviceInformation.data(using: .utf8)
print("TX: ", terminator: " ")
print(data! as NSData)
mSocket.write(data!, withTimeout:10, tag: 0)
mSocket.readData(withTimeout: -1, tag: 0) // Add this line
}
顺便说一句:我编辑了我的问题以创建一个可供所有人使用的示例。
参考这个 post 我在从 GCDAsyncSocket 接收数据时遇到了一些问题,找不到有效的 Swift 示例。
import UIKit
import CocoaAsyncSocket
class DiscoveryViewControllerTest: UIViewController, GCDAsyncSocketDelegate{
let host = "192.168.55.1"
let port:UInt16 = 4000
let cmdDeviceInformation = "?0600\r";
let cmdDeviceIStandByeExit = "?060B\r";
let cmdDeviceIStandByeEnter = "?060A\r";
var mSocket: GCDAsyncSocket!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Started wifi scanning!\n")
mSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
do {
try mSocket.connect(toHost: host, onPort: port)
} catch let error {
print(error)
}
print("Connecting to instrument...!\n")
}
public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
print("didConnectToHost!\n");
let data = cmdDeviceIStandByeEnter.data(using: .utf8)
print("TX: ", terminator: " ")
print(data! as NSData)
mSocket.write(data!, withTimeout:10, tag: 0)
mSocket.readData(withTimeout: -1, tag: 0) //This line was missing!
}
public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
print("didWriteData");
}
public func socket(_ sock: GCDAsyncSocket, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
print("didReceiveData")
let rxData:Data = Data()
mSocket.readData(to: rxData, withTimeout: 5, buffer: nil, bufferOffset: 0, tag: 0)
print("RX: ", terminator: " ")
print(rxData as NSData)
}
public func socket(_ sock: GCDAsyncSocket, didRead: Data, withTag tag:CLong){
print("didRead!");
}
public func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
print("didDisconnect!")
}
}
连接和写入方法是运行,但从未调用"didReceive"方法。
控制台输出:
Started wifi scanning!
Connecting to instrument...!
didConnectToHost!
TX: <3f303630 410d> didWriteData
编辑 我解决了我的问题并将问题代码更改为随时可用的示例。
我发现了我的错误。线索是在 socket didConnectToHost()
中的 mSocket.write()
函数调用后面启用读取。完整的函数如下所示:
public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
print("didConnectToHost!\n");
let data = cmdDeviceInformation.data(using: .utf8)
print("TX: ", terminator: " ")
print(data! as NSData)
mSocket.write(data!, withTimeout:10, tag: 0)
mSocket.readData(withTimeout: -1, tag: 0) // Add this line
}
顺便说一句:我编辑了我的问题以创建一个可供所有人使用的示例。