Chat for CI. - El Forum - 11-22-2011
[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 "<?xml version=\"1.0\"?>\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>";
}
}
?>
and chatty.php (view)
Code: <html>
<head>
<title>Chat</title>
[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]
<style type="text/css">
#messagewindow {
height: 250px;
border: 1px solid;
padding: 5px;
overflow: auto;
}
#wrapper {
/*margin: auto;*/
width: 438px;
}
input {
width: 100%;
border: 1px solid gray;
height: 30px;
}
</style>
</head>
<body>
<div id="wrapper">
<p id="messagewindow"><span id="loading">Loading...</span></p>
<form id="chatform">
<div id="author">
<input type="hidden" name="user_name" id="name" value="<?php echo $who_are_you; ?>"/>
</div><br />
<div id="txt">
<input type="text" name="content" id="content" value="" />
</div>
<div id="contentLoading" class="contentLoading">
<img src="/public/images/blueloading.gif" alt="Loading data, please wait...">
</div><br />
<input type="hidden" value="ok" /><br />
</form>
<p><?php if ($_SERVER['PHP_SELF'] == '/index.php/chat') { echo $who_are_you;}?></p>
<p><?php echo anchor('city', 'City'); ?></p>
</div>
</body>
</html>
Any ideas?
I think get the data from database - need use cron, but, I guess, this not better solution.
|