Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/08/2012, 06:04
edie8
 
Fecha de Ingreso: noviembre-2011
Mensajes: 516
Antigüedad: 12 años, 5 meses
Puntos: 10
Respuesta: problemas con un chat

e conseguido lo que estaba buscando pero tengo un problemilla al hablar a algien a ese algien le sale una caja de chat y de nombre pone indefinido, nose que error tengo el codigo es este:
Código Javascript:
Ver original
  1. var windowFocus = true;
  2. var username;
  3. var chatHeartbeatCount = 0;
  4. var minChatHeartbeat = 1000;
  5. var maxChatHeartbeat = 33000;
  6. var chatHeartbeatTime = minChatHeartbeat;
  7. var originalTitle;
  8. var blinkOrder = 0;
  9.  
  10. var chatboxFocus = new Array();
  11. var newMessages = new Array();
  12. var newMessagesWin = new Array();
  13. var chatBoxes = new Array();
  14.  
  15. $(document).ready(function(){
  16.     originalTitle = document.title;
  17.     startChatSession();
  18.  
  19.     $([window, document]).blur(function(){
  20.         windowFocus = false;
  21.     }).focus(function(){
  22.         windowFocus = true;
  23.         document.title = originalTitle;
  24.     });
  25. });
  26.  
  27.  
  28.  var RequestObject = false;
  29.    //directorio donde tenemos el archivo ajax.php
  30.   var Archivo = 'consulta_chat.php';
  31.  
  32.   // el tiempo X que tardará en actualizarse
  33.   window.setInterval("actualizacion_reloj()", 1000);
  34.  
  35.   if (window.XMLHttpRequest) RequestObject = new XMLHttpRequest();
  36.   if (window.ActiveXObject) RequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  37.  
  38.   function ReqChange() {
  39.   // Si se ha recibido la información correctamente
  40.     if (RequestObject.readyState==4) {
  41.      // si la información es válida
  42.      if (RequestObject.responseText.indexOf('invalid') == -1) {
  43.      // Buscamos la div con id online
  44.        document.getElementById("cajon_chat").innerHTML = RequestObject.responseText;
  45.      } else {
  46.       // Por si hay algun error document.getElementById("online").innerHTML = "Error llamando";
  47.      }
  48.     }
  49.   }
  50.  
  51.   function llamadaAjax() {
  52.         // Mensaje a mostrar mientras se obtiene la información remota...
  53.     var previousContent = '';
  54.     // Preparamos la obtención de datos
  55.     RequestObject.open("GET", Archivo+"?"+Math.random() , true);
  56.     RequestObject.onreadystatechange = ReqChange;
  57.     // Enviamos
  58.     RequestObject.send(null);
  59.   }
  60.  
  61.   function actualizacion_reloj() {
  62.    llamadaAjax();
  63.  }
  64.  
  65.  
  66.  
  67. function restructureChatBoxes() {
  68.     align = 0;
  69.     for (x in chatBoxes) {
  70.         chatboxtitle = chatBoxes[x];
  71.  
  72.         if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
  73.             if (align == 0) {
  74.                 $("#chatbox_"+chatboxtitle).css('right', '20px');
  75.             } else {
  76.                 width = (align)*(225+7)+20;
  77.                 $("#chatbox_"+chatboxtitle).css('right', width+'px');
  78.             }
  79.             align++;
  80.         }
  81.     }
  82. }
  83.  
  84. function chatWith(chatuser, chatid) {
  85.     createChatBox(chatid, chatuser);
  86.     $("#chatbox_"+chatid+" .chatboxtextarea").focus();
  87. }
  88.  
  89. function createChatBox(chatid, chatboxtitle,minimizeChatBox) {
  90.     if ($("#chatbox_"+chatboxtitle).length > 0) {
  91.         if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
  92.             $("#chatbox_"+chatboxtitle).css('display','block');
  93.             restructureChatBoxes();
  94.         }
  95.         $("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
  96.         return;
  97.     }
  98.  
  99.     $(" <div />" ).attr("id","chatbox_"+chatboxtitle)
  100.     .addClass("chatbox")
  101.     .html('<div class="chatboxhead"><div class="chatboxtitle">'+chatboxtitle+'</div><div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">-</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\','+chatid+');"></textarea></div>')
  102.     .appendTo($( "body" ));
  103.                
  104.     $("#chatbox_"+chatboxtitle).css('bottom', '0px');
  105.    
  106.     chatBoxeslength = 0;
  107.  
  108.     for (x in chatBoxes) {
  109.         if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
  110.             chatBoxeslength++;
  111.         }
  112.     }
  113.  
  114.     if (chatBoxeslength == 0) {
  115.         $("#chatbox_"+chatboxtitle).css('right', '20px');
  116.     } else {
  117.         width = (chatBoxeslength)*(225+7)+20;
  118.         $("#chatbox_"+chatboxtitle).css('right', width+'px');
  119.     }
  120.    
  121.     chatBoxes.push(chatboxtitle);
  122.  
  123.     if (minimizeChatBox == 1) {
  124.         minimizedChatBoxes = new Array();
  125.  
  126.         if ($.cookie('chatbox_minimized')) {
  127.             minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
  128.         }
  129.         minimize = 0;
  130.         for (j=0;j<minimizedChatBoxes.length;j++) {
  131.             if (minimizedChatBoxes[j] == chatboxtitle) {
  132.                 minimize = 1;
  133.             }
  134.         }
  135.  
  136.         if (minimize == 1) {
  137.             $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
  138.             $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
  139.         }
  140.     }
  141.  
  142.     chatboxFocus[chatboxtitle] = false;
  143.  
  144.     $("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
  145.         chatboxFocus[chatboxtitle] = false;
  146.         $("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
  147.     }).focus(function(){
  148.         chatboxFocus[chatboxtitle] = true;
  149.         newMessages[chatboxtitle] = false;
  150.         $('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
  151.         $("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
  152.     });
  153.  
  154.     $("#chatbox_"+chatboxtitle).click(function() {
  155.         if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
  156.             $("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
  157.         }
  158.     });
  159.  
  160.     $("#chatbox_"+chatboxtitle).show();
  161. }
  162.  
  163.  
  164. function chatHeartbeat(){
  165.  
  166.     var itemsfound = 0;
  167.    
  168.     if (windowFocus == false) {
  169.  
  170.         var blinkNumber = 0;
  171.         var titleChanged = 0;
  172.         for (x in newMessagesWin) {
  173.             if (newMessagesWin[x] == true) {
  174.                 ++blinkNumber;
  175.                 if (blinkNumber >= blinkOrder) {
  176.                     document.title = x+' dice...';/* AKI ES LO DE ARRIBA DEL TITULO*/
  177.                     titleChanged = 1;
  178.                     break; 
  179.                 }
  180.             }
  181.         }
  182.        
  183.         if (titleChanged == 0) {
  184.             document.title = originalTitle;
  185.             blinkOrder = 0;
  186.         } else {
  187.             ++blinkOrder;
  188.         }
  189.  
  190.     } else {
  191.         for (x in newMessagesWin) {
  192.             newMessagesWin[x] = false;
  193.         }
  194.     }
  195.  
  196.     for (x in newMessages) {
  197.         if (newMessages[x] == true) {
  198.             if (chatboxFocus[x] == false) {
  199.                 //FIXME: add toggle all or none policy, otherwise it looks funny
  200.                 $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
  201.             }
  202.         }
  203.     }
  204.    
  205.     $.ajax({
  206.       url: "chat.php?action=chatheartbeat",
  207.       cache: false,
  208.       dataType: "json",
  209.       success: function(data) {
  210.  
  211.         $.each(data.items, function(i,item){
  212.             if (item)   { // fix strange ie bug
  213.  
  214.                 chatboxtitle = item.f;
  215.  
  216.                 if ($("#chatbox_"+chatboxtitle).length <= 0) {
  217.                     createChatBox(chatboxtitle);
  218.                 }
  219.                 if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
  220.                     $("#chatbox_"+chatboxtitle).css('display','block');
  221.                     restructureChatBoxes();
  222.                 }
  223.                
  224.                 if (item.s == 1) {
  225.                     item.f = username;
  226.                 }
  227.  
  228.                 if (item.s == 2) {
  229.                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  230.                 } else {
  231.                     newMessages[chatboxtitle] = true;
  232.                     newMessagesWin[chatboxtitle] = true;
  233.                     $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
  234.                 }
  235.  
  236.                 $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  237.                 itemsfound += 1;
  238.             }
  239.         });
  240.  
  241.         chatHeartbeatCount++;
  242.  
  243.         if (itemsfound > 0) {
  244.             chatHeartbeatTime = minChatHeartbeat;
  245.             chatHeartbeatCount = 1;
  246.         } else if (chatHeartbeatCount >= 10) {
  247.             chatHeartbeatTime *= 2;
  248.             chatHeartbeatCount = 1;
  249.             if (chatHeartbeatTime > maxChatHeartbeat) {
  250.                 chatHeartbeatTime = maxChatHeartbeat;
  251.             }
  252.         }
  253.        
  254.         setTimeout('chatHeartbeat();',chatHeartbeatTime);
  255.     }});
  256. }
  257.  
  258. function closeChatBox(chatboxtitle) {
  259.     $('#chatbox_'+chatboxtitle).css('display','none');
  260.     restructureChatBoxes();
  261.  
  262.     $.post("chat.php?action=closechat", { chatbox: chatboxtitle} , function(data){ 
  263.     });
  264.  
  265. }
  266.  
  267. function toggleChatBoxGrowth(chatboxtitle) {
  268.     if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
  269.        
  270.         var minimizedChatBoxes = new Array();
  271.        
  272.         if ($.cookie('chatbox_minimized')) {
  273.             minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
  274.         }
  275.  
  276.         var newCookie = '';
  277.  
  278.         for (i=0;i<minimizedChatBoxes.length;i++) {
  279.             if (minimizedChatBoxes[i] != chatboxtitle) {
  280.                 newCookie += chatboxtitle+'|';
  281.             }
  282.         }
  283.  
  284.         newCookie = newCookie.slice(0, -1)
  285.  
  286.  
  287.         $.cookie('chatbox_minimized', newCookie);
  288.         $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
  289.         $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
  290.         $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  291.     } else {
  292.        
  293.         var newCookie = chatboxtitle;
  294.  
  295.         if ($.cookie('chatbox_minimized')) {
  296.             newCookie += '|'+$.cookie('chatbox_minimized');
  297.         }
  298.  
  299.  
  300.         $.cookie('chatbox_minimized',newCookie);
  301.         $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
  302.         $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
  303.     }
  304.    
  305. }
esta es la primera parte.

Última edición por edie8; 26/08/2012 a las 04:33