此函数向我的 React Native 应用程序中的服务器发送请求。
async function request(url, method = 'GET', data, contentType = 'application/json') {
const state = store.getState()
const config = {
method,
headers: {
'Accept': 'application/json',
'Authorization': state.user.token
}
}
if (contentType === 'application/json') config.headers['Content-Type'] = 'application/json'
if (method === 'POST' || method === 'PATCH') {
config.body = data
}
console.log(url, new Date())
const response = await fetch(url, config)
return await response.json()
}
快递上的服务器。带有控制器的路由示例。
router.get('/message/:userID', passport.authenticate('jwt', {session: false}), controller.getAllMessage)
module.exports.getAllMessage = async function(req, res) {
try {
console.log('getAllMessage', new Date())
//какой-то код
res.status(200).json(message)
} catch (e) {
errorHandler(res, e)
}
}
在此示例中,如果我第一次发送请求,则日志之间的时间差异很小,如果请求经过一秒钟然后到达相同的 url,或者与:userID已经存在的 url 不同的 url,则延迟是不确定的秒数,平均为 15-30 秒。其他路线也一样。在 Postman 和 Angular Web 客户端中,所有请求都不会延迟。不知道这里有什么问题。任何帮助将不胜感激。
如果您泄漏问题不在提供的代码中,您可以查看完整代码。
问题原来是由于socket.io。字符串
const socket = io.connect(url)最终出现在组件的函数中。我将它放在组件之前,问题就消失了。