Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
5
Indexable
Never
<!DOCTYPE HTML>
<html>
 <head>
  <title>Flask-SocketIO Test</title>
   <script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
   <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.5/socket.io.min.js"></script>
   <script type="text/javascript" charset="utf-8">
    let startTime;
    let endTime;
    let timePeriod = 0;
    let run_time = 0;
    let total_passed_time = 0;
    let stupido = 0;
     $(document).ready(function() {
      console.log("READY")
      namespace = '/test';
      var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace);

      socket.on('connect', function() {
        socket.emit('my_event', {data: 'I\'m connected!', value: 1}); });
      
        socket.on('my_response', function(msg) {
        console.log(msg.data);
        console.log(timePeriod);
        if(timePeriod > 0){
          if(stupido == 0 ){
            stupido = 1;
            run_time = Date.now()
          }else{
            stupido = 0;
            prev_date = run_time; 
            run_time = Date.now();
            timePeriod = timePeriod - (run_time-prev_date)/1000 - 0.3
          }          
          
          //$('#log').append('Received '+': '+msg.data+'<br>').html(); 
          $('#json_response').append('JSON response: '+': '+JSON.stringify(msg.data, null,2)+'<br>').html; 

          }
        else{
          stupido = 0;
        }});
          
      $('form#emit').submit(function(event) {
          socket.emit('my_event', {value: $('#emit_value').val()});
          return false; });
      $('#buttonVal').click(function(event) {
          console.log($('#buttonVal').val());
          socket.emit('click_event', {value: $('#buttonVal').val()});
          return false; });
      $('form#disconnect').submit(function(event) {
          socket.emit('disconnect_request');
          return false; });         
      });
    </script>
 </head>

 <body>
  <h1>Flask-SocketIO Test</h1>
  <h2>Send:</h2>
  <form id="emit" method="POST" action='#'>
    <input type="text" name="emit_value" id="emit_value" placeholder="Amplituda">
    <input type="submit" value="Send">
  </form>
  <button id="buttonVal" type="submit" value="send">Send value</button>
  <form id="disconnect" method="POST" action="#">
    <input type="submit" value="Disconnect">
  </form> 
  <button id="on">Start</button>
  <h2>Receive:</h2>
  <div id="log"></div>
  <div id="json_response"></div>
 </body>
</html>

<script type="text/javascript" charset="utf-8">
  const button = document.getElementById('on');
      let clickCount = 0;

      button.addEventListener('click', () => {
        clickCount++;
        if (clickCount % 2 !== 0){
          button.textContent = "Stop";
          startTime = Date.now();
          timePeriod = 0;
        }
        else {
          button.textContent = "Start";
          endTime = Date.now()
          timePeriod = (endTime-startTime)/1000;
        }
      });
    </script>