迅速没有收到来自redis TCP连接的响应

时间:2018-07-18 06:54:07

标签: swift tcp redis gcdasyncsocket

我当前正在本地计算机上运行Redis,并且正在使用GCDAsyncSocket库连接到它。连接工作正常,我能够执行redis命令,但无法收到来自redis的响应 这是我的代码

func startSocket()
{

    socket = GCDAsyncSocket.init(delegate: self, delegateQueue: DispatchQueue.main)
    do
    {
        try socket.connect(toHost: "127.0.0.1", onPort: 6379, withTimeout: 10)
        print(1)

    }
    catch
    {
        print("error")
    }
}
func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
    print("2")
    connect()
    print(host)
}

func socket (_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
    print("yes")
    if (tag == 1)
    {
    let x = data
    let Recieved: String = String(data: x, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
    print(Recieved)
    }
}
func connect()
{
    let name = "SET ONE TEST \r\n"
    print(name)
    let sent = name.data(using: String.Encoding.utf8)
    socket.write(sent!, withTimeout: 10, tag: 1)
}

当我在redis实例中选中GET ONE时,我按预期方式进行了TEST,但无法在函数“ func socket(_ sock:GCDAsyncSocket,didRead data:Data,withTag tag:Int)”中获取接收到的变量。

0 个答案:

没有答案