作者:living10
共分为5个部分,Global.asax,index.aspx,talk.aspx,login.aspx,count.aspx。经2003sever+iss6.0测试通过。现在代码公布如下:
Global.asax代码:
<script language=vb runat=server>
Sub session_onstart() '统计在线人数,每进来一个cnt加1
Application.Lock()
Application("cnt") = Application("cnt") + 1
Application.UnLock()
End Sub
Sub session_onend()
Application.Lock()
Application("cnt") = Application("cnt") - 1
Application.UnLock()
End Sub
</script>
index.aspx代码:
<%
response.Redirect("login.aspx")
%>
login.aspx代码:
<script language=vb runat=server>
Sub login(ByVal s As Object, ByVal e As EventArgs)
If usrname.Text = " " Then
lbldesc.Text = "错误的用户名!请重新输入!"
Else
Session("usrname") = usrname.Text
Response.Redirect("talk.aspx")
End If
End Sub
</script>
<html>
<head runat="server">
<title>living10在线聊天室</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
在线讨论区<br />
<br />
请输入用户名:
<asp:TextBox ID="usrname" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="登录" OnClick="login" /></div>
<asp:Label ID=lbldesc ForeColor=red runat=server></asp:Label>
</form>
</body>
</html>
talk.aspx代码:
<script language=vb runat=server>
Sub page_load(ByVal s As Object, ByVal e As EventArgs)
If Session("usrname") = " " Then
Response.Redirect("login.aspx")
Else
cont.Text = Replace(Application("message"), "<para>", vbCrLf)
counter.Text = Application("cnt")
End If
End Sub
Sub talk(ByVal s As Object, ByVal e As EventArgs)
If Session("usrname") = " " Then
Response.Redirect("p5-2.aspx")
Else
Dim name, content, ip, show As String
Dim i, n As Integer
name = Session("usrname")
content = Request("cont1")
ip = Request.ServerVariables("remote_addr")
Application.Lock()
'将用户名,ip地址以及发言内容写入application对象中
Application("message") = name + ":" + ip + ":" + content + "<para>" + Application("message")
Application.UnLock()
show = Application("message")
'只保存最新20条信息,其他的内容删除
i = 1
For n = 1 To 20
i = InStr(i, show, "<para>") + 6 '查找是否到达最末消息
If i = 6 Then Exit For '已到达最末消息
Next
If i <> 6 Then
Application.Lock()
Application.UnLock()
End If
cont.Text = Replace(Application("message"), "<para>", vbCrLf)
End If
End Sub
Sub logout(ByVal s As Object, ByVal e As EventArgs)
Session.Abandon()
Response.Redirect("count.aspx")
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
在线讨论区 Prowered By living10 <br />
<asp:TextBox ID="cont" runat="server" Columns=120 Rows=30 ReadOnly=true TextMode=multiLine></asp:TextBox><br />
参与讨论:<asp:TextBox ID="cont1" runat="server" size=50 Width="224px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="发言" OnClick="talk"/>
<asp:Button ID="Button2" runat="server" Text="退出" OnClick="logout" />
当前在线人数:
<asp:TextBox ID="counter" runat="server" Width="88px"></asp:TextBox></div>
</form>
</body>
</html>
count.aspx代码:
<script language=vb runat=server>
Sub page_load(ByVal s As Object, ByVal e As EventArgs)
counter.Text = "在线人数:" & Application("cnt")
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
统计在线人数<br />
<br />
<asp:TextBox ID="counter" runat="server" size=20></asp:TextBox></div>
</form>
</body>
</html>
asp.net 在线聊天室
最新推荐文章于 2021-08-05 17:49:32 发布