asp.net 在线聊天室

asp.net 在线聊天室

作者: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 />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; 在线讨论区<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp;<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        请输入用户名:
        <asp:TextBox ID="usrname" runat="server"></asp:TextBox><br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <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>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; 在线讨论区                            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>&nbsp;
        <asp:Button ID="Button1" runat="server" Text="发言" OnClick="talk"/>
        &nbsp; &nbsp;
        <asp:Button ID="Button2" runat="server" Text="退出" OnClick="logout" />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp;当前在线人数:&nbsp;
        <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>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
        <br />
        <br />
        <br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; 统计在线人数<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp;<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
        &nbsp;
        <asp:TextBox ID="counter" runat="server" size=20></asp:TextBox></div>
    </form>
</body>
</html>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值