segunda parte del chat.js
   
Código Javascript
:
Ver original- function startChatSession(){   
-     $.ajax({ 
-       url: "chat.php?action=startchatsession", 
-       cache: false, 
-       dataType: "json", 
-       success: function(data) { 
-   
-         username = data.username; 
-   
-         $.each(data.items, function(i,item){ 
-             if (item)   {  
-                 chatboxtitle = item.f; 
-   
-                 if ($("#chatbox_"+chatboxtitle).length <= 0) { 
-                     createChatBox(chatboxtitle,1); 
-                 } 
-                  
-                 if (item.s == 1) { 
-                     item.f = username; 
-                 } 
-   
-                 if (item.s == 2) { 
-                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>'); 
-                 } else { 
-                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':  </span><span class="chatboxmessagecontent">'+item.m+'</span></div>'); 
-                 } 
-             } 
-         }); 
-          
-         for (i=0;i<chatBoxes.length;i++) { 
-             chatboxtitle = chatBoxes[i]; 
-             $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight); 
-             setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); }  
-     setTimeout('chatHeartbeat();',chatHeartbeatTime); 
-          
-     }}); 
- } 
- jQuery.cookie = function(name, value, options) { 
-     if (typeof value != 'undefined') { 
-         options = options || {}; 
-         if (value === null) { 
-             value = ''; 
-             options.expires = -1; 
-         } 
-         var expires = ''; 
-         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
-             var date; 
-             if (typeof options.expires == 'number') { 
-                 date = new Date(); 
-                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 
-             } else { 
-                 date = options.expires; 
-             } 
-             expires = '; expires=' + date.toUTCString(); 
-         } 
-         var path = options.path ? '; path=' + (options.path) : ''; 
-         var domain = options.domain ? '; domain=' + (options.domain) : ''; 
-         var secure = options.secure ? '; secure' : ''; 
-         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
-     } else {  
-         var cookieValue = null; 
-         if (document.cookie && document.cookie != '') { 
-             var cookies = document.cookie.split(';'); 
-             for (var i = 0; i < cookies.length; i++) { 
-                 var cookie = jQuery.trim(cookies[i]); 
-                 if (cookie.substring(0, name.length + 1) == (name + '=')) { 
-                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 
-                     break; 
-                 } 
-             } 
-         } 
-         return cookieValue; 
-     } 
- }; 
Y estos son codigos de php
chat.php   
Código PHP:
Ver original- define ('DBPATH','localhost'); 
- define ('DBUSER','root'); 
- define ('DBPASS','pass'); 
- define ('DBNAME','chat'); 
-   
- session_start(); 
-   
- global $dbh; 
- $dbh = mysql_connect(DBPATH,DBUSER,DBPASS); 
- mysql_selectdb(DBNAME,$dbh); 
-   
- if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }  
- if ($_GET['action'] == "sendchat") { sendChat(); }  
- if ($_GET['action'] == "closechat") { closeChat(); }  
- if ($_GET['action'] == "startchatsession") { startChatSession(); }  
-   
- if (!isset($_SESSION['chatHistory'])) { 
-     $_SESSION['chatHistory'] = array();  
- } 
-   
- if (!isset($_SESSION['openChatBoxes'])) { 
-     $_SESSION['openChatBoxes'] = array();    
- } 
-   
- function chatHeartbeat() { 
-      
-     $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' AND recd = 0) order by id ASC"; 
-     $query = mysql_query($sql); 
-     $items = ''; 
-   
-     $chatBoxes = array(); 
-   
-     while ($chat = mysql_fetch_array($query)) { 
-   
-         if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) { 
-             $items = $_SESSION['chatHistory'][$chat['from']]; 
-         } 
-   
-         $chat['message'] = sanitize($chat['message']); 
-   
-         $items .= <<<EOD 
-                        { 
-             "s": "0", 
-             "f": "{$chat['from']}", 
-             "m": "{$chat['message']}" 
-        }, 
- EOD; 
-   
-     if (!isset($_SESSION['chatHistory'][$chat['from']])) { 
-         $_SESSION['chatHistory'][$chat['from']] = ''; 
-     } 
-   
-     $_SESSION['chatHistory'][$chat['from']] .= <<<EOD 
-                            { 
-             "s": "0", 
-             "f": "{$chat['from']}", 
-             "m": "{$chat['message']}" 
-        }, 
- EOD; 
-          
-         unset($_SESSION['tsChatBoxes'][$chat['from']]); 
-         $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent']; 
-     } 
-   
-     if (!empty($_SESSION['openChatBoxes'])) { 
-     foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { 
-         if (!isset($_SESSION['tsChatBoxes'][$chatbox])) { 
-             $now = time()-strtotime($time); 
-             $time = date('g:iA M dS', strtotime($time)); 
-   
-             $message = "Sent at $time"; 
-             if ($now > 180) { 
-                 $items .= <<<EOD 
- { 
- "s": "2", 
- "f": "$chatbox", 
- "m": "{$message}" 
- }, 
- EOD; 
-   
-     if (!isset($_SESSION['chatHistory'][$chatbox])) { 
-         $_SESSION['chatHistory'][$chatbox] = ''; 
-     } 
-   
-     $_SESSION['chatHistory'][$chatbox] .= <<<EOD 
-         { 
- "s": "2", 
- "f": "$chatbox", 
- "m": "{$message}" 
- }, 
- EOD; 
-             $_SESSION['tsChatBoxes'][$chatbox] = 1; 
-         } 
-         } 
-     } 
- } 
-   
-     $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['logueado_nombre'])."' and recd = 0"; 
-     $query = mysql_query($sql); 
-   
-     if ($items != '') { 
-         $items = substr($items, 0, -1); 
-     } 
- header('Content-type: application/json'); 
- ?> 
- { 
-         "items": [ 
-             <?php echo $items;?> 
-         ] 
- } 
-   
- <?php 
- } 
-   
- function chatBoxSession($chatbox) { 
-      
-     $items = ''; 
-      
-     if (isset($_SESSION['chatHistory'][$chatbox])) { 
-         $items = $_SESSION['chatHistory'][$chatbox]; 
-     } 
-   
-     return $items; 
- } 
-   
- function startChatSession() { 
-     $items = ''; 
-     if (!empty($_SESSION['openChatBoxes'])) { 
-         foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) { 
-             $items .= chatBoxSession($chatbox); 
-         } 
-     } 
-   
-   
-     if ($items != '') { 
-         $items = substr($items, 0, -1); 
-     } 
-   
- header('Content-type: application/json'); 
- ?> 
- { 
-         "username": "<?php echo $_SESSION['logueado_nombre'];?>", 
-         "items": [ 
-             <?php echo $items;?> 
-         ] 
- } 
-   
- <?php 
-   
-   
- } 
-   
- function sendChat() { 
-     $from = $_SESSION['logueado_nombre']; 
-     $to = $_POST['to']; 
-     $message = $_POST['message']; 
-   
-     $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time()); 
-      
-     $messagesan = sanitize($message); 
-   
-     if (!isset($_SESSION['chatHistory'][$_POST['to']])) { 
-         $_SESSION['chatHistory'][$_POST['to']] = ''; 
-     } 
-   
-     $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD 
-                        { 
-             "s": "1", 
-             "f": "{$to}", 
-             "m": "{$messagesan}" 
-        }, 
- EOD; 
-   
-   
-     unset($_SESSION['tsChatBoxes'][$_POST['to']]); 
-   
-     echo "1"; 
- } 
-   
- function closeChat() { 
-   
-     unset($_SESSION['openChatBoxes'][$_POST['chatbox']]); 
-      
-     echo "1"; 
- } 
-   
- function sanitize($text) { 
-     return $text; 
- } 
Consulta_chat.php   
Código PHP:
Ver original- <?php  
- $nombre =$_SESSION['logueado_nombre'];  
- $id_usuario=$_SESSION['logueado_id'];   
-   
-   
- include("db.php"); ?>  
- <?php 
-   
-   die("Problemas en la conexion"); 
-   die("Problemas en la selección de la base de datos"); 
- $registros=mysql_query("select * from usuarios where id<>'$id_usuario' and online='1'",$conexion)-  or 
 
-   $online=mysql_query("select * from usuarios where id='$id_usuario'",$conexion); 
-   if($onl['online']=='1'){ 
-   { 
-   echo "<a href=\"javascript:void(0)\" onClick=\"chatWith('$reg[nombre]');\" ><img src=\"$reg[imagen]\"style=\"width:30px; border:none;\"></img>$reg[nombre]</a>"; 
-   echo "<br><br>";} 
-          
-   } 
- ?> 
minichat.php   
Código PHP:
Ver original- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd" > 
-   
- <html> 
- <head> 
- <title>Sample Chat Application</title> 
- <style> 
- body { 
-     padding:0; 
-     margin:0 auto; 
-     font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; 
-     font-size:11px; 
- } 
- /*a:visited { color:#33FF33} */ 
- img:visited{color:#660000} 
- </style> 
-   
- <link type="text/css" rel="stylesheet" media="all" href="css/chat.css" /> 
- <link type="text/css" rel="stylesheet" media="all" href="css/screen.css" /> 
-   
- <!--[if lte IE 7]> 
- <link type="text/css" rel="stylesheet" media="all" href="css/screen_ie.css" /> 
- <![endif]--> 
-   
- </head> 
- <body> 
- <div id="formulario" style="float:right; margin-top:-80px; margin-right:30px;" ><?php include('consulta_id.php'); ?></div> 
- <div id="cajon_chat" style=" border:double;  border:#000000; float:right; width:130px; margin-top::10px;"> 
-   
- <div id="main_container" style="overflow:auto;"> 
-   
- <!-- YOUR BODY HERE --> 
- </div> 
- </div> 
-   
- <script type="text/javascript" src="js/jquery.js"></script> 
- <script type="text/javascript" src="js/chat.js"></script> 
- <script type="text/javascript" src="js/funciones_chat.js"></script> 
- <script type="text/javascript" src="ajax1.js"></script> 
- </body> 
- </html>