Welcome Guest, Not a member yet? Register   Sign In
Chat for CI.
#1

[eluser]InterMedio[/eluser]
Hello everyone.
I need show all users, who are in the chat. So, I use simple chat and authorization.
Code of chat (controller):
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Chat extends CI_Controller{

function __construct()
{
  parent::__construct();

  $this->load->helper('url');
  $this->load->library('simple_auth');
  
}
function index(){  
  
  if (!$this->simple_auth->user_logged()) {
   redirect('/login/');
  } else {
   $data_chat['who_are_you'] = $this->simple_auth->get_name();
   $this->load->view('chatty', $data_chat);  
  
  }
}

function backend(){
  
  $store_num = 40;
  $display_num = 40;
  
  header("Content-type: text/xml");
  header("Cache-Control: no-cache");
  
  foreach($_POST AS $key => $value) {
      ${$key} = mysql_real_escape_string($value);
  }    


   if(@$action == "postmsg"){
    $current = time();  
    if ($name === $this->simple_auth->get_name()) {
     $this->db->query("INSERT INTO messages SET user='$name', msg='$message', time='$current' ");  
     $delid = mysql_insert_id() - $store_num;
     $this->db->query("DELETE FROM messages WHERE id <= $delid");
    } else {
     $this->error;
    }
   }

  
   if (empty($time)){
    $sql = "SELECT * FROM messages ORDER BY id ASC LIMIT $display_num";
   }else{
    $sql = "SELECT * FROM messages WHERE time > $time ORDER BY id ASC LIMIT $display_num";
   }
  
   $query = $this->db->query("$sql");
  
   if($query->num_rows()==0){
    $status_code = 2;
   }else{
    $status_code = 1;
   }
    
   echo "&lt;?xml version=\"1.0\"?&gt;\n";
   echo "<response>\n";
   echo "\t<status>$status_code</status>\n";
   echo "\t<time>".time()."</time>\n";
  
    if($query->num_rows()>0){
     foreach($query->result() as $row){    
      $escmsg = htmlspecialchars(stripslashes($row->msg));
      echo "\t<message>\n";
      echo "\t\t<author>$row->user</author>\n";
      echo "\t\t<text>$escmsg</text>\n";
      echo "\t</message>\n";
     }
    }
   echo "</response>";
    
  
}

}
?&gt;


and chatty.php (view)

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Chat&lt;/title&gt;
[removed][removed]
[removed]
  $(document).ready(function(){
   timestamp = 0;
   updateMsg();
   hideLoading();
   $("form#chatform").submit(function(){
    showLoading();        
    $.post("/chat/backend",{
       message: $("#content").val(),
       name: $("#name").val(),
       action: "postmsg",
       time: timestamp
      }, function(xml) {
     addMessages(xml);
     $("#content").val("");
     hideLoading();
     $("#content").focus();
    });
    return false;
   });
  });
  function rmContent(){
  
  }
  function showLoading(){
   $("#contentLoading").hide(); /* $("#contentLoading").show(); */
   $("#txt").hide();
   $("#author").hide();
  }
  function hideLoading(){
   $("#contentLoading").hide();
   $("#txt").show();
   $("#author").show();
  }
  function addMessages(xml) {
   if($("status",xml).text() == "2") return;
   timestamp = $("time",xml).text();
   $("message",xml).each(function(id) {
    message = $("message",xml).get(id);
    $("#messagewindow").prepend("<b>"+$("author",message).text()+
           "</b>: "+$("text",message).text()+
           "<br />");
   });

  
  }
  function updateMsg() {
   $.post("/chat/backend",{ time: timestamp }, function(xml) {
    $("#loading").remove();    
    addMessages(xml);
   });
   setTimeout('updateMsg()', 1000);
  }
[removed]
&lt;style type="text/css"&gt;
  #messagewindow {
   height: 250px;
   border: 1px solid;
   padding: 5px;
   overflow: auto;
  }
  #wrapper {
   /*margin: auto;*/
   width: 438px;
  }
  input {
   width: 100%;
   border: 1px solid gray;
   height: 30px;
  }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="wrapper">
  <p id="messagewindow"><span id="loading">Loading...</span></p>
  &lt;form id="chatform"&gt;
  <div id="author">
    &lt;input type="hidden" name="user_name" id="name" value="&lt;?php echo $who_are_you; ?&gt;"/&gt;
  </div><br />

  <div id="txt">
       &lt;input type="text" name="content" id="content" value="" /&gt;
  </div>
  
  <div id="contentLoading" class="contentLoading">  
  <img src="/public/images/blueloading.gif" alt="Loading data, please wait...">  
  </div><br />
  
  &lt;input type="hidden" value="ok" /&gt;&lt;br />
  &lt;/form&gt;
  <p>&lt;?php if  ($_SERVER['PHP_SELF'] == '/index.php/chat') { echo $who_are_you;}?&gt;</p>
  <p>&lt;?php echo anchor('city', 'City'); ?&gt;</p>
  
</div>
&lt;/body&gt;
&lt;/html&gt;

Any ideas?
I think get the data from database - need use cron, but, I guess, this not better solution.




Theme © iAndrew 2016 - Forum software by © MyBB