轻量级网页在线聊天系统
2017-08-19 23:50
260 查看
ajax异步请求实时刷新
用文本文档代替数据库
主页
index.html
<!DOCTYPE html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
<title>Document</title>
<style
type="text/css">
*{
margin:
0;
padding:
0;
}
.bigbox textarea{
width:
600px;
height:100px;
border:
1px solid #666;
margin:0
auto;
/*text-align: center;*/
display:
block;
/*margin-left: 200px;*/
}
.bigbox input{
/*display: block;*/
margin-left:
930px;
}
.bigbox .box{
width:
600px;
height:
400px;
overflow-y:
auto;
word-break:
break-all;
text-overflow:
clip;
border:
1px solid black;
margin:
10px auto;
margin-bottom:
20px;
}
ul{
list-style:
none;
}
li{
margin:
30px;
/*border:1px solid red;*/
background:
skyblue;
}
/*li a{*/
/*text-decoration: none;*/
/*border: 1px solid gray;*/
/*border-radius: 5px;*/
/*background-color: skyblue;*/
/*word-wrap: break-word;*/
/*max-width: 300px;*/
/*min-width: 50px;*/
/*display: block;*/
/*padding: 10px;*/
/*margin-top: 30px;*/
/*}*/
</style>
</head>
<body>
<div
class="bigbox">
<div
class="box"
id="content">
<ul>
<li>今日最佳影片......</li>
</ul>
</div>
<div>
<form>
<textarea
placeholder="发表评论吧"></textarea>
<br>
<input
type="button"
name=""
value="发表评论">
</form>
</div>
</div>
</body>
<script
type="text/javascript">
var content =
document.getElementById('content');
readData();
//打开页面读取数据
// console.log(content.scrollHeight);
// content.scrollTop =(content.offsetHeight);
//
document.querySelector('input[type=button]').onclick=function(){
var nowTime=new
Date();
var xhr=new
XMLHttpRequest();
xhr.open('post','show.php');
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
document.querySelector('ul').innerHTML=xhr.responseText;
content.scrollTop =
content.scrollHeight;
document.querySelector('textarea').value='';
}
}
//判断发送的内容是否为空
if(document.querySelector('textarea').value!=''){
xhr.send('val=<li>'+gettime(nowTime)+':发表评论:
'+document.querySelector('textarea').value+'</li>');
}else{
xhr.send('val=');
}
}
setInterval(function(){
readData();//每隔一秒读取页面实时刷新
},1000)
function readData(){
var xhr=new
XMLHttpRequest();
xhr.open('post','show.php');
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
document.querySelector('ul').innerHTML=xhr.responseText;
content.scrollTop =
content.scrollHeight;
}
}
xhr.send('val=');
}
function
gettime(data){
var year=data.getFullYear();
var month=(+data.getMonth())+1;
var day=data.getDate();
var hour=data.getHours();
var min=data.getMinutes();
var sec=data.getSeconds();
month =
month < 10 ?
"0"+month :month;
var str =
"" +year +
"-" + month +"-" +day +
" " +hour +":" +min +":" +sec;
return str;
}
</script>
</html>
show.php 读取文档里面的数据,根据发送来的数据更新数据
<?php
$val=$_POST['val'];
$ip = $_SERVER["REMOTE_ADDR"];
$ips='游客:ip为'.$ip.'在';
$info=file_get_contents('liuyanban.txt');
if($val!=''){
file_put_contents('liuyanban.txt',$info.''.$ips.''.$val);
}else{
// file_put_contents('liuyanban.txt',$info.''.$val);
}
$info1=file_get_contents('liuyanban.txt');
echo $info1;
?>
liyanban.txt 建一个空的即可,注意路径不要错
最终页面展示
用文本文档代替数据库
主页
index.html
<!DOCTYPE html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
<title>Document</title>
<style
type="text/css">
*{
margin:
0;
padding:
0;
}
.bigbox textarea{
width:
600px;
height:100px;
border:
1px solid #666;
margin:0
auto;
/*text-align: center;*/
display:
block;
/*margin-left: 200px;*/
}
.bigbox input{
/*display: block;*/
margin-left:
930px;
}
.bigbox .box{
width:
600px;
height:
400px;
overflow-y:
auto;
word-break:
break-all;
text-overflow:
clip;
border:
1px solid black;
margin:
10px auto;
margin-bottom:
20px;
}
ul{
list-style:
none;
}
li{
margin:
30px;
/*border:1px solid red;*/
background:
skyblue;
}
/*li a{*/
/*text-decoration: none;*/
/*border: 1px solid gray;*/
/*border-radius: 5px;*/
/*background-color: skyblue;*/
/*word-wrap: break-word;*/
/*max-width: 300px;*/
/*min-width: 50px;*/
/*display: block;*/
/*padding: 10px;*/
/*margin-top: 30px;*/
/*}*/
</style>
</head>
<body>
<div
class="bigbox">
<div
class="box"
id="content">
<ul>
<li>今日最佳影片......</li>
</ul>
</div>
<div>
<form>
<textarea
placeholder="发表评论吧"></textarea>
<br>
<input
type="button"
name=""
value="发表评论">
</form>
</div>
</div>
</body>
<script
type="text/javascript">
var content =
document.getElementById('content');
readData();
//打开页面读取数据
// console.log(content.scrollHeight);
// content.scrollTop =(content.offsetHeight);
//
document.querySelector('input[type=button]').onclick=function(){
var nowTime=new
Date();
var xhr=new
XMLHttpRequest();
xhr.open('post','show.php');
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
document.querySelector('ul').innerHTML=xhr.responseText;
content.scrollTop =
content.scrollHeight;
document.querySelector('textarea').value='';
}
}
//判断发送的内容是否为空
if(document.querySelector('textarea').value!=''){
xhr.send('val=<li>'+gettime(nowTime)+':发表评论:
'+document.querySelector('textarea').value+'</li>');
}else{
xhr.send('val=');
}
}
setInterval(function(){
readData();//每隔一秒读取页面实时刷新
},1000)
function readData(){
var xhr=new
XMLHttpRequest();
xhr.open('post','show.php');
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
document.querySelector('ul').innerHTML=xhr.responseText;
content.scrollTop =
content.scrollHeight;
}
}
xhr.send('val=');
}
function
gettime(data){
var year=data.getFullYear();
var month=(+data.getMonth())+1;
var day=data.getDate();
var hour=data.getHours();
var min=data.getMinutes();
var sec=data.getSeconds();
month =
month < 10 ?
"0"+month :month;
var str =
"" +year +
"-" + month +"-" +day +
" " +hour +":" +min +":" +sec;
return str;
}
</script>
</html>
show.php 读取文档里面的数据,根据发送来的数据更新数据
<?php
$val=$_POST['val'];
$ip = $_SERVER["REMOTE_ADDR"];
$ips='游客:ip为'.$ip.'在';
$info=file_get_contents('liuyanban.txt');
if($val!=''){
file_put_contents('liuyanban.txt',$info.''.$ips.''.$val);
}else{
// file_put_contents('liuyanban.txt',$info.''.$val);
}
$info1=file_get_contents('liuyanban.txt');
echo $info1;
?>
liyanban.txt 建一个空的即可,注意路径不要错
最终页面展示
相关文章推荐
- WoLive-在线客服系统源码,网页聊天源码开源版
- 昨天做了个jsp版网页在线聊天系统
- Unity轻轻轻轻轻量级多人在线聊天系统
- Yahoo,Msn,Skype,QQ,阿里旺旺,网页聊天,网页链接,在线聊天链接接口调用
- 在线即时通讯工具的网页即时聊天的html代码
- 几个在线网页聊天网站
- 在线客服聊天系统教学视频以及程序源代码和模板
- 基于webSocket实现的一对一在线聊天系统
- 前端通信:SSE设计方案(二)--- 服务器推送技术的实践以及一些应用场景的demo(包括在线及时聊天系统以及线上缓存更新,代码热修复案例)
- tomcat websocket 实现网页在线即时聊天
- Linux网络编程(多人在线聊天系统)
- 详解如何实现在线聊天系统中的实时消息获取
- asp.net 网页在线即时聊天
- [Web 开发] 在网页中嵌入常用聊天软件在线状态
- 基于Socekt的Unity多人在线聊天系统
- 项目经验总结---网页在线聊天
- 网页中嵌套QQ代码设置在线状态无需加好友聊天
- 在线客服聊天系统powertalkbox 所有文章
- 为你的博客打造自己的在线聊天系统-方便在线交流
- 在线聊天系统