Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/02/2011, 10:58
kozenko
 
Fecha de Ingreso: octubre-2010
Mensajes: 83
Antigüedad: 13 años, 6 meses
Puntos: 1
[PHP] Webchat

Estoy desarrollando un webchat puramente en php.
Logro conectarme, pero tengo un par de problemas:
No logro que la informacion (lo que habla cada uno) aparezca al final, solo logro que aparezca cuando termina el script.
No se como hacer la "cajita" para que uno hable, se hacer formularios y todo, pero tendria que empezar denuevo el script cada vez que le doy "enviar"
Desde ya muchas gracias,
Koz

PD: Si es posible todo en php mejor, pero si tengo que agregar otro lenguaje lo agregare sin problemas

EDIT:
el codigo:
Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4. ini_set('display_errors', 'on');
  5.  
  6. /* --- Varibles and Config Info --- */
  7. $config = array(
  8.         'server' => 'irc.toribash.com',
  9.         'port'   => 6667,
  10.         'name'   => 'Kozbot',
  11.         'nick'   => 'Kozzy',
  12.        
  13.        
  14.         'channel' => '#koz',   
  15.         'logging' => false,
  16.         'warning' => false,
  17. );
  18. // mysql
  19.                 $link = mysql_connect('localhost', 'root');
  20. if (!$link) {
  21.     die('Not connected : ' . mysql_error());
  22. }
  23.  
  24. $db_selected = mysql_select_db('kozzy', $link);
  25. if (!$db_selected) {
  26.     die ('Can\'t use Kozzy : ' . mysql_error());
  27. }
  28.  
  29.  
  30. /* --- IRCBot Class --- */
  31.  
  32. class IRCBot {
  33.  
  34.         //This is going to hold our TCP/IP connection
  35.         var $socket;
  36.  
  37.         //This is going to hold all of the messages both server and client
  38.         var $ex = array();    
  39.         //var $logging = true;
  40.  
  41.         /*
  42.          Construct item, opens the server connection, logs the bot in
  43.          @param array
  44.         */
  45.  
  46.         function __construct($config)
  47.         {
  48.                 $this->socket = fsockopen($config['server'], $config['port']);
  49.                 $this->login($config);
  50.                 $this->main($config);
  51.         }
  52.  
  53.         /*
  54.          Logs the bot in on the server
  55.          @param array
  56.         */
  57.  
  58.         function login($config)
  59.         {
  60.                 $this->send_data('USER', $config['nick'].' KozEnko '.$config['nick'].' :'.$config['name']);
  61.                 $this->send_data('NICK', $config['nick']);
  62.                 $this->join_channel($config['channel']);
  63.                  if($config['logging']) {
  64.                         $date = date("n-j-y");
  65.                         $time = date('h:i:s A');
  66.                         $logfile = fopen("$date-log.html","a");
  67.                         fwrite($logfile,"<br/>**************** Logging Started at $time ****************<br/>");
  68.                         fclose($logfile);
  69.                        
  70.                         //Warn that logging has been enabled
  71.                          if($config['warning']) {
  72.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Enabled]");
  73.                         }
  74.                  }
  75.         }
  76.  
  77.         /*
  78.          This is the workhorse function, grabs the data from the server and displays on the browser
  79.         */
  80.  
  81. function main($config)
  82.         {            
  83.                 $data = fgets($this->socket, 256);
  84.                
  85.                 echo nl2br($data);
  86.                
  87.                 flush();
  88.  
  89.                 $this->ex = explode(' ', $data);
  90.                 $mensaje_base = strstr($data, ':');
  91.                 $mensaje_basee = substr($mensaje_base, 1);
  92.                 $mensajee = strstr($mensaje_basee, ':');
  93.                 $mensaje = substr($mensajee, 1);
  94.                                
  95.                 if($this->ex[0] == 'PING')
  96.                 {
  97.                         $this->send_data('PONG', $this->ex[0]); //Plays ping-pong with the server to stay connected.
  98.                 }
  99.                
  100.                  //Logs the chat
  101.                 if($config['logging'])
  102.                 {
  103.                     $logtxt = $this->filter_log($this->ex[1], $this->ex[2], $this->ex[0], $this->get_msg($this->ex)); //Gets human readable text from irc data
  104.                     if($logtxt != null) { //Writes to log if it is a message
  105.                         $date = date("n-j-y");
  106.                         $logfile = fopen("$date-log.html","a");
  107.                         fwrite($logfile,"$logtxt<br />");
  108.                         fclose($logfile);  
  109.                     }
  110.                 }
  111.  
  112.         $this->main($config);
  113.     }
  114.  
  115.     /* --- IRCBot Class's Functions --- */
  116.     function filter_log($type, $chan, $nick, $msg) {
  117.         $nick = ltrim($nick, ":");
  118.         $nick = substr($nick, 0, strpos($nick, "!"));
  119.         $msg = ltrim($msg, ":");
  120.  
  121.         if($type == "PRIVMSG") {
  122.             return date("[H:i]")." &lt;".$nick."&gt; ".$msg;
  123.         }
  124.         return null     ;
  125.     }
  126.  
  127.     function get_msg($n) {
  128.         $message = "";
  129.         for($i=$n; $i <= (count($this->ex)); $i++) { $message .= $this->ex[$i]." "; }
  130.         return $message;
  131.     }
  132.  
  133.     function send_data($cmd, $msg = null) {
  134.         if($msg != null) {
  135.             fputs($this->socket, $cmd.' '.$msg."\r\n");
  136.             echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
  137.         } else {
  138.             fputs($this->socket, $cmd."\r\n");
  139.             echo '<strong>'.$cmd.'</strong><br />';
  140.         }
  141.     }l
  142.  
  143. }
  144. $bot = new IRCBot($config);
  145. ?>

Última edición por kozenko; 09/02/2011 a las 11:13