我正在使用codeigniter进行聊天但是没有work.please帮助,这是我的代码。
我的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('welcome_mdl');
}
public function index()
{
$this->load->view('welcome_message');
}
public function saveUserInfo()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
if ($this->form_validation->run()) {
$user_id=$this->welcome_mdl->saveUser($this->form_validation->set_value('username'));
redirect('welcome/chatRoom/'.$user_id.'');
}
else
{
redirect('');
}
}
public function chatRoom($user_id)
{
if($user_id)
{
$result=$this->welcome_mdl->getUser($user_id);
//
$data['user_id']=$result['user_id'];
//
$data['username']=$result['username'];
$this->load->view('chatpage',$data);
}
else
{
redirect('');
}
}
}
模型
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Welcome_mdl extends CI_Model
{
private $users_table_name = 'users';
function __construct()
{
parent::__construct();
$ci =& get_instance();
$this->users_table_name = $this->users_table_name;
}
function saveUser($username)
{
$this->db->set('online',1);
$this->db->set('username',$username);
$this->db->insert('users');
return $this->db->insert_id();
}
function getUser($user_id)
{
$this->db->where('user_id',$user_id);
$query=$this->db->get('users');
// print_r($query->row_array());die;
return $query->row_array();
}
}
查看page-welcome.message.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<!--<link rel="shortcut icon" href="../../assets/ico/favicon.ico">-->
<title>Welcome to ChatRoom</title>
<!-- Bootstrap core CSS -->
<link href="<?php echo base_url();?>asset/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<?php echo base_url();?>asset/css/signin.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<form class="form-signin" role="form" action="<?php echo base_url();?>index.php/welcome/saveUserInfo" method="post">
<h2 class="form-signin-heading">Enter your username</h2>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter your name" required autofocus name="username" id="username">
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Join Room</button>
</form>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
</body>
</html>
chatpage.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">
<title>Welcome to ChatRoom</title>
<!-- Bootstrap core CSS -->
<link href="<?php echo base_url();?>asset/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap theme -->
<link href="<?php echo base_url();?>asset/css/bootstrap-theme.min.css" rel="stylesheet">
<!-- Font Awesome core CSS -->
<link href="<?php echo base_url();?>asset/css/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<?php echo base_url();?>asset/css/theme.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body role="document">
<!-- Fixed navbar -->
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="<?php echo site_url('chatRoom/'.$user_id);?>">ChatRoom</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#">Hi <?php echo $username?></a></li>
<li><a href="<?php echo base_url();?>index.php">Leave Room</a></li>
</ul>
</div>
</div>
</div>
<div class="container theme-showcase" role="main">
<div class="spinner-feeds" style="text-align: center; margin-top: 200px;"><i class="fa fa-spinner fa-spin fa-3x"></i></div>
<div class="error-feeds alert alert-danger" style="display:none;margin: 0px;"></div>
<div class="panel panel-default" id="chat-window" style="display:none;">
</div>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="<?php echo base_url();?>asset/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>asset/js/moment.min.js"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var user_id='<?php echo $user_id?>';
var room_id=1;
if(typeof io=="undefined")
{
$('.spinner-feeds').hide();
$('#chat-window').hide();
$('.error-feeds').show();
}
else
{
var socket = io.connect('http://localhost:3000');
var chatpage=socket.of('/chatpage')
.on('connect_failed', function (reason) {
$('.spinner-feeds').hide();
$('#chat-window').hide();
$('.error-feeds').show();
console.error('unable to connect chatpage to namespace', reason);
})
.on('error',function(reason){
//alert("in error func");
$('.spinner-feeds').hide();
$('#chat-window').hide();
$('.error-feeds').show();
$('#chat-window').html('');
console.error('unable to connect chatpage to namespace', reason);
})
.on('reconnect_failed',function(){
alert("in reconnect fail func");
$('.spinner-feeds').hide();
$('#chat-window').hide();
$('.error-feeds').show();
$('#chat-window').html('');
})
.on('connect', function () {
console.info('sucessfully established a connection of chatpage with the namespace');
chatpage.emit('senddata',{user_id:user_id,room_id:room_id});
});
chatpage.on('chatdata',function(data){
$('.error-feeds').hide();
$('.spinner-feeds').hide();
$('#chat-window').html('');
$('#chat-window').show();
var header='';
var content='';
var footer='';
var cells='';
if(data.memdata)
{
for(n in data.memdata)
{
cells+='<span class="label label-default"><input type="hidden" class="userId" value="'+data.memdata[n].user_id+'"/>'+data.memdata[n].username+'</span> ';
}
}
header='<div class="panel-heading"><h3 class="panel-title"><span class="label label-success">Online:</span> <span id="online-list"> '+cells+'</span></h3></div>';
$('#chat-window').append(header);
content='<div class="panel-body" style="min-height:410px;"><ul class="media-list" id="chat_block_list"></ul></div>';
$('#chat-window').append(content);
if(data.converdata)
{
for(n in data.converdata)
{
$('#chat_block_list').append('<li class="media"><div class="media-body"><h4 class="media-heading">'+data.converdata[n].username+'</h4><p>'+data.converdata[n].comment+'</p></div></li>');
}
}
else
{
$('#chat_block_list').append('');
}
footer='<div class="panel-footer"><textarea class="form-control" rows="2" style="display:inline;width:95%" name="msg_box" id="msg_box"></textarea><button type="button" class="btn btn-primary btn-sm pull-right" id="msg_send">Send</button></div>';
$('#chat-window').append(footer);
});
chatpage.on('showcomment',function(data){
$('#chat_block_list').append('<li class="media"><div class="media-body"><h4 class="media-heading">'+data.room_comment[0].username+'</h4><p>'+data.room_comment[0].comment+'</p></div></li>');
$('#msg_box').val('');
});
chatpage.on('newuser',function(data){
$('#online-list').append('<span class="label label-default"><input type="hidden" class="userId" value="'+data.userdata[0].user_id+'"/>'+data.userdata[0].username+'</span> ')
});
chatpage.on('removeuser',function(data){
$('#online-list span').each(function(index){
var user_id=$(this).find('.userId').val();
if(user_id==data.user_id)
{
$(this).remove();
}
});
});
$('body').on("keypress",'#msg_box', function(e) {
if (e.which == 13) {
$(this).blur();
var message = $(this).val();
if(message)
{
sendChat(message);
}
return false; // prevent the button click from happening
}
});
$('body').on("click",'#msg_send', function(e) {
var message = $('#msg_box').val();
if(message)
{
sendChat(message);
}
});
function sendChat(message)
{
var curr_date= moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
chatpage.emit('sendcomment', {msg:message,user_id:user_id,room_id:room_id,datetime:curr_date});
}
}
function confirmExit()
{
alert("exiting");
window.location.href='<? echo base_url();?>index.php';
return true;
}
window.onbeforeunload = confirmExit;
</script>
</body>
</html>
user和user_id被插入表'users',但没有数据进入表'comments'。
MyDatabase的:chat.sql
-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jun 23, 2014 at 04:05 AM
-- Server version: 5.5.27
-- PHP Version: 5.4.7
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `chat`
--
-- --------------------------------------------------------
--
-- Table structure for table `comments`
--
CREATE TABLE IF NOT EXISTS `comments` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`comment` text NOT NULL,
`user_id` int(11) NOT NULL,
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`online` tinyint(4) NOT NULL DEFAULT '1',
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;