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

    技术2022-08-01  77

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

    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">   <input type="submit" style="border-radius:3px;border:none;padding:5px;height:27px" name=Submit value=" 发送 ">   <input type="button" style="border-radius:3px;border:none;padding:5px;height:27px" onclick="document.getElementById('txtHint').innerHTML=''" value=" 清屏 "> </form> </div> </body> </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("不能发送空消息!"); 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("<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

    Processed: 0.014, SQL: 9