18.8 联系人管理
聊天室中的“好友”、“黑名单”、“陌生人”、“群”等概念是联系人的管理范畴。联系人管理中最主要的部分是对好友管理,而好友管理中较复杂的部分则是添加好友。本节以添加好友为重点,详细介绍联系人管理的相关内容。
18.8.1 寻找好友
前面章节已经介绍了添加好友操作的大致流程,这里主要讲述其具体实现。在QQ菜单中的“我的好友”菜单项中有一个“添加好友“的按钮,单击该按钮将执行find1()方法。find1()方法的作用是在div层中打开find1.aspx页面。其实现代码如下:
//添加好友第一步,选择浏览方式
function find1() {
var div = top.document.createElement("div");
div.id = "sear";//id
var html =' <table width="300" height="295" id ="myfind" border="0" cellspacing= "0" cellpadding="0">\
<tr >\
<td><iframe frameborder=0 scrolling=no width=400 height=295 '; //构造字符串
html +='src=\"find1.aspx';
html += '\"></iframe></td> </tr></table>'; //构造完毕
div.innerHTML = html;
div.style.position = "absolute"; //绝对位置
div.style.left = 200+"px";
div.style.top = 150+"px";
//鼠标键按下允许事件冒泡
div.onmousedown = function() { window.event.cancelBubble=false;};
top.document.body.appendChild(div); //添加上这个窗体
}
执行find1()方法后进入添加好友第一步—— 选择查找方式,显示查找好友方式的选择页面,此页面的显示效果如图18.19所示。
页面上包括3个选项:看谁在线上、用户账号、群的名称。3个选项的后续执行步骤大致相同。这里选择第一个选项看谁在线上为例子,单击“下一步”按钮,页面相关数据被POST到Find2.aspx页面中。Find2.aspx是用来显示用户列表和执行添加好友操作的。执行Find2.aspx文件后,进入添加好友第二步——用户列表,在显示出来的用户列表中,单击选中某条好友记录,便可查看他的详细信息,也可以向他发出加为好友的请求。Find2.aspx的显示效果如图18.20所示。

图18.19 添加好友第一步—— 选择查找方式 图18.20 添加好友第二步——用户列表
Find2.aspx的代码构成主体是一个GridView控件,此控件用来显示好友列表。页面中利用一个ID为dnmb的hidden控件,记录当前选中的用户ID。Find2.aspx的页面代码实现如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="find2.aspx.cs" Inherits= "find2" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查找结果</title>
<link href="images/style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="js/tools.js"></script>
<script language="JavaScript">
function trselect(){ //选中某一条记录
var id=form1.dnmb.value;
if(id!="") $("a"+id).className='';
form1.more.disabled=false; //查看详情按钮可用
form1.add.disabled=false; //添加好友按钮可用
}
var npage=1;
var pageUrl=window.location;
pageUrl=pageUrl.toString();
var Parameter=pageUrl.split("?");
var scurl=Parameter[1];
function finddetail() //查看详情
{
if(document.form1.dnmb.value=='')//选中用户
{top.frames('chatqq').myAlert('请先选择一个用户!');return;}
top.frames('chatqq').getdetail($('dnmb').value)
}
</script>
</head>
<body topmargin="0" leftmargin="0" style="border: none" background="images/ 4_r3_c4.gif"
scroll="no" oncontextmenu="return false" onselectstart="return false">
<form id="form1" runat="server">
<table border="1" cellspacing="0" bordercolor="#D6D3CE" background="images/ 4_r3_c4.gif"
style="width: 398px; height: 274px">
<tr>
<td align="center" class="but" style="width: 436px">
<table width="100%" height="205" style="overflow: auto" border="0" cellpadding="1"
cellspacing="0" class="tddown" background="images/4_r3_c4.gif">
<tr class="but">
<td width="100" height="18" align="center" class="but" onmousedown="this.className='tddown'"
onmouseup="this.className='but'" onmouseout="this.className='but'">
账号</td>
<td width="200" height="18" align="center" class="but" onmousedown="this.className='tddown'"
onmouseup="this.className='but'" onmouseout="this.class Name='but'">
姓名</td>
<td height="18" align="center" class="but" onmousedown= "this.className='tddown'"
onmouseup="this.className='but'" onmouseout="this. className= 'but'" style="width: 30px">
性别</td>
<td width="100" height="18" align="center" class="but" onmousedown="this.className='tddown'"
onmouseup="this.className='but'" onmouseout="this.class Name='but'">
来自何处</td>
</tr>
<tr valign="top" bgcolor="#FFFFFF">
<td colspan="5" align="center" style="width: 416px">
<div id="search1">
<asp:GridView ID="GridView1" runat="server" Width= "100%" GridLines="None" AllowPaging="True"
AutoGenerateColumns="False" ShowHeader="False" OnRowCreated="GridView1_RowCreated"
DataKeyNames="userid">
<PagerSettings Visible="False" />
<Columns>
<asp:BoundField DataField="username">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="realname">
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="sex">
<ItemStyle Width="18px" />
</asp:BoundField>
<asp:BoundField DataField="companyaddr">
<ItemStyle Width="100px" />
</asp:BoundField>
</Columns>
<EmptyDataTemplate>
对不起,没有找到符合条件的用户!
</EmptyDataTemplate>
</asp:GridView>
</div>
</td>
</tr>
</table>
<div id="search2">
</div>
</td>
</tr>
<tr>
<td align="center" class="but" style="width: 436px">
<a id="up1" runat="server">
<input name="button_1" type="button" class="button1" value= " 上一页 "></a> <a id="down1"
runat="server">
<input name="button_2" type="button" class="button1 " value=" 下一页 "></a>
<input name="more" type="button" disabled class="button1" onclick= "finddetail();"
value="详细资料">
<input name="add" type="button" disabled class="button1" value= "加为好友" id="add" onserverclick="Button1_ServerClick"
runat="server">
<input name="dnmb" type="hidden" id="dnmb">
</td>
</tr>
<tr>
<td align="center" bgcolor="#DCEAFA" style="width: 436px; text-align: right;">
<input name="Button" type="button" class="button1" value=" 上一步 " onclick="window.history.go(-1);">
<input name="button" type="button" class="button1" onclick= "top.frames('chatqq').closeInfoWnd('sear');"
value=" 关 闭 "></td>
</tr>
</table>
</form>
</body>
</html>
18.8.2 查看待添加好友的资料
由上节的代码可以看出,“查看详细资料”按钮调用了finddetail()方法来显示选中用户的详细资料。而finddetail()方法又调用了getdetail()方法来具体实现用户资料的显示。这两个函数的具体实现代码如下:
function finddetail()
{
if(document.form1.dnmb.value=='')
{top.frames('chatqq').myAlert('请先选择一个用户!');return;}
top.frames('chatqq').getdetail($('dnmb').value)
}
//显示用户的详细资料
function getdetail(userid,username)
{
var div = top.document.createElement("div");
div.id = "Sysmsg";
//字符串
var html =' <table width="530" height="450" border="0" cellspacing="0" cellpadding="0">\
<tr><td ><iframe frameborder=0 scrolling=no id = mydetail width=530 height=450 ';
if(arguments.length==1) //显示特定人的详情
html +='src=\"detail.aspx?userid='+userid;
if(arguments.length==0) //显示自己的详情
html +='src=\"detail.aspx';
if(arguments.length==2) //显示黑名单中的某人的详情
html +='src=\"detail.aspx?type = black&userid = '+userid+"&username =" +escape (username);
html += '\"></iframe></td> </tr></table>';
//设置一些参数
div.innerHTML = html;
div.style.position = "absolute";
div.style.left = 300+"px";
div.style.top = 100+"px";
div.style.width=530+"px";
div.style.height= 450+"px";
div.onmousedown = function() { top.event.cancelBubble=false;};
top.document.body.appendChild(div);
Drag.init(div, div); //初始化拖动
}
服务器端Find2.aspx.cs文件负责产生相应的数据源,供GridView控件显示信息使用。同时也执行添加好友的动作。在其Page_Load事件中,首先判断是请求显示在线用户、特定用户还是群,然后根据不同情况进行不同的处理。在GridView1_RowCreated事件中,则给GridView控件注册了一个客户端onClick事件,用来记录选中的用户ID。Find2.aspx.cs的具体代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyChat;
public partial class find2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDictionaryEnumerator id = Cache.GetEnumerator();
MyChat.Sql s = new MyChat.Sql();
if(Request["radiobutton"] == "0") //在线用户
{
string onlineuser = "0";
while (id.MoveNext()) //遍历cache,寻找聊天用户类
{
MyChat.ChatUser o = id.Value as MyChat.ChatUser;
if(o!= null)
onlineuser += "," + o.Userid;//记录下在线用户的id
}
//从数据库中取出所有在线用户的信息
System.Data.DataSet ds = s.getMyDataSet("select * from userinfo
where userid in (" + onlineuser + ")");
GridView1.DataSource = ds;
}
if (Request["radiobutton"] == "1") //特定用户
{
System.Data.DataSet ds = s.getMyDataSet("select * from userinfo
where username like '%" + Request.Form["snumber"] + "%'");
GridView1.DataSource = ds; //把所有用户的信息作为数据源
}
if(Request["radiobutton"] == "2") //查找群
{
Server.Transfer("find3.aspx"); //转到群浏览页面
return;
}
//如果指定页面,显示指定的页面
if(Request.QueryString["page"] != null && Request.QueryString ["page"] != "")
GridView1.PageIndex = int.Parse(Request.QueryString["page"]);
GridView1.DataBind();
Random r = new Random();
if(GridView1.PageIndex + 2 <= GridView1.PageCount)//设置上一页按钮的链接
up1.HRef = "find2.aspx?page=" + Convert.ToString(GridView1. PageIndex + 1) + "&rnd = " + Convert.ToString(r.Next());
if (GridView1.PageIndex > 0) //设置下一页按钮的链接
down1.HRef = "find2.aspx?page=" + Convert.ToString(GridView1. PageIndex - 1) + "&rnd = " + Convert.ToString(r.Next());
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow) //数据字段
{
string tem = GridView1.DataKeys[e.Row.RowIndex][0].ToString();
//用户id
string j = "trselect();this.className='select';form1.dnmb.value='" + tem + "';";
e.Row.Attributes.Add("id", "a" + tem); //id
e.Row.Attributes.Add("onclick", j); //onclick属性
}
}
//加为好友
protected void Button1_ServerClick(object sender, EventArgs e)
{
improve im = new improve();
string str = im.AddFriend(Request["dnmb"]); //请求加为好友
switch (str)
{
case "success": //成功
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top. frames('chatqq').myAlert('成功加为好友!!');", true);
break;
case "isblack": //在黑名单中
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top. frames('chatqq').myAlert('你在对方黑名单中,不能加为好友!!');", true);
break;
case "havefriend": //已经是好友
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top. frames('chatqq').myAlert('您已经添加了该好友,不能再次添加!!');", true);
break;
case "forbid": //禁止被加为好友
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top.frames('chatqq').myAlert('对方拒绝被加为好友,不能添加!!');", true);
break;
case "confirm": //需要确认
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top.frames ('chatqq').showconfirm('" + Request["dnmb"] + "');", true);
break;
}
}
}
18.8.3 添加好友完成
在图18.20中,单击“加为好友”按钮,可能出现的反馈情况包括:对方拒绝添加、添加失败、添加成功、出现好友验证窗体、用户验证等。其中第3种情况比较复杂,单击“加为好友”按钮后,进入添加好友第三步操步——输入验证信息,即出现好友要求验证窗体,如图18.21所示。
从Find2.aspx.cs文件代码中可以看到,这个验证窗体的出现是直接调用客户端函数showcon- firm()的结果。showconfirm()构造了一个简易的显示层,供用户输入验证信息进行验证。Showcon- firm()方法的实现代码如下:
// 显示请求加为好友的验证窗口
function showconfirm(friendid) {
var div = top.document.createElement("div");
div.id = "friendwnd"; //id
var html = "<img src=\"images/c.gif\" onclick=\"top.frames('chatqq').close InfoWnd('friendwnd')\">\
<div >\
<table><tr><td colspan=2>对方需要身份验证才能加为好友,请输入附加消息</td></tr>\
<tr><td valign=\"top\"><span>附加消息:</span></td>\
<td><textarea name=\"content\" rows=\"\" cols=\"\">可以作个朋友么?</textarea></td></tr>\
<tr><td> </td><td>\
</td>\
</tr>\
<tr><td></td><td><input type=\"button\" value=\"发送\" onclick =\"var va=document.getElementById('content').value;top.frames('chatqq').sendf(" + friendid+ ",va);\"";
html += " class='button1' > <input type=\"reset\" value=\"关闭\" class='button1' onclick=\"top.frames("chatqq").closeInfoWnd('friendwnd')\"></td></tr>\
</table>\
</div>"; //构造窗体内容
div.innerHTML = html;
div.style.position = "absolute";
div.style.left =260+"px";
div.style.backgroundImage="url(images/4_r3_c4.gif)"; //背景
div.style.top = 190+"px";
div.style.height = 100+"px";
div.style.textAlign="right";
div.onmousedown = function() { top.event.cancelBubble=false;};
top.document.body.appendChild(div); //添加上窗口
}
用户输入验证信息,程序调用sendf()方法请求服务器进行验证。Sendf()方法其实就是直接利用客户端Request对象向服务器发送一个请求,然后对请求的结果进行相应的显示。相关的客户端代码如下:
//发送加为好友的请求
function sendf(toid,content) {
//请求的附加消息
url = "askforf.ashx?msg="+escape(content)+"&toid="+toid+"&rnd="+ 10000* Math.random();
Request.sendPOST(url, "", doSendMessageRes, true, doSendMessageFail); //发送请求
}
//成功后的回调函数
function doSendMessageRes(req, data) {
myAlert("操作成功");
top.frames('chatqq').closeInfoWnd('friendwnd');
}
//失败后的回调函数
function doSendMessageFail(req, data) {
myAlert("操作失败");
top.frames('chatqq').closeInfoWnd('friendwnd');
}
服务器端处理好友验证请求的程序是askforf.ashx。它的作用是向被请求者发送一条系统消息,询问用户是否同意被加为好友。代码如下:
<%@ WebHandler Language="C#" Class="askforf" %>
using System;
using System.Web;
using MyChat;
public class askforf : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; //文本类型
string msg = context.Server.UrlDecode(context.Request.QueryString["msg"]); //消息
string toid = context.Request.QueryString["toid"]; //对方id
improve im = new improve();
im.AskForAddF(toid, msg); //请求加为好友
}
public bool IsReusable
{
get
{
return false;
}
}
}
被请求者将收到如图18.22所示的一条系统消息。进入添加好友第四步—— 收到被请求加为好友的信息,用户可以单击“同意”链接接受请求,也可以单击“不同意”链接拒绝请求。

图18.22 添加好友第四步—— 收到被请求加为好友的信息
若单击“同意”链接后,程序将调用allowf.ashx把自己加为对方的好友,同时向对方发送如图18.23所示的一条系统消息,进入添加好友第五步—— 添加成功,这样添加好友的过程结束。allowf.ashx的代码如下:

图18.23 添加好友第五步—— 添加成功
<%@ WebHandler Language="C#" Class="allowf" %>
using System;
using System.Web;
using MyChat;
public class allowf : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; //设置输出格式为文本
//请求者的账号
string username = context.Server.UrlDecode(context.Request.QueryString ["friendname"]); improve im = new improve();
//同意请求
if(context.Request.QueryString["type"] == "1")
{
im.PermitAddFriend(username);
}
//不同意被加为好友
if(context.Request.QueryString["type"] == "2")
{
im.RejectAddFriend(username);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
18.8.4 删除好友和加入、移除黑名单
在两人聊天窗口(chat.aspx)的右上部有两个按钮:“删除该好友”和“拉入黑名单”,这两个按钮用来实现删除好友和加入黑名单的功能。两个按钮的运行时效果如图18.24所示。
两个按钮执行的程序都是服务器端控件,即直接执行服务器端代码。代码的主体都是执行对数据库的操作,程序执行后给客户端一个提示信息框。具体实现代码如下:
//删除好友
protected void Button1_Click(object sender, EventArgs e)
{
string gid = Request.QueryString["othername"]; //要删除的好友账号
string sql = " delete [friend] where userid = '" + tools.GetUserId (User.Identity.Name) + "' and othername='" + gid + "'";
MyChat.Sql s = new MyChat.Sql();
s.ExecuteSql(sql); //删除
//显示删除的提示
ClientScript.RegisterStartupScript(this.GetType(), "sde", "clearInterval (isdog);top.frames('chatqq').myAlert('您已经成功删除该好友!!');var myt = document.get ElementById('myte').value;top.frames('chatqq').closeInfoWnd(myt);", true);
}
//拉黑名单
protected void Button2_Click(object sender, EventArgs e)
{
string gid = Request.QueryString["othername"]; //对方账号
string sql = " delete [friend] where userid = '" + User.Identity.Name + "' and othername='" + gid + "'";
MyChat.Sql s = new MyChat.Sql();
s.ExecuteSql(sql); //删除好友设置
sql = "insert [friend](type,userid,othername) values(1," + tools.GetUserId (User.Identity.Name) + ",'" + gid + "')";
s.ExecuteSql(sql); //拉入黑名单
//显示操作成功的提示
ClientScript.RegisterStartupScript(this.GetType(),"clearInterval (isdog); top.frames('chatqq').myAlert('您已经把该好友拉入了黑名单!!');var myt = document. getElementById('myte').value;top.frames('chatqq').closeInfoWnd(myt);", true);
}
单击QQ菜单中黑名单菜单项中的某个用户,就会显示这个用户的详细信息。与一般用户的详细信息不同的是,这个页面增加了一个按钮“移除黑名单”,用来实现将此用户移除黑名单的功能。显示效果如图18.25所示。
![]()
图18.25 移除黑名单按钮
实现这个功能就是要把数据库中的黑名单记录删除掉,然后给客户端一个相应的提示信息。实现代码如下:
// 删除黑名单
protected void delblack_ServerClick(object sender, EventArgs e)
{
string gid = Request.QueryString["username"]; //账号
MyChat.Sql s = new MyChat.Sql();
string sql = "delete from friend where userid =" + tools.GetUserId (User.Identity.Name) + " and type =1 and othername= '" + gid + "' ";
s.ExecuteSql(sql); //删除
//删除成功的提示
ClientScript.RegisterStartupScript(this.GetType(), "sde", "top.frames('chatqq'). myAlert('您已经成功把该用户移除黑名单!!');top.frames('chatqq'). closeInfoWnd('myt');", true);
}
18.8.5 群
群聊天的功能大致与聊天室相似,群可以自由创建,在群中可以实现多人聊天。下面分别讲述创建群和群的其他操作两方面功能的具体实现。
1.创建群
在Web QQ主界面的“我的群”菜单项中,单击“创建群”图标,此时执行createGroupWnd()方法,显示出创建群的界面。createGroupWnd()方法用于构造一个群的界面,供用户输入群的一些设置信息,代码如下:
// 显示创建群的窗口
function createGroupWnd()
{
var div = top.document.createElement("div");
div.id = "friendwnd";
//构造字符串
var html = "<iframe id = 'friendmy' frameborder=0 style ='display:none' scrolling= no src=\"about:blank\"></iframe><img src=\"images/c.gif\" onclick=\"top.frames ('chatqq').closeInfoWnd('friendwnd');\">\
<div class=\"winTitle\" align='left'><br /> <form name=\"sendMsgForm\" ><span align='left'>群的名称:<input type=\"text\" id=\"gname\" name =\"gname\" ></input> </span> </div>\
<div class=\"winCont\" id=\"sendMsgBoxCont\">\
<table class=\"t_l\">\
<tr><td valign=\"top\">群描述:</td>\
<td><textarea name=\"gnotes\" id =\"gnotes\" rows=\"\" cols=\"\" class=\"tArea1\"></textarea></td></tr>\
<tr><td> </td><td>\
</td>\
</tr>\
<tr><td></td><td><input type=\"button\" value=\"创建\" onclick =\"top.frames ('chatqq').createG(\document.getElementById('gname').value,document.getElementById('gnotes').value); \" class='button1' > <input type=\"reset\" value=\"关闭\" class='button1' onclick=\"top.frames('chatqq').closeInfoWnd('friendwnd')\"></td></tr>\
</form></table>\
</div>";
div.innerHTML = html;
//设置一些必要属性
div.style.position = "absolute";
div.style.left =460+"px";
div.style.backgroundImage="url(images/4_r3_c4.gif)";
div.style.top = 190+"px";
div.style.height = 100+"px";
div.style.textAlign="right";
div.onmousedown = function() { top.event.cancelBubble=false;};
top.document.body.appendChild(div);
}
执行createGroupWnd()方法后,显示如图18.26所示的创建群的界面。
在两个文本框中分别输入群名称和群描述,单击“创建”按钮,依次执行客户端脚本方法createG()和调用服务器端createg.ashx程序完成创建群的操作。createG()和createg.ashx的代码如下:
//客户端请求创建一个群
function createG(gname,gnotes)
{
//构造url字符串
url = "createg.ashx?rnd="+10000*Math.random()+"&gname="+escape(gname)+"&gnotes= "+escape(gnotes);
//发出请求
Request.sendPOST(url, "", doSendMessageRes,true, doSendMessageFail);
//加载的提示
top.document.getElementById("friendwnd").innerHTML = "<br /><br /><br />正在提交请求,请稍后……";
}
//服务器端创建群(createg.ashx)的代码
<%@ WebHandler Language="C#" Class="createg" %>
using System;
using System.Web;
using MyChat;
public class createg : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string name = context.Server.UrlDecode(context.Request.QueryString["gname"]); //群名称
string notes = context.Server.UrlDecode(context.Request.QueryString["gnotes"]); //群介绍
string myname = System.Web.HttpContext.Current.User.Identity.Name
;//用户名
MyChat.Sql s = new MyChat.Sql();
//数据库中建一个群
s.ExecuteSql("insert [group] (name,ownername,notes) values ('" + name +
"','" + myname + "','" + notes + "')");
System.Data.SqlClient.SqlDataReader r = s.GetReader("select top(1)
id,[datetime] from [group] where ownername='" + myname + "' order by
[datetime] desc");
string gid = "0"; //得到创建那个群的id
if (r.Read()) gid = r["id"].ToString();
r.Close();
//群中插入用户
string sql = " insert [group_user](groupid,username) values (" + gid + ",'"
+ myname + "')";
s.ExecuteSql(sql);
}
public bool IsReusable
{
get
{
return false;
}
}
}
2.群的其他操作
在群聊天窗口(groupchat.aspx)的右下角有两个选项:邀请好友加入和退出该圈。运行效果如图18.27所示。当单击“邀请好友加入”链接时,程序调用客户端脚本方法createinvite Wnd(),该方法运行后显示如图18.28所示的一个信息输入框。

图18.27 群聊天窗口的右下角 图18.28 邀请好友加入群的界面
其中createinviteWnd()方法是用来构造一个信息输入窗体的。代码如下:
// 显示邀请的窗口
function createInviteWnd()
{
var div = top.document.createElement("div");
div.id = "dwnd";
//字符串
var html = "<iframe id = 'friendmy' frameborder=0 style ='display:none' scrolling=no src=\"about:blank\"></iframe><img src=\"images/c.gif\" onclick=\"top. frames('chatqq').closeInfoWnd('dwnd');\">\
<div class=\"winTitle\" align='left'><br /> <form name=\"sendMsgForm\" ><span align='left'>被邀请者<input type=\"text\" id=\"gname\" name =\"gname\" ></input> </span> </div>\
<div class=\"winCont\" id=\"sendMsgBoxCont\">\
<table class=\"t_l\">\
<tr><td valign=\"top\">消息:</td>\
<td><textarea name=\"gnotes\" id =\"gnotes\" rows=\"\" cols=\"\" class=\"tArea1\"></textarea></td></tr>\
<tr><td> </td><td>\
</td>\
</tr>\
<tr><td></td><td><input type=\"button\" value=\"邀请\" onclick ='invite(";
html += "document.getElementById(\"gname\").value,document.getElementById (\"gnotes\").value);' class='button1' > <input type=\"reset\" value=\"关闭\" class='button1' onclick=\"top.frames('chatqq').closeInfoWnd('dwnd')\"></td></tr>\
</form></table>\
</div>";
div.innerHTML = html;
//设置属性
div.style.position = "absolute";
div.style.left =360+"px";
div.style.backgroundImage="url(images/4_r3_c4.gif)";
div.style.top = 190+"px";
div.style.height = 150+"px";
div.style.textAlign="right"; //右对齐
div.onmousedown = function() { top.event.cancelBubble=false;};
top.document.body.appendChild(div);
}
在构造的信息输入窗体中输入被邀请者的账号和邀请消息后,单击“邀请”按钮,依次执行客户端方法invite()和调用服务器端invite.ashx文件完成邀请操作。邀请操作的实质就是向被邀请者发送一条消息,邀请其加入这个群。客户端方法invite()和服务器端文件invite.ashx的代码如下:
//邀请某人加入群
function invite(gname,gnotes)
{
//构造url
url = "invite. ashx?rnd=" +10000*Math. random()+"&gname= "+escape(gname)+" &gnotes= "+escape(gnotes);
//发出请求
Request.sendPOST(url, "", doSendMessageRes,true, doSendMessageFail);
}
//invite.ashx的代码
<%@ WebHandler Language="C#" Class="invite" %>
using System;
using System.Web;
using MyChat;
public class invite : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string name = context.Server.UrlDecode(context.Request.QueryString["gname"]);
//被邀请者账号
string notes = context.Server.UrlDecode(context.Request.QueryString ["gnotes"]);
//邀请内容
improve im = new improve();
im.SendMsg(context, name, notes); //发出邀请
}
public bool IsReusable {
get{
return false;
}
}
}
“退出群”的操作比较简单。“退出群”执行的是一个服务器端按钮控件,单击该按钮后执行如下代码。在代码中,首先在群用户表中删除自身的这条记录,然后显示一些操作成功的客户端提示信息。
protected void Button1_Click(object sender, EventArgs e)
{
string gid = Request.QueryString["groupid"]; //群id
string sql = " delete [group_user] where username = '" + User.Identity.Name + "' and groupid=" + gid;
MyChat.Sql s = new MyChat.Sql();
s.ExecuteSql(sql); //自己退出这个群
//客户端提示操作成功
ClientScript.RegisterStartupScript(this.GetType(), "sde", "clearInterval (isdog);top.frames('chatqq').myAlert('您已经成功退出该群!!');var myt = document.get ElementById('myte').value;top.frames('chatqq').closeInfoWnd(myt);", true);
}






