title: Sending data in a timer by JS WebRTC crashes when reloading before the end
body: <p>I have this simple piece of code in a server.js javascript file served by node:</p>
  
  <pre><code>function multiStep(myConnection, data) {
      var i=0;
          var myTimer=setInterval(function() {
          if (i&lt;data.length){
              var element=JSON.stringify(data[i]);
              console.log("mando elemento: "+element);
              myConnection.send(element);
              i++;
          }
      }, 3000);
  }
  
  //require our websocket library 
  clearInterval(myTimer);
  var WebSocketServer = require('ws').Server; 
  //creating a websocket server at port 9090 
  var wss = new WebSocketServer({port: 9090}); 
  
  //when a user connects to our sever 
  wss.on('connection', function(connection) {
  
      loadJSON(function(response) { 
  
  //when server gets a message from a connected user 
          connection.on('message', function(message){ 
                  console.log("Got message from a user:", message); 
          });
          var json = JSON.parse(response);
          multiStep(connection, json, 0);
      })
  });
  </code></pre>
  
  <p>loadJSON simply loads a json data file from another web site.
  When I run the client application the first time or when the timeout has ended everything goes fine. Yet if I reload the page while the timeout is not finished I get a crash when I try to use the connection of the old page on the server with report:</p>
  
  <blockquote>
    <p>/var/www/html/MATERIALI/phonegap/node_modules/ws/lib/WebSocket.js:219
        else throw new Error('not opened');
                   ^ Error: not opened
        at WebSocket.send (/var/www/html/MATERIALI/phonegap/node_modules/ws/lib/WebSocket.js:219:16)
        at null. (/var/www/html/MATERIALI/phonegap/WebRTC/server.js:36:9)
        at wrapper [as _onTimeout] (timers.js:261:14)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)</p>
  </blockquote>
  
  <p>As a matter of fact I could simply ignore the old session given the page is reloaded. How do I avoid the server to crash in these circumstances?</p>