Swift 3 和 Alamofire 4.0,需要时请求

标签 swift request alamofire

所以我有这个代码

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()
                        }
                    }
                }
            }
        }
    }
}

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

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 次。但是……

我只想在服务器有内容要显示时向服务器发送请求,在本例中是来自用户的消息。

我应该怎么做?

最佳答案

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

如果您是这个意思,您可以使用推送通知 并仅在收到消息通知时刷新。除非您不知道何时需要执行您的请求。

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

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

希望对你有帮助

皮埃尔

关于Swift 3 和 Alamofire 4.0,需要时请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521328/

相关文章:

ios - 如何在不更改 NavigationItem 标题的情况下推送新 Controller ?

node.js - 如何修改核心设置或node js默认库中的node js请求超时?

快速 UDP 连接

ios - 从 Parse 检索多个图像的更好方法

javascript - 多个 Promise 请求在 Javascript 中链接在一起

ajax - 如何在redux中发出AJAX请求

swift - 插入函数作为参数

swift - Alamofire PUT 不起作用

ios - 使用 Alamofire 将文件保存到 .DocumentDirectory

ios - 哪个 UITextView 在 Swift 中启动 textViewDidChange?