title: send audio using html5 websockets
body: <p>My requirement is to send audio chunks captured from microphone to the server using html5 web socket connection.
  Establish a web socket connection with the server.When user is speaking capture small chunks of audio and send to the server. This should happen till the user stops speaking for 10 seconds, then close the web socket connection. Again open the web socket connection when user starts speaking.</p>
  <p>I know how to open a connection and send the audio for the first time.
  I have two buttons. onClick of one button(startRecording) i will open web socket connection and i am recording the audio.
  onClick of second button(stopRecording) i am sending the audio to the server.</p>
  <p>But my problem is how to do this with out buttons and it has to record the audio till user pause and send to the server.</p>
  <pre><code>  window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
  
    var recorder;
    var audio = document.querySelector('audio');
    var websocket ;
    var text;
      var onSuccess = function(s) {
      var context = new AudioContext();
      var mediaStreamSource = context.createMediaStreamSource(s);
      recorder = new Recorder(mediaStreamSource);
      recorder.record();
  
      // audio loopback
      // mediaStreamSource.connect(context.destination);
    }
  
    
    function startRecording() {
        var wsURI = &quot;url&quot;;
  
          websocket = new WebSocket(wsURI);
          websocket.onopen = function(evt) { 
          onOpen(evt)
          };
          websocket.onclose = function(evt) { onClose() };
          websocket.onmessage = function(evt) { onMessage(evt) };
          websocket.onerror = function(evt) { onError(evt) };
          function onOpen(evt) {
                var message = {
                  'action': 'start',
                  //'content-type': 'audio/l16;rate=22050' 
                  'content-type': 'audio/wav;rate=22050'
                };
                websocket.send(JSON.stringify(message));
              }
          function onMessage(evt) {
                console.log(evt.data);
                //console.log(JSON.parse(evt.data))
                
                }
                               
              }
          function onClose() {
              websocket.close();
              }
      if (navigator.getUserMedia) {
        navigator.getUserMedia({audio: true}, onSuccess, onFail);
      } else {
        console.log('navigator.getUserMedia not present');
      }
    }
  
    function stopRecording() {
      recorder.stop();
      recorder.exportWAV(function(s) {
        //audio.src = window.URL.createObjectURL(s);
          websocket.send(s);
          //websocket.onclose();
      });
    }
  </code></pre>