Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当有新帖子时,如何让聊天脚本播放声音?_Php_Javascript_Audio_Chat - Fatal编程技术网

Php 当有新帖子时,如何让聊天脚本播放声音?

Php 当有新帖子时,如何让聊天脚本播放声音?,php,javascript,audio,chat,Php,Javascript,Audio,Chat,聊天页面JavaScript: 当有新帖子时,如何让聊天脚本播放声音 以下是我正在使用的3个脚本: <script type="text/javascript"><!-- function showmessages(){ if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","show-messages.php?" + Math.random(),

聊天页面JavaScript: 当有新帖子时,如何让聊天脚本播放声音

以下是我正在使用的3个脚本:

<script type="text/javascript"><!--
function showmessages(){
   if(window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET","show-messages.php?" + Math.random(),false);
      xmlhttp.send(null);
   }
   else{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open("GET","showmessages.php?" + Math.random(),false);
      xmlhttp.send();
   }
   document.getElementById('messages').innerHTML = xmlhttp.responseText;
   setTimeout('showmessages()',5000);
}

showmessages();
function send(){
   var sendto = 'send.php?message=' + document.getElementById('message').value + '&name=' + document.getElementById('name').value;
   if(window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET",sendto,false);
      xmlhttp.send(null);
   }
   else{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open("GET",sendto,false);
      xmlhttp.send();
   }
   var error = '';
   switch(parseInt(xmlhttp.responseText)){
   case 1:
      error = '<span class="pink">The database is down!</span>';
      break;
   case 2:
      error = '<span class="pink">The database is down!</span>';
      break;
   case 3:
      error = '<span class="pink">Don`t forget the message!</span>';
      break;
   case 4:
      error = '<span class="pink">The message is too long!</span>';
      break;
   case 5:
      error = '<span class="pink">Don`t forget the name!</span>';
      break;
   case 6:
      error = '<span class="pink">The name is too long!</span>';
      break;
   case 7:
      error = '<span class="pink">This name is already used by somebody else!</span>';
      break;
   case 8:
      error = '<span class="pink">The database is down!</span>';
   }
   if(error == ''){
      document.getElementById('error').innerHTML = '';
      showmessages();
   }
   else{
      document.getElementById('error').innerHTML = error;
   }
}
// --></script>



Your Name: <input type="text" id="name" value="" /><br />
Enter Your Message:<br /><textarea id="message" cols="20" rows="2"></textarea><br /> 
<input type="button" value="Send" onClick="send();document.getElementById('message').value = '';"/>

<div id="messages"></div>
<div id="error"></div>

您的姓名:
输入您的消息:

show-messages.php

<?php
mysql_connect('*******','*******','*******1');
mysql_select_db('chat') or die(2);
$random = microtime(true);

echo '<p><a href="?'.$random.'">Refresh Chat</a></p>
';
$result = mysql_query("select * from chat order by time desc limit 0,10");
$messages = array();
$i = 0;
while($row = mysql_fetch_array($result)){
$messages[$i] = '<p class="purple"><span class="pink"><strong>'. $row[name] . '</strong></span> said:<br /><span class="purple">' . $row[message] . '</span><br /> <span class="pink">';

$timeago = time() - $row['time'];
if($timeago > 86400){
$messages[$i] .= date('g:i A M, d Y',$row[time]);
}
else{
$hours = floor($timeago / 3600); 
$minutes = floor(($timeago - ($hours * 3600)) / 60);
$seconds = floor($timeago - ($hours * 3600) - ($minutes * 60));
if($hours > 0){
if($minutes > 9){
if($seconds > 9){
$messages[$i] .= $hours . ' h, ' . $minutes . ' min, ' . $seconds . ' sec';
}
else{
$messages[$i] .= $hours . ' h, ' . $minutes . ' min, 0' . $seconds . ' sec';
}
}
else{
if($seconds > 9){
$messages[$i] .= $hours . ' h, 0' . $minutes . ' min, ' . $seconds . ' sec';
}
else{
$messages[$i] .= $hours . ' h, 0' . $minutes . ' min, 0' . $seconds . ' sec';
}
}
}
else if($minutes > 0){
if($seconds > 9){
$messages[$i] .= $minutes . ' min, ' . $seconds . ' sec';
}
else{
$messages[$i] .= $minutes . ' min, 0' . $seconds . ' sec';
}
}
else{
$messages[$i] .= $seconds . ' sec';
}
$messages[$i] .= ' ago';
}
$messages[$i] .= "</span></p><p class='purple'>----------------------------</p>";
$old = $row[time];
$i++;
}
for($i=0;$i<=9;$i++){
echo $messages[$i];
}
mysql_query("delete from chat where time < " . $old);
$random = microtime(true);
echo '<p><a href="?'.$random.'">Refresh Chat</a></p>';
?>

send.php

<?php
//Connect to MySQL
mysql_connect('******', '******', '******') or die (1);
//Select database
mysql_select_db('chat') or die (2);
$message = $_GET['message']; 
$name = $_GET['name'];
//Check if message is empty and send the error code
if(strlen($message) < 1){
echo 3;
}
//Check if message is too long
else if(strlen($message) > 255){
   echo 4;
}
//Check if name is empty
else if(strlen($name) < 1){
   echo 5;
}
//Check if name is too long
else if(strlen($name) > 29){
   echo 6;
}
//Check if the name is used by somebody else
else if(mysql_num_rows(mysql_query("select * from chat where name = '" . $name . "' and ip != '" . $_SERVER['REMOTE_ADDR'] . "'")) != 0){
   echo 7;
}
//If everything is fine
else{
   //This array contains the characters what will be removed from the message and name, because else somebody could send redirection script or links
   $search = array("<",">","&gt;","&lt;");
   //Insert a new row in the chat table
   mysql_query("insert into chat values ('" . time() . "', '" . str_replace($search,"",$name) . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . str_replace($search,"",$message) . "')") or die(8);
}
?>

因为函数showmessages()是通过AJAX调用show-messages.php的函数,所以回调选项会很好。你考虑过使用吗?我一直在用它。它为不同类型的结果提供了令人难以置信的处理程序(我最喜欢的是JSON返回),并且在函数完成后将运行回调函数,您可以进行设置

在回调时,如果你能触发一个声音来播放,那就太完美了。不过,我不熟悉在网站上播放音乐(除了那种将.wav文件嵌入在后台播放的HTML中的老派方法,但这是Web1.0,而不是2.0)

下面是一个我发现可以播放存储在服务器上的特定噪音的示例:

利用这两个,我相信你可以很快完成

编辑:

function showmessages(){
    $.get("show-messages.php", { random: Math.random()},
   function(data){
        $('#messages').html(data); // put teh return data here
        // call back the function to play sound based on the jQuery plugin. 
   });
}
以上是可以设置的更简单的AJAX查询,而不是当前的showmessage()函数

此外,如果您希望得到一个更复杂的结果,该结果使用jSON包含多个数组(将其视为PHP数组,javascript对象形式除外)。还要注意PHP中的json_encode()函数,它从任何PHP数组中创建一个json对象(甚至是多级的,我认为不包括PHP对象)


那么,在你发布的大约100行代码中,你能至少告诉我们哪些代码行触发了一篇新文章吗?你试过谷歌吗@mrtsherman
因为我还是一个新手,麻烦你发布一些示例代码好吗?我编辑了答案,加入了一些示例。然而,详细的研究值得你自己去做——相信我。学习这个框架是我职业生涯中最好的事情之一。jQuery中的AJAX工具非常棒。@jeffkee我甚至无法让演示程序开始工作。还有其他想法吗?您确认您的AJAX和回调功能正常吗?检查数据库等,确保这些元素正常工作。现在,如果新插件已安装但无法工作(检查Firebug是否存在任何问题-可能插件路径错误..通常也应该在jQuery库之后调用插件),那么只需尝试不同的插件即可。可能插件更新不够,无法与最新版本的jQuery一起使用。
function showmessages(){
    $.get("show-messages.php", { random: Math.random()},
   function(data){
       if(data.code==1) {
           // assuming 1 means successful
            $('#messages').html(data.content); // put teh return data here
        // call back the function to play sound based on the jQuery plugin.
       } else {
           // execute code that runs IF the result was no good
       }
   },'json');
}