所以我有这个代码
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/