用ajax控件 编写的聊天室中,接收方是怎么收到发送方发送的消息的啊? 哪位大侠指点一下。就是一个人说话了,另一个人的页面是怎么刷新的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有一种技术叫 ajax,还有一种技术叫 comet 与轮询,不是一两句话能说清楚的好好研读吧 http://www.cnblogs.com/kingjiong/archive/2008/10/14/1311106.html这篇文章中是怎么实现的,不会是他说的“Timer Control: 这个控件主要是为了更新聊天室的内容,每隔7秒刷新一次内容”这个控件来刷新实现的吧,那也太慢了啊。我看了半天也没找到其他刷新的地方。我下了他代码测试了一下,这边一发,那边就刷新了。 那基本上就是给你玩、自娱自乐的。基于传统的http机制没有什么好办法,如果你把它当真了,它注定了要拖垮服务器、而且响应也很慢。现在比较多地是在网页中嵌入一个小的flash代码,然后利用flash的socket能力来通讯。当然,网页嵌入silverlight也是一样。 你也可以自己去找互联网上的web应用来研究。随便找一个http调试器(比如ie8的“开发人员工具”即可),然后找几个网上几乎所有相互转抄的文章中都说是什么http长连接的web应用,你跟踪一下看看它真的是什么长连接吗? 呃~~我觉得这杨对http长连接的说法可能还是还是有点不妥。我换一种说法吧。你的服务器端不是你所看到的asp.net那种机制,你需要掌握比较底层的方式来自己写http服务器程序,并且你需要多台服务器,它不是跟搞asp.net应用程序的人可以说明白的。它需要一些只有(哪怕很小)电信级服务器端通讯系统平台才能支持的东西。 我先理解这个简单的,那这篇文章(http://www.cnblogs.com/kingjiong/archive/2008/10/14/1311106.html)里面是不是就是用哪个timer控件控制每7秒刷一次啊,<asp:Timer Id="Timer1" Interval="7000" OnTick="Timer1_OnTick" runat="server" />代码里面interval确实是7秒,但是我感觉刷新的很快啊,这边发送信息,那边一两秒就收到了,还是他通过其他的方式刷新的啊。 每隔几秒自动把updatepanel里Application呈现给用户发言时改变Application值 ajax定时发送请求,接收相关信息,http长链接也很损服务器资源 我就是这样整的啊,但是我有两个问题啊。1.就是每次刷新的时候嘛,显示消息的div的滚动条,都会滚动到最上面。不发送消息,我用鼠标滚动的时候,刷新的时候他也会自动滚动到最上面去了,没法查看聊天记录。2.存在Application里面,到最后那application会变成很大啊,有没有更好的方法啊,或者用Application存,没过一段时间给他清一次零。但是那样的话。所有的聊天记录都没了。代码如下:能运行,就是存在上面那两个问题搞不明白,那我大哥帮我看下啊!Default.aspx:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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> <script type ="text/javascript"> //使DIV的滚动条移动到最底端 function layerMove() { var div1 = document.getElementById("<%=content.ClientID %>"); div1.scrollTop = 5000; }</script></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <%-- 显示消息部分--%> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000"> </asp:Timer> <div id ="content" runat="server" style="padding: 10px; border: 1px solid #9999FF; height: 280px; width: 95%;overflow-y:auto" ></div> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> <asp:AsyncPostBackTrigger ControlID="btn_send" EventName="Click" /> </Triggers> </asp:UpdatePanel> <%-- 发送消息部分--%> <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"> <ContentTemplate> <div style="margin-top: 9px" > <asp:TextBox ID="txt_Message" runat="server" CssClass="text_input" Height="70px" TextMode="MultiLine" Width="93%"></asp:TextBox> <asp:Button ID="btn_send" runat="server" CssClass="button_bak" Text="发送" onclick="btn_send_Click" /> </div> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="btn_send" EventName="Click" /> </Triggers> </asp:UpdatePanel> </form></body></html>后台页面:Default.aspx.csusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //初次打开时,提示进入了房间。 Application.Lock(); Application["Message"] += "<font color=red>" + Session["UserName"] + "</font>" + "<font color=blue> 在</font>" + System.DateTime.Now.ToLongTimeString() + "<font color=blue>" + " 进入了房间" + "</font>" + "<br/>"; Application.UnLock(); } } //每2秒刷新一次DIV content中的内容,并使其滚动条在最下面 protected void Timer1_Tick(object sender, EventArgs e) { //判断Application["Message"]变化,变化则更新 if(content.InnerHtml != Application["Message"].ToString()) { content.InnerHtml = Application["Message"].ToString(); //滚动条滚动到最下面 ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "layerMove();", true); } } protected void btn_send_Click(object sender, EventArgs e) { //发送某人说的话 Application.Lock(); Application["Message"] += Session["UserName"] + " " + System.DateTime.Now.ToLongTimeString() + " :" + txt_Message.Text + "<br/>"; Application.UnLock(); //更新对话框 content.InnerHtml = Application["Message"].ToString(); //滚动条滚动到最下面 ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "layerMove();", true); txt_Message.Text = ""; }}代码很简单,那我大哥帮我看下啊 你可以加上一个<label><input type="checkbox" onclick="stop=this.checked">停止滚动</label><script type ="text/javascript">var stop = false;function layerMove() { if(stop) return; var div1 = document.getElementById("<%=content.ClientID %>"); div1.scrollTop = 5000;}</script> 本帖最后由 net_lover 于 2011-08-15 13:25:27 编辑 WEB聊天程序最好最好还是用falsh之类的做吧。否则人多服务器压力会很大的 不会写呀,孟大哥,我的整个程序就13楼得那一个default页面,能帮我改下吗,还有那个滚动条滚动的问题。 正解,可以会有一些HTTP拉取技术。好像拉取有三种,采用的就是JS定时防问。一般在线聊天,还是基于FALSH开发吧。就跟网页游戏一样,都是FALSH 哈哈,那个滚动条的问题终于解决了。每次刷新,你如果不设scrollTop值的话,他就默认滚动到最上面,所以必须设,具体原因我也不清楚。解决方法是是这样的:每次刷新时,记录刷新前的值,如果没有人说话,则给Srolltop=刷新前的值。如果有人发言,则滚动条滚动到下面。参考博客 :http://http://blog.csdn.net/metababy/article/details/5960501希望能帮到有相同问题的人。 还有谢谢孟老大啊,结贴了。上面的链接多了个http ,晕。http://blog.csdn.net/metababy/article/details/5960501 请帮忙看下这两种传值到了存储过程中有何区别(已经被困扰了一晚上,请大家帮帮忙!) gridview 中绑定后链接无显示 如何用控制HTML按钮的可用性~ 网站精确查询 ASP.NET中用JAVASCRIPT遇到的问题 什么样的服务器SmtpMail.SmtpServer的值 谁给我一个好的TreeView范例吧 生成一些事件之后 控件又被删除 这些事件不能自动删除吗? 难怪要手动删除吗?还有一个问题! 为什么在读取datagrid的textbox的text值(datagrid里点击edit后)时读到的却是原来的值,而不是编辑后(即所看到的EDIT的TEXT)的值? 求.net的安装程序!!! 点击会增加div的取值和控制问题,就是类似邮箱添加附件,会增加一行的那种的取值和控制 无法删除此对象,因为未在 ObjectStateManager 中找到它。
这篇文章中是怎么实现的,不会是他说的“Timer Control: 这个控件主要是为了更新聊天室的内容,每隔7秒刷新一次内容”这个控件来刷新实现的吧,那也太慢了啊。我看了半天也没找到其他刷新的地方。我下了他代码测试了一下,这边一发,那边就刷新了。
1.就是每次刷新的时候嘛,显示消息的div的滚动条,都会滚动到最上面。不发送消息,我用鼠标滚动的时候,刷新的时候他也会自动滚动到最上面去了,没法查看聊天记录。
2.存在Application里面,到最后那application会变成很大啊,有没有更好的方法啊,或者用Application存,没过一段时间给他清一次零。但是那样的话。所有的聊天记录都没了。代码如下:能运行,就是存在上面那两个问题搞不明白,那我大哥帮我看下啊!
Default.aspx:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>
<script type ="text/javascript">
//使DIV的滚动条移动到最底端
function layerMove() {
var div1 = document.getElementById("<%=content.ClientID %>");
div1.scrollTop = 5000;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<%-- 显示消息部分--%>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000">
</asp:Timer>
<div id ="content" runat="server"
style="padding: 10px; border: 1px solid #9999FF; height: 280px; width: 95%;overflow-y:auto" ></div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
<asp:AsyncPostBackTrigger ControlID="btn_send" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<%-- 发送消息部分--%>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div style="margin-top: 9px" >
<asp:TextBox ID="txt_Message" runat="server" CssClass="text_input"
Height="70px" TextMode="MultiLine" Width="93%"></asp:TextBox>
<asp:Button ID="btn_send" runat="server" CssClass="button_bak" Text="发送"
onclick="btn_send_Click" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_send" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
后台页面:Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//初次打开时,提示进入了房间。
Application.Lock();
Application["Message"] += "<font color=red>" + Session["UserName"]
+ "</font>" + "<font color=blue> 在</font>" + System.DateTime.Now.ToLongTimeString()
+ "<font color=blue>" + " 进入了房间" + "</font>" + "<br/>";
Application.UnLock();
} } //每2秒刷新一次DIV content中的内容,并使其滚动条在最下面
protected void Timer1_Tick(object sender, EventArgs e)
{ //判断Application["Message"]变化,变化则更新
if(content.InnerHtml != Application["Message"].ToString())
{
content.InnerHtml = Application["Message"].ToString();
//滚动条滚动到最下面
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "layerMove();", true);
}
}
protected void btn_send_Click(object sender, EventArgs e)
{
//发送某人说的话
Application.Lock();
Application["Message"] += Session["UserName"] + " " + System.DateTime.Now.ToLongTimeString()
+ " :" + txt_Message.Text + "<br/>";
Application.UnLock();
//更新对话框
content.InnerHtml = Application["Message"].ToString();
//滚动条滚动到最下面 ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "layerMove();", true);
txt_Message.Text = ""; }
}
代码很简单,那我大哥帮我看下啊
<label><input type="checkbox" onclick="stop=this.checked">停止滚动</label><script type ="text/javascript">
var stop = false;
function layerMove() {
if(stop) return;
var div1 = document.getElementById("<%=content.ClientID %>");
div1.scrollTop = 5000;
}
</script>
否则人多服务器压力会很大的
如果有人发言,则滚动条滚动到下面。
参考博客 :http://http://blog.csdn.net/metababy/article/details/5960501
希望能帮到有相同问题的人。