junlarsen / league-connect

:electric_plug: Node.js HTTP/1.1, HTTP/2.0 and WebSocket interface to the League of Legends Client APIs
https://www.npmjs.com/package/league-connect
MIT License
156 stars 24 forks source link

Error: connect ECONNREFUSED #127

Open kvrzh opened 1 month ago

kvrzh commented 1 month ago

image Hey, you have very good project, but i have some problem, this code will fail in 100% times, when you are not in LOL, then you enter the game. Seems like session is not rdy to make requests...

Also, just a proposal, will be to trigger client.on('connect', () => {}) even on first start of game)

EugeneWilson commented 1 month ago

This is caused because when the process is created the internal server has not yet had time to initialize and is not ready to take requests. This error on connection could be fixed by a loop calling the client and catching any ECONNREFUSED errors similar to what we did with web sockets. Adding something like this in the authenticate function would prevent said error during startup

return new Promise<Credentials>(function testConnection(resolve, reject) {
    createHttp1Request({ method: 'GET', url: '/process-control/v1/process' }, credentials)
      .then(() => resolve(credentials))
      .catch((err) => {
        if (err.code === 'ECONNREFUSED') {
          setTimeout(testConnection, pollInterval, resolve, reject)
        } else {
          reject(err)
        }
      })
  })

I have no problem creating a PR that adds it if that's a feature @junlarsen would like. My question would be if it's a new connection option or added to awaitConnection