2017-12-30 10 views
-1

누군가 새로운 메시지를받을 때마다 소리를 내고 싶습니다. 지금 나는 chatbox을 그리고 난 그것이 chatbox (누군가가 DB에 chatbox 테이블에 삽입 기본적으로 할 때)마다 새로운 메시지가 도착새 메시지 도착시 소리 내기

<script type="text/javascript"> 
    function ajax(){ 
     var req = new XMLHttpRequest(); 

     req.onreadystatechange = function(){ 
      if (req.readyState == 4 && req.status == 200) { 
       document.getElementById('chat').innerHTML = req.responseText; 
      } 
     } 

     req.open('GET', 'divisiones/chat.php', true); 
     req.send(); 
    } 

    //linea que hace que se refreseque la pagina cada segundo 
    setInterval(function(){ajax();}, 1000); 
</script> 
<div class="content-wrapper" onload="ajax();"> 
    <section class="content"> 
        <div class="row"> 
      <div class="col-md-12"> 
       <div class="box box-primary" style="background-color: #fffbf8;"> 
        <div style="background: repeating-linear-gradient(-45deg, white, white 1px, #fffbf8 1px, #fffbf8 8px);"> 
         <div class="wrap"> 
          <div class="row"> 
           <div id="caja-chat"> 
            <div id="chat"></div> 
           </div> 

           <form method="POST" action="division_chat.php?auth=<?php echo $salt_key_check; ?>"> 
            <input type="text" name="nombre" placeholder="Ingresa tu nombre"> 
            <textarea name="mensaje" placeholder="Ingresa tu mensaje"></textarea> 
            <input type="submit" name="enviar" value="Enviar"> 
           </form> 

           <?php 
            if (isset($_POST['enviar'])) { 

             $nombre = $_POST['nombre']; 
             $mensaje = $_POST['mensaje']; 


             $consulta = "INSERT INTO division_chat VALUES (NULL,'$session_login_usr', '$mensaje', '$fptime', '$log_ip', '$divisionpd')"; 

             $ejecutar = $con_db->query($consulta); 

             if ($ejecutar) { 
              echo "<embed loop='false' src='beep.mp3' hidden='true' autoplay='true'>"; 
             } 
            } 

           ?> 
          </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</section> 
</div> 
+0

https://www.html5rocks.com/en을/tutorials/webaudio/intro /. HTML5 Web Audio API로 시작하는 것이 좋습니다. JavaScript를 사용하여 오디오 파일을로드하고 재생/일시 중지 할 수 있습니다. – DaZa

+0

[SQL Injections] (http://php.net/manual/en/security.database.sql-injection.php)에 대한 정보가 많이 있으며 [Prepared Statements] (http://php.net/)를 사용해야합니다. manual/en/mysqli.quickstart.prepared-statements.php)를 사용하십시오. 특히 사용자 입력을 전혀 피할 수 없기 때문에 특별히 그렇습니다! –

+0

@DaZa 감사합니다 !!!! 그래서 chatbox가 새로운 메시지를받을 때마다 소리를 얻으려면 어떻게해야할까요? –

답변

0

을보고있는 사람들을 위해 소리를 재생하고 싶습니다 새 메시지가 해당 값과 동일한 지 확인할 수 있도록 LAST req.responseText 값을 어딘가에 저장해야합니다.

req.responseText 값을 저장하기 위해 나는 HTML5 localStorage을 사용할 것입니다.

그리고 당신은 같은 것을 할 수 있습니다

var LastMessage = localstorage.getItem('last_message'); 

if(req.responseText != LastMessage){ 

/////Play your sound here///// 
$('#audio').play(); 

} 

를이 페이지에있는 곳이 있는지 확인합니다 : 내가 읽어 보시기 바랍니다

<audio id="audio" src="audio.mp3"></audio> 
관련 문제