HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
878 stars 142 forks source link

Endpoint runs on app start? #84

Open sethdorris opened 7 years ago

sethdorris commented 7 years ago

I am finding that the endpoint runs without a user ever arriving at that endpoint. Example:

  app.ws('/tracker', function(ws, req) {
    var indexOfMock = 0;
    //Send current week's team to only the connected client;
    function nextPoll() {
      var p1 = MockAPIPolling(indexOfMock).then(apires => { return apires; })
      var p2 = pool.query(api.MainLeagueStatTrackerData, [api.GetCurrentWeek()]).then(data => { return data.rows })
      return Promise.all([p1, p2]).then(([results, league]) => {
        if (returnObj.broadcast) {
          var model = StatTrackerVMCreator.Create(results, league);
          wss.clients.forEach(client => {
            client.send(JSON.stringify(model))
          })
        }
        if (!returnObj.raceFinished) { //taking off the if to let it run continuously on test
          return setTimeout(nextPoll, 5000)
        }
      })
    }
    nextPoll();
  })

Full file in the repo can be found here.