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

上代码:

index.html

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;

}

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("不能发送空消息!");外汇常见问题https://www.fx61.com/faq

document.form1.sContent.focus();

return false

};

return flag;

}

function backform()

{

window.close();

}

function bk()

{

alert("即将退出聊天室");

setTimeout(backform,1000);

}

最重要的来了: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("

"+con+"")

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("

"+con+"")

end if

%>

你可能感兴趣的:(asp,txt)