Socket.io-重新连接前刷新令牌

Socket.io-重新连接前刷新令牌,socket.io,refresh-token,Socket.io,Refresh Token,我正在用Socket.io创建一个电子应用程序。当用户的计算机进入睡眠模式时,服务器将与客户端断开连接,并抛出错误“transport close”。当用户尝试重新连接时,我会检查令牌是否仍然有效,如果无效,我会刷新令牌并尝试将其发送到socketIo服务器 我遇到的问题是,在“reconnect_-trument”socket.io上,它不会等到我刷新令牌后才尝试重新连接,它会立即尝试使用旧令牌重新连接,而旧令牌会被服务器拒绝,这似乎还会终止与用户的连接,从而妨碍以后的重新连接尝试 这是连接到

我正在用Socket.io创建一个电子应用程序。当用户的计算机进入睡眠模式时,服务器将与客户端断开连接,并抛出错误“transport close”。当用户尝试重新连接时,我会检查令牌是否仍然有效,如果无效,我会刷新令牌并尝试将其发送到socketIo服务器

我遇到的问题是,在“reconnect_-trument”socket.io上,它不会等到我刷新令牌后才尝试重新连接,它会立即尝试使用旧令牌重新连接,而旧令牌会被服务器拒绝,这似乎还会终止与用户的连接,从而妨碍以后的重新连接尝试

这是连接到服务器的部分代码

module.exports.connect = async (JWT) => {
    return new Promise( async resolve => {

        console.log("connecting to the server")

        const connectionOptions = {
            secure: true,
            query: {token: JWT},
            reconnectionDelay: 4000
        }

        let socket = await socketIo.connect(`${process.env.SERVER_URL}:${process.env.SERVER_PORT}`, connectionOptions);

        resolve(socket)
    })
}
    io.use(async (socket, next) => {

  let token = socket.handshake.query.token;

  //and the instruction from here https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html
  let tokenIsValid = await checkTokenValidity(token);

  if( tokenIsValid ) {
    next();
  } else {
    next(new Error('invalidToken'));
    console.log("Not valid token")
  }
})
这是我尝试重新连接的代码

socket.on('reconnect_attempt', async () => {

        const getCurrentJWT = require("../../main").getCurrentJWT;

        let JWT = await getCurrentJWT(); //By the time this line returns, socket.io has already tried to reconnect

        if(JWT.success) { //if refreshed successfully
            console.log("Trying to submit new token......", JWT);

            socket.query.token = JWT.JWT;

        } else {
            console.log("Token not refreshed.")
        }
    });
这是我在服务器上的一部分

module.exports.connect = async (JWT) => {
    return new Promise( async resolve => {

        console.log("connecting to the server")

        const connectionOptions = {
            secure: true,
            query: {token: JWT},
            reconnectionDelay: 4000
        }

        let socket = await socketIo.connect(`${process.env.SERVER_URL}:${process.env.SERVER_PORT}`, connectionOptions);

        resolve(socket)
    })
}
    io.use(async (socket, next) => {

  let token = socket.handshake.query.token;

  //and the instruction from here https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html
  let tokenIsValid = await checkTokenValidity(token);

  if( tokenIsValid ) {
    next();
  } else {
    next(new Error('invalidToken'));
    console.log("Not valid token")
  }
})