title: SockJS -> Infinite xhr-streaming calls after reconnect
body: <p>im using Stomp over SockJS and Spring websockets as backend. Occasionally Im having problems after my stomp client reconnect (wifi loss, server down, other). The connection is perfectly restored but passed few seconds i can see in browser network how SockJS client try and try send xhr-streaming with the old session id. The backend responde with close frame c[1000, "Go Away!"] The application its still work but this problem load the CPU and slow the application. </p>
  
  <p>I could play turning off and starting the server several times (Not always happen). I cant understand how SockJS once reconnected ( We destroy and create the Sockjs instance from 0 ) send xhr-streaming requests with the id of the old session ( Maintains the pre-reconnection id session? memory leak? ). I'd like to keep it Sockjs not any state after reconnection and always starting from scratch.</p>
  
  <p><a href="https://i.stack.imgur.com/nbOzr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nbOzr.png" alt="enter image description here"></a></p>
  
  <p>Backend log:</p>
  
  <pre><code>2016-05-02 21:45:01.943 DEBUG [http-nio-8090-exec-7] o.s.w.s.s.t.h.XhrStreamingTransportHandler - Connection already closed (but not removed yet) for XhrStreamingSockJsSession[id=ypsjtids]
  </code></pre>
  
  <p>Client log:</p>
  
  <p>(New Sessión Id -> h2pystok, Old session Id -> ypsjtids)</p>
  
  <pre><code>&lt;&lt;&lt; PONG
  browser.js:120 sockjs-client:buffered-sender send +45ms "\n"
  browser.js:120 sockjs-client:buffered-sender sendSchedule +0ms 1
  browser.js:120 sockjs-client:ajax-based create ajax sender +2ms        http://localhost/eess-services/stomp/355/h2pystok ["\n"]
  browser.js:120 sockjs-client:browser:xhr POST +0ms http://localhost/eess-services/stomp/355/h2pystok/xhr_send
  ws.js:216 &gt;&gt;&gt; PING
  browser.js:120 sockjs-client:browser:xhr withCredentials +2ms
  browser.js:120 sockjs-client:browser:xhr readyState +18ms 4
  browser.js:120 sockjs-client:browser:xhr status +1ms 200
  browser.js:120 sockjs-client:browser:xhr finish +0ms 200 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh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hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
  o
  h
  h
  h
  h
  h
  
  browser.js:120 sockjs-client:receiver:xhr finish +0ms 200 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
  
  browser.js:120 sockjs-client:receiver:xhr _chunkHandler +0ms 200
  browser.js:120 sockjs-client:receiver:xhr close +0ms network
  browser.js:120 sockjs-client:polling close +1ms null network undefined
  browser.js:120 sockjs-client:polling _scheduleReceiver +0ms
  browser.js:120 sockjs-client:receiver:xhr http://localhost/eess-   services/stomp/266/ypsjtids/xhr_streaming +0ms
  browser.js:120 sockjs-client:browser:xhr POST +1ms http://localhost/eess-services/stomp/266/ypsjtids/xhr_streaming
  </code></pre>
  
  <p>There is a issue in github:
  <a href="https://github.com/sockjs/sockjs-client/issues/308" rel="noreferrer">https://github.com/sockjs/sockjs-client/issues/308</a></p>
  
  <p>I have this problem in production :(
  Regards.</p>
  
  <p>EDIT:</p>
  
  <p>I found where is the possible error. In polling.js i have see a special treatment when the close reason is 'network'. When is network again call the function _scheduleReceiver(). When we reconnect the infinite loop occurs. I dont know what is the reason for this treatment but I could try this deleting the special treatment of 'network' and everything works correctly. @skozin Can you try?</p>
  
  <p>if (!self.pollIsClosing) { if (reason === 'network') { self._scheduleReceiver(); } else { self.emit('close', code || 1006, reason); self.removeAllListeners(); } }</p>
  
  <p>the workaround is:</p>
  
  <p>if (!self.pollIsClosing) { self.emit('close', code || 1006, reason); self.removeAllListeners(); }</p>