Ver Mensaje Individual
  #28 (permalink)  
Antiguo 19/09/2007, 15:49
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
Re: MiniChat con PHP/AJAX

index.php
Código PHP:
<?php
if($_GET["Bota"]=="bai")
 {
  
$file fopen("file.txt""a");
  
fwrite($file,date("Y/m/d - H:i:s")." ".$_REQUEST["text"]."\n");
  
fclose($file);
  exit();
 }
elseif(
$_GET["Leidu"]=="bai")
 {
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
$file fopen("file.txt""r");
  while (!
feof($file)) 
   {
    
$buffer fgets($file4096);
    echo 
$buffer."<br />";
   }
  
fclose($file);
  exit();
 }
?>
Código HTML:
<html>
<head>
<title>Txat-tsue :)</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">  
setInterval("fajax2()",5000);
</script>
</head>
<body>
<input type="text" id="text" size="50" maxlength="50" />
<input type="button" value="bota" onclick="fajax()" />
<div id="txat">
</div>
<script type="text/javascript">
    document.getElementById('text').value="";
    document.getElementById('text').focus();
 fajax2();
</script>
</body>
</html> 
y el javascript ajax.js
Código:
function ajaxFunction01()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      return xmlHttp;
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        return xmlHttp;
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
  } 
function ajaxFunction02()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      return xmlHttp;
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        return xmlHttp;
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
  } function fajax()
{
    var text = document.getElementById('text').value;
    var ajax=ajaxFunction01();    
    ajax.open("post","index.php?Bota=bai",true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("text="+text);
    document.getElementById('text').value="";
    document.getElementById('text').focus();
    fajax2();
}
function fajax2()
 {
    var ajax2=ajaxFunction02();    
    ajax2.onreadystatechange=function()
      {
      if(ajax2.readyState==4)
        {
        document.getElementById('txat').innerHTML=ajax2.responseText;
        }
      else
       {
        // si queremos mostrar algo mientras carga
        //document.getElementById('txat').innerHTML="kargaten...";
       }
      }
    ajax2.open("get","index.php?Leidu=bai",true);
    ajax2.send(null);    
 }
solo falta tener el file.txt con permisos de escritura.

-------------------------------------------

Bueno pues eso es todo.

Bueno para el siguiente paso propongo:

txat con bases de datos y que cargue solo cuando sea necesario.


Mi idea es tener un input hidden con el valor del ultimo mensaje hash-eado por ejemplo con:

id + (fecha+hora) y cada 5 segundos mirar el has del ultimo mensaje, si son iguales no cargar, si son diferentes se carga.

Creo que es viable, ahora es tarde y tengo trabajo pero mañana a ver si nos ponemos a ello, OK?
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan

Última edición por ZiTAL; 19/09/2007 a las 16:09