怎么实现网页在线聊天?思路是怎么样? 类似网页QQ,能及时刷新消息,在下以前接触过JSP的在线聊天,用的是application,好像不实用!不知道哪位大侠接触过此类技术,麻烦给讲解一下!!!基于ASP.NET的大概讲一下思路!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一个是C/S,一个是B/S,还是不一样的. QQ是怎么通信的Q:我们上网一般使用的内网的IP地址,没有外网固IP,而UDP通信是需要IP地址的,那么既然不知道对方的IP地址,QQ是怎么实现两个点之间的通信的呢? A: 这个我最熟悉啦·UDP通信需要2个条件, 对方机器的IP地址,和接受消息的端口号. QQ 是怎么做到的呢? 其实很简单,我们下载的都是 QQ客户端, 腾讯保留着QQ服务端。并且这个服务端是24小时开启的, 那么我们好友之间是如何通信的呢? 这就要讲到QQ的通信模式了。 第一点:腾讯的QQ服务端必须开启,且服务器的IP地址是固定的,有固定的端口号接受客户端发送的消息。 第二点:客服端登录时,将自己的IP 和端口 发送给服务端,服务端记录,并将该IP和端口号发送给你所有的好友,群发消息告诉他们你已经上线。 第三点:好友间的聊天,其实这个就是点对点的了,不再通过服务器了。因为在步骤2 服务器已经将你的ip和端口发送给了你的好友,那么你的好友可以直接将消息发送到你的电脑。 第四点:如何知道好友已经离线呢?这是比较关键的,因为QQ使用的是UDP协议,UDP我们知道是不可靠连接,即只管发送消息,不管能否收到。 那么对方怎么知道你下线呢?在你下线时,会发送消息给服务器,服务器将会从上线列表里去除你的ID,并且群发消息告知你的好友。 网页QQ是用Silverlight做的 页面的话基本上是用ajax了当然你会自己做插件那就跟好了 聊天室页面每隔一定时间AJAX自动刷新一次,心跳机制powertalkboxhttp://topic.csdn.net/u/20090907/11/0932981B-DCB6-4425-BCBE-64186F0B6354.html 建个类,在全局里创建实例,类里搁个对象存提交来的对话。用几个webservice提供,对话对象内容的存取。页面上对ajax提交聊天信息,定时取信息。把这个类直接给你吧。Global.asax protected void Application_Start(object sender, EventArgs e) { Application["TheGlobalMsg"] = new TheGlobalMsgs(); } public class TheGlobalMsgs { const long timerDelay = 60000;//定时器过期时间60秒(1分钟内的短消息保留),客户端最长每30秒一取! Timer theTimer; public static DataTable theMsgsTable = null; public TheGlobalMsgs() { theMsgsTable = new DataTable("TheMsgTable"); //| 建立表, 表名:"Msgs" System.Data.DataColumn Column1 = new DataColumn("id", typeof(System.Int64)); //| 建立字段, 字段名:"id", 类型: int System.Data.DataColumn Column2 = new DataColumn("msgText", typeof(System.String)); System.Data.DataColumn Column3 = new DataColumn("GetUserID", typeof(System.String)); System.Data.DataColumn Column4 = new DataColumn("SendUserID", typeof(System.String)); System.Data.DataColumn Column5 = new DataColumn("SendUserName", typeof(System.String)); System.Data.DataColumn Column6 = new DataColumn("SendTime", typeof(System.DateTime)); Column1.AutoIncrement = true; //| 设置自增ID Column1.AutoIncrementSeed = 1; //| ID 起始值 Column1.AutoIncrementStep = 1; //| 递增量 theMsgsTable.Columns.Add(Column1); theMsgsTable.Columns.Add(Column2); theMsgsTable.Columns.Add(Column3); theMsgsTable.Columns.Add(Column4); theMsgsTable.Columns.Add(Column5); theMsgsTable.Columns.Add(Column6); //我想在内存中动态创建一个表 // 找到这么一段代码 请高手如何创建一个完整的表 包括创建字段,设置主键,设置字段类型 等等 theMsgsTable.PrimaryKey = new System.Data.DataColumn[] { Column1 }; TimerCallback OnTimerTick = new TimerCallback(TimerTick); theTimer = new Timer(OnTimerTick, null, timerDelay, timerDelay); } /// <summary> /// 定时期过期方法 删除表中的两分钟以前的过期值 /// </summary> /// <param name="state"></param> private void TimerTick(object state) { DataView theDV = new DataView(theMsgsTable); theDV.AllowDelete = true; theDV.AllowEdit = true; theDV.AllowNew = true; theDV.RowFilter = "SendTime < #" + DateTime.Now.AddMilliseconds(-120000).ToLongTimeString() + "#" ; for (int i = 0; i < theDV.Count; i++) { theDV.Delete(i); } theMsgsTable.AcceptChanges(); } /// <summary> /// 向内存表中添加一个消息 /// </summary> /// <param name="inp_SendUser_ID"></param> /// <param name="inp_SendUser_Name"></param> /// <param name="inp_GetUser_ID"></param> /// <param name="inp_TheMsgText"></param> /// <returns></returns> public bool AddMsg(string inp_SendUser_ID, string inp_SendUser_Name, string inp_GetUser_ID, string inp_TheMsgText) { if (inp_GetUser_ID == null || inp_GetUser_ID == "" || !DAL.DBBase.IsInt(inp_GetUser_ID) || inp_TheMsgText == null || inp_TheMsgText == "") { return false; } if (inp_SendUser_ID != null && inp_SendUser_ID != "") { if (!DAL.DBBase.IsInt(inp_SendUser_ID)) { return false; } } System.Data.DataRow theRow = theMsgsTable.NewRow(); //新行; //theRow[id] = ""; 行ID可以自加得到!!!! theRow["msgText"] = inp_TheMsgText; theRow["GetUserID"] = inp_GetUser_ID; theRow["SendUserID"] = inp_SendUser_ID; theRow["SendUserName"] = inp_SendUser_Name; theRow["SendTime"] = System.DateTime.Now; theMsgsTable.Rows.Add(theRow); // 将该行数据添加进表 theMsgsTable.AcceptChanges(); return true; } /// <summary> /// 得到发给某人的短消息 /// </summary> /// <param name="inp_GetUser_ID">某人的ID</param> /// <returns>DataTable</returns> public DataTable GetSomeBodyMsgs(string inp_GetUser_ID) { DataTable theBackTable = null; if (inp_GetUser_ID != null && inp_GetUser_ID != "" && DAL.DBBase.IsInt(inp_GetUser_ID)) { DataView theDV = new DataView(theMsgsTable); theDV.AllowDelete = true; theDV.AllowEdit = true; theDV.AllowNew = true; theDV.RowFilter = "GetUserID = " + inp_GetUser_ID; theDV.Sort = "SendTime"; theBackTable = theDV.ToTable();//从视图中取值 for (int i = 0; i < theDV.Count; i++) { theDV.Delete(i); } theMsgsTable.AcceptChanges(); } return theBackTable; } } webservice中得到某用户聊天信息return ((TheGlobalMsgs)Application["TheGlobalMsg"]).GetSomeBodyMsgs(USER_ID);添加一条聊天记录return ((TheGlobalMsgs)Application["TheGlobalMsg"]).AddMsg(USER_ID, TRUE_NAME, inp_GetUser_ID, inp_TheMsgText); [Quote=引用 12 楼 hwbox 的回复:]感谢各位特别谢谢 hwbox 大虾结贴给分了 错误:验证 (Internet Explorer 6): 属性“target”不是元素“frame”的有效属性 【100分】求帮忙。。。。。。。。。。。 帮忙写一条SQL 用fileupload上传图片如何获取原始路径 高分请教个问题 SQL2005找不到服务器 求助:我用WebBrowser做一个套打的功能。比如:见内容描述(注意:我是web开发) 如何在datagrid里动态加入textbox&drowdownlist等web控件?Thanks! 请教一个JAVASCRIPT的简单问题。 急,问个问题,急怎样判断控件首次加载? 关于二进制的一些问题 请问一个小问题?
Q:我们上网一般使用的内网的IP地址,没有外网固IP,而UDP通信是需要IP地址的,那么既然不知道对方的IP地址,QQ是怎么实现两个点之间的通信的呢?
A:
这个我最熟悉啦·UDP通信需要2个条件, 对方机器的IP地址,和接受消息的端口号.
QQ 是怎么做到的呢? 其实很简单,我们下载的都是 QQ客户端, 腾讯保留着QQ服务端。并且这个服务端是24小时开启的,
那么我们好友之间是如何通信的呢?
这就要讲到QQ的通信模式了。
第一点:腾讯的QQ服务端必须开启,且服务器的IP地址是固定的,有固定的端口号接受客户端发送的消息。
第二点:客服端登录时,将自己的IP 和端口 发送给服务端,服务端记录,并将该IP和端口号发送给你所有的好友,群发消息告诉他们你已经上线。
第三点:好友间的聊天,其实这个就是点对点的了,不再通过服务器了。因为在步骤2 服务器已经将你的ip和端口发送给了你的好友,那么你的好友可以直接将消息发送到你的电脑。
第四点:如何知道好友已经离线呢?这是比较关键的,因为QQ使用的是UDP协议,UDP我们知道是不可靠连接,即只管发送消息,不管能否收到。
那么对方怎么知道你下线呢?在你下线时,会发送消息给服务器,服务器将会从上线列表里去除你的ID,并且群发消息告知你的好友。
页面的话基本上是用ajax了
当然你会自己做插件那就跟好了
页面每隔一定时间AJAX自动刷新一次,心跳机制
powertalkbox
http://topic.csdn.net/u/20090907/11/0932981B-DCB6-4425-BCBE-64186F0B6354.html
{
Application["TheGlobalMsg"] = new TheGlobalMsgs();
}
public class TheGlobalMsgs
{
const long timerDelay = 60000;//定时器过期时间60秒(1分钟内的短消息保留),客户端最长每30秒一取! Timer theTimer;
public static DataTable theMsgsTable = null;
public TheGlobalMsgs()
{
theMsgsTable = new DataTable("TheMsgTable"); //| 建立表, 表名:"Msgs"
System.Data.DataColumn Column1 = new DataColumn("id", typeof(System.Int64)); //| 建立字段, 字段名:"id", 类型: int
System.Data.DataColumn Column2 = new DataColumn("msgText", typeof(System.String));
System.Data.DataColumn Column3 = new DataColumn("GetUserID", typeof(System.String));
System.Data.DataColumn Column4 = new DataColumn("SendUserID", typeof(System.String));
System.Data.DataColumn Column5 = new DataColumn("SendUserName", typeof(System.String));
System.Data.DataColumn Column6 = new DataColumn("SendTime", typeof(System.DateTime)); Column1.AutoIncrement = true; //| 设置自增ID
Column1.AutoIncrementSeed = 1; //| ID 起始值
Column1.AutoIncrementStep = 1; //| 递增量 theMsgsTable.Columns.Add(Column1);
theMsgsTable.Columns.Add(Column2);
theMsgsTable.Columns.Add(Column3);
theMsgsTable.Columns.Add(Column4);
theMsgsTable.Columns.Add(Column5);
theMsgsTable.Columns.Add(Column6);
//我想在内存中动态创建一个表
// 找到这么一段代码 请高手如何创建一个完整的表 包括创建字段,设置主键,设置字段类型 等等
theMsgsTable.PrimaryKey = new System.Data.DataColumn[] { Column1 }; TimerCallback OnTimerTick = new TimerCallback(TimerTick);
theTimer = new Timer(OnTimerTick, null, timerDelay, timerDelay); } /// <summary>
/// 定时期过期方法 删除表中的两分钟以前的过期值
/// </summary>
/// <param name="state"></param>
private void TimerTick(object state)
{
DataView theDV = new DataView(theMsgsTable);
theDV.AllowDelete = true;
theDV.AllowEdit = true;
theDV.AllowNew = true;
theDV.RowFilter = "SendTime < #" + DateTime.Now.AddMilliseconds(-120000).ToLongTimeString() + "#" ;
for (int i = 0; i < theDV.Count; i++)
{
theDV.Delete(i);
}
theMsgsTable.AcceptChanges();
} /// <summary>
/// 向内存表中添加一个消息
/// </summary>
/// <param name="inp_SendUser_ID"></param>
/// <param name="inp_SendUser_Name"></param>
/// <param name="inp_GetUser_ID"></param>
/// <param name="inp_TheMsgText"></param>
/// <returns></returns>
public bool AddMsg(string inp_SendUser_ID, string inp_SendUser_Name, string inp_GetUser_ID, string inp_TheMsgText)
{
if (inp_GetUser_ID == null || inp_GetUser_ID == "" || !DAL.DBBase.IsInt(inp_GetUser_ID) || inp_TheMsgText == null || inp_TheMsgText == "")
{
return false;
}
if (inp_SendUser_ID != null && inp_SendUser_ID != "")
{
if (!DAL.DBBase.IsInt(inp_SendUser_ID))
{
return false;
}
} System.Data.DataRow theRow = theMsgsTable.NewRow(); //新行;
//theRow[id] = ""; 行ID可以自加得到!!!!
theRow["msgText"] = inp_TheMsgText;
theRow["GetUserID"] = inp_GetUser_ID;
theRow["SendUserID"] = inp_SendUser_ID;
theRow["SendUserName"] = inp_SendUser_Name;
theRow["SendTime"] = System.DateTime.Now;
theMsgsTable.Rows.Add(theRow); // 将该行数据添加进表
theMsgsTable.AcceptChanges();
return true;
} /// <summary>
/// 得到发给某人的短消息
/// </summary>
/// <param name="inp_GetUser_ID">某人的ID</param>
/// <returns>DataTable</returns>
public DataTable GetSomeBodyMsgs(string inp_GetUser_ID)
{
DataTable theBackTable = null;
if (inp_GetUser_ID != null && inp_GetUser_ID != "" && DAL.DBBase.IsInt(inp_GetUser_ID))
{
DataView theDV = new DataView(theMsgsTable);
theDV.AllowDelete = true;
theDV.AllowEdit = true;
theDV.AllowNew = true;
theDV.RowFilter = "GetUserID = " + inp_GetUser_ID;
theDV.Sort = "SendTime";
theBackTable = theDV.ToTable();//从视图中取值
for (int i = 0; i < theDV.Count; i++)
{
theDV.Delete(i);
}
theMsgsTable.AcceptChanges();
}
return theBackTable;
}
}
return ((TheGlobalMsgs)Application["TheGlobalMsg"]).GetSomeBodyMsgs(USER_ID);添加一条聊天记录
return ((TheGlobalMsgs)Application["TheGlobalMsg"]).AddMsg(USER_ID, TRUE_NAME, inp_GetUser_ID, inp_TheMsgText);
感谢各位
特别谢谢 hwbox 大虾
结贴给分了