Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/08/2012, 04:57
edie8
 
Fecha de Ingreso: noviembre-2011
Mensajes: 516
Antigüedad: 13 años, 1 mes
Puntos: 10
Respuesta: Poner sonidos en chat

segunda parte del chat.js
Código Javascript:
Ver original
  1. function startChatSession(){  
  2.     $.ajax({
  3.       url: "chat.php?action=startchatsession",
  4.       cache: false,
  5.       dataType: "json",
  6.       success: function(data) {
  7.  
  8.         username = data.username;
  9.  
  10.         $.each(data.items, function(i,item){
  11.             if (item)   {
  12.                 chatboxtitle = item.f;
  13.  
  14.                 if ($("#chatbox_"+chatboxtitle).length <= 0) {
  15.                     createChatBox(chatboxtitle,1);
  16.                 }
  17.                
  18.                 if (item.s == 1) {
  19.                     item.f = username;
  20.                 }
  21.  
  22.                 if (item.s == 2) {
  23.                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  24.                 } else {
  25.                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
  26.                 }
  27.             }
  28.         });
  29.        
  30.         for (i=0;i<chatBoxes.length;i++) {
  31.             chatboxtitle = chatBoxes[i];
  32.             $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  33.             setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); }
  34.     setTimeout('chatHeartbeat();',chatHeartbeatTime);
  35.        
  36.     }});
  37. }
  38. jQuery.cookie = function(name, value, options) {
  39.     if (typeof value != 'undefined') {
  40.         options = options || {};
  41.         if (value === null) {
  42.             value = '';
  43.             options.expires = -1;
  44.         }
  45.         var expires = '';
  46.         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  47.             var date;
  48.             if (typeof options.expires == 'number') {
  49.                 date = new Date();
  50.                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  51.             } else {
  52.                 date = options.expires;
  53.             }
  54.             expires = '; expires=' + date.toUTCString();
  55.         }
  56.         var path = options.path ? '; path=' + (options.path) : '';
  57.         var domain = options.domain ? '; domain=' + (options.domain) : '';
  58.         var secure = options.secure ? '; secure' : '';
  59.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  60.     } else {
  61.         var cookieValue = null;
  62.         if (document.cookie && document.cookie != '') {
  63.             var cookies = document.cookie.split(';');
  64.             for (var i = 0; i < cookies.length; i++) {
  65.                 var cookie = jQuery.trim(cookies[i]);
  66.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
  67.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         return cookieValue;
  73.     }
  74. };
Y estos son codigos de php
chat.php
Código PHP:
Ver original
  1. define ('DBPATH','localhost');
  2. define ('DBUSER','root');
  3. define ('DBPASS','pass');
  4. define ('DBNAME','chat');
  5.  
  6. session_start();
  7.  
  8. global $dbh;
  9. $dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
  10. mysql_selectdb(DBNAME,$dbh);
  11.  
  12. if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
  13. if ($_GET['action'] == "sendchat") { sendChat(); }
  14. if ($_GET['action'] == "closechat") { closeChat(); }
  15. if ($_GET['action'] == "startchatsession") { startChatSession(); }
  16.  
  17. if (!isset($_SESSION['chatHistory'])) {
  18.     $_SESSION['chatHistory'] = array();
  19. }
  20.  
  21. if (!isset($_SESSION['openChatBoxes'])) {
  22.     $_SESSION['openChatBoxes'] = array();  
  23. }
  24.  
  25. function chatHeartbeat() {
  26.    
  27.     $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' AND recd = 0) order by id ASC";
  28.     $query = mysql_query($sql);
  29.     $items = '';
  30.  
  31.     $chatBoxes = array();
  32.  
  33.     while ($chat = mysql_fetch_array($query)) {
  34.  
  35.         if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
  36.             $items = $_SESSION['chatHistory'][$chat['from']];
  37.         }
  38.  
  39.         $chat['message'] = sanitize($chat['message']);
  40.  
  41.         $items .= <<<EOD
  42.                        {
  43.             "s": "0",
  44.             "f": "{$chat['from']}",
  45.             "m": "{$chat['message']}"
  46.        },
  47. EOD;
  48.  
  49.     if (!isset($_SESSION['chatHistory'][$chat['from']])) {
  50.         $_SESSION['chatHistory'][$chat['from']] = '';
  51.     }
  52.  
  53.     $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
  54.                            {
  55.             "s": "0",
  56.             "f": "{$chat['from']}",
  57.             "m": "{$chat['message']}"
  58.        },
  59. EOD;
  60.        
  61.         unset($_SESSION['tsChatBoxes'][$chat['from']]);
  62.         $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
  63.     }
  64.  
  65.     if (!empty($_SESSION['openChatBoxes'])) {
  66.     foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
  67.         if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
  68.             $now = time()-strtotime($time);
  69.             $time = date('g:iA M dS', strtotime($time));
  70.  
  71.             $message = "Sent at $time";
  72.             if ($now > 180) {
  73.                 $items .= <<<EOD
  74. {
  75. "s": "2",
  76. "f": "$chatbox",
  77. "m": "{$message}"
  78. },
  79. EOD;
  80.  
  81.     if (!isset($_SESSION['chatHistory'][$chatbox])) {
  82.         $_SESSION['chatHistory'][$chatbox] = '';
  83.     }
  84.  
  85.     $_SESSION['chatHistory'][$chatbox] .= <<<EOD
  86.         {
  87. "s": "2",
  88. "f": "$chatbox",
  89. "m": "{$message}"
  90. },
  91. EOD;
  92.             $_SESSION['tsChatBoxes'][$chatbox] = 1;
  93.         }
  94.         }
  95.     }
  96. }
  97.  
  98.     $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' and recd = 0";
  99.     $query = mysql_query($sql);
  100.  
  101.     if ($items != '') {
  102.         $items = substr($items, 0, -1);
  103.     }
  104. header('Content-type: application/json');
  105. ?>
  106. {
  107.         "items": [
  108.             <?php echo $items;?>
  109.         ]
  110. }
  111.  
  112. <?php
  113.             exit(0);
  114. }
  115.  
  116. function chatBoxSession($chatbox) {
  117.    
  118.     $items = '';
  119.    
  120.     if (isset($_SESSION['chatHistory'][$chatbox])) {
  121.         $items = $_SESSION['chatHistory'][$chatbox];
  122.     }
  123.  
  124.     return $items;
  125. }
  126.  
  127. function startChatSession() {
  128.     $items = '';
  129.     if (!empty($_SESSION['openChatBoxes'])) {
  130.         foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
  131.             $items .= chatBoxSession($chatbox);
  132.         }
  133.     }
  134.  
  135.  
  136.     if ($items != '') {
  137.         $items = substr($items, 0, -1);
  138.     }
  139.  
  140. header('Content-type: application/json');
  141. ?>
  142. {
  143.         "username": "<?php echo $_SESSION['logueado_nombre'];?>",
  144.         "items": [
  145.             <?php echo $items;?>
  146.         ]
  147. }
  148.  
  149. <?php
  150.  
  151.  
  152.     exit(0);
  153. }
  154.  
  155. function sendChat() {
  156.     $from = $_SESSION['logueado_nombre'];
  157.     $to = $_POST['to'];
  158.     $message = $_POST['message'];
  159.  
  160.     $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
  161.    
  162.     $messagesan = sanitize($message);
  163.  
  164.     if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
  165.         $_SESSION['chatHistory'][$_POST['to']] = '';
  166.     }
  167.  
  168.     $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
  169.                        {
  170.             "s": "1",
  171.             "f": "{$to}",
  172.             "m": "{$messagesan}"
  173.        },
  174. EOD;
  175.  
  176.  
  177.     unset($_SESSION['tsChatBoxes'][$_POST['to']]);
  178.  
  179.     $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
  180.     $query = mysql_query($sql);
  181.     echo "1";
  182.     exit(0);
  183. }
  184.  
  185. function closeChat() {
  186.  
  187.     unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  188.    
  189.     echo "1";
  190.     exit(0);
  191. }
  192.  
  193. function sanitize($text) {
  194.     $text = htmlspecialchars($text, ENT_QUOTES);
  195.     $text = str_replace("\n\r","\n",$text);
  196.     $text = str_replace("\r\n","\n",$text);
  197.     $text = str_replace("\n","<br>",$text);
  198.     return $text;
  199. }
Consulta_chat.php
Código PHP:
Ver original
  1. <?php
  2. $nombre =$_SESSION['logueado_nombre'];
  3. $id_usuario=$_SESSION['logueado_id'];  
  4.  
  5.  
  6. include("db.php"); ?>
  7. <?php
  8.  
  9. $conexion=mysql_connect("localhost","root","pass")or
  10.   die("Problemas en la conexion");
  11. mysql_select_db("chat",$conexion) or
  12.   die("Problemas en la selección de la base de datos");
  13. $registros=mysql_query("select * from usuarios where id<>'$id_usuario' and online='1'",$conexion) or
  14.   die("Problemas en el select:".mysql_error());
  15.   $online=mysql_query("select * from usuarios where id='$id_usuario'",$conexion);
  16.   $onl=mysql_fetch_array($online);
  17.   if($onl['online']=='1'){
  18.   while ($reg=mysql_fetch_array($registros))
  19.   {
  20.   echo "<a href=\"javascript:void(0)\" onClick=\"chatWith('$reg[nombre]');\" ><img src=\"$reg[imagen]\"style=\"width:30px; border:none;\"></img>$reg[nombre]</a>";
  21.   echo "<br><br>";}
  22.        
  23.   }
  24. ?>
minichat.php
Código PHP:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd" >
  2.  
  3. <html>
  4. <head>
  5. <title>Sample Chat Application</title>
  6. <style>
  7. body {
  8.     padding:0;
  9.     margin:0 auto;
  10.     font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
  11.     font-size:11px;
  12. }
  13. /*a:visited { color:#33FF33} */
  14. img:visited{color:#660000}
  15. </style>
  16.  
  17. <link type="text/css" rel="stylesheet" media="all" href="css/chat.css" />
  18. <link type="text/css" rel="stylesheet" media="all" href="css/screen.css" />
  19.  
  20. <!--[if lte IE 7]>
  21. <link type="text/css" rel="stylesheet" media="all" href="css/screen_ie.css" />
  22. <![endif]-->
  23.  
  24. </head>
  25. <body>
  26. <div id="formulario" style="float:right; margin-top:-80px; margin-right:30px;" ><?php include('consulta_id.php'); ?></div>
  27. <div id="cajon_chat" style=" border:double;  border:#000000; float:right; width:130px; margin-top::10px;">
  28.  
  29. <div id="main_container" style="overflow:auto;">
  30.  
  31. <!-- YOUR BODY HERE -->
  32. </div>
  33. </div>
  34.  
  35. <script type="text/javascript" src="js/jquery.js"></script>
  36. <script type="text/javascript" src="js/chat.js"></script>
  37. <script type="text/javascript" src="js/funciones_chat.js"></script>
  38. <script type="text/javascript" src="ajax1.js"></script>
  39. </body>
  40. </html>