【问题标题】:Swift 3 and Alamofire 4.0, request when neededSwift 3 和 Alamofire 4.0,需要时请求
【发布时间】:2017-11-15 05:19:41
【问题描述】:

所以我有这个代码

func requestMSG(){
    if uID != nil {
        let parameter: Parameters = [
            "id" : self.uID!
        ]
        Alamofire.request("http://front\(randomServer).omegle.com/events", method: .post, parameters: parameter).responseData { (response) in
            if let check = response.error{
                print(check.localizedDescription)
            }
        }
        Alamofire.request("http://front\(randomServer).omegle.com/events",method: .post , parameters: parameter).responseJSON { (response) in
            if response.result.value != nil {
                print("This is response: \(response)")
                if let result = response.result.value {
                    let JSON = result as? [[String]]
                    if JSON?.count == 1{
                        if JSON?[0].count == 2{
                            print(JSON?[0][1] as Any)
                            self.chatLog.append((JSON?[0][1])!)
                            self.chatTable.reloadData()
                        }
                    }
                }
            }
        }
    }
}

稍后,我触发了一个“定时器”,它从“/event”请求一些信息。 响应看起来像这样

This is response: SUCCESS: <null>
This is response: SUCCESS: <null>
This is response: SUCCESS: <null>
This is response: SUCCESS: <null>
This is response: SUCCESS: (
        "hiii there"
Optional("hiii there")
This is response: SUCCESS: <null>
This is response: SUCCESS: <null>

每秒请求 1 次。但是……

我只想在服务器有东西要显示时才向服务器发送请求,在这个例子中,是来自用户的消息。

我该怎么做?

【问题讨论】:

    标签: swift request alamofire


    【解决方案1】:

    您的意思是仅在发送新消息时才收到消息吗?想知道何时设置了消息?

    如果您的意思是,认为您可以使用 推送通知 并仅在收到消息通知时刷新。除非您无法知道何时需要完成您的请求。

    您也可以使用 socketIo 发出实时请求(但您的服务器端需要在 NodeJs 中)

    我认为最好的解决方案是使用通知来处理您的应用程序并在收到消息时请求消息。

    希望对你有帮助...

    皮埃尔

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多