ASP+TXT文件读写实现简易聊天室

临近暑假,是个不错的开发时间,为此,首先要实现的功能就是聊天室
我上网查了一下,大多做asp聊天室的用的都是ASP/Access
但是如果用这个原理去做的话就会发现一个问题,发不了较长的文字,因此用txt存储聊天内容的方案就油然而生。
来,上代码:

  1. index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="./main.css">
<script type="text/javascript" src="./main.js"></script>
<title>asp+txt聊天室</title>
</head>
<body>
<input type="text" style="display:none;" id="ajaxwrite" value=" ">
<div class="body_in" id="body_in">
	<form name="form1" method="post" action="chat.asp" target="frme" onsubmit="return checkform()">
		<iframe src="chat.asp" name="frme" style="display:none;"></iframe>
		<div style="overflow-x:hidden;overflow-y:scroll;width:99%;height:350px;border-radius:5px;" id="txtHint"></div>
		<br>
		<input type="text" style="display:none;" name=userName value="Default_User">
		<input type="text" style="width:85%;border-radius:4px;" name=sContent autocomplete="off" placeholder="说点什么~" id="cnt">
		&nbsp;
		<input type="submit" style="border-radius:3px;border:none;padding:5px;height:27px" name=Submit value=" 发送 ">
		&nbsp;
		<input type="button" style="border-radius:3px;border:none;padding:5px;height:27px" onclick="document.getElementById('txtHint').innerHTML=''" value=" 清屏 ">
	</form>
</div>
</body>
</html>
  1. main.css
.body_in{
	border:3px solid #ccc;
	padding:5px;
	border-radius:16px;
	background-color:#FFFFFF;
	width:800px;
}
xmp{
	white-space:pre-wrap;
	white-space:-moz-pre-wrap;
	white-space:-pre-wrap;
	white-space:-o-pre-wrap;
	word-wrap:break-word;
}
  1. main.js

window.onload=function(){
     showHint();
     setInterval("showHint()", 2000);//两秒钟刷新一次
}

function showHint()//ajax
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			if(xmlhttp.responseText != document.getElementById("ajaxwrite").value){
				document.getElementById("txtHint").innerHTML=xmlhttp.responseText + document.getElementById("txtHint").innerHTML;
				document.getElementById("ajaxwrite").value=xmlhttp.responseText;
			}else document.getElementById("ajaxwrite").value=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","chat.asp",true);
	xmlhttp.send();
}

function checkform()
{
	var flag=true;
	if(document.form1.sContent.value=="")
	{
		alert("不能发送空消息!");
		document.form1.sContent.focus();
		return false
	};
	return flag;
}
function backform()
{
	window.close();
}
function bk()
{
    alert("即将退出聊天室");
	setTimeout(backform,1000);
}
  1. 最重要的来了:chat.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
	session.codepage=65001
	response.charset="utf-8"
%>

<%
dim conn,connstr,rs
Server.ScriptTimeout=900
On Error Resume Next
	Set conn = Server.CreateObject("ADODB.Connection")
	Conn.open connstr
%>

<%
    context=request("sContent")
	username=request("userName")
    if context <> "" and username <> "" then
		CFile=Server.MapPath("mdb.txt")
		SET FileObject=Server.CreateObject("Scripting.FileSystemObject")
		Set Out=FileObject.CreateTextFile(CFile,TRUE,FALSE)
		Application.lock
		Out.WriteLine("[" + username + "] " + context)
		Application.unlock
		Out.Close
		
		CFile=Server.MapPath("mdb.txt")
		Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
		Set Out=FileObject.OpenTextFile(CFile,1,FALSE,FALSE)
		con=Out.ReadLine
		Out.Close
		response.write("<xmp style='border-radius:7px;padding:5px;border:1px solid #ccc;'> "+con+"</xmp>")
	else
		CFile=Server.MapPath("mdb.txt")
		Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
		Set Out=FileObject.OpenTextFile(CFile,1,FALSE,FALSE)
		con=Out.ReadLine
		Out.Close
		response.write("<xmp style='border-radius:7px;padding:5px;border:1px solid #ccc;'> "+con+"</xmp>")
	end if
%>

作者实测,绝对成功
我用的服务器是IIS

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值