当前位置:
首页 > 编程开发 > Objective-C编程 >
-
无刷新的聊天室的制作兼谈组件制作和ClientSide Script(一)
我们在传统的web程序当中比较头疼的一件事是屏幕的刷新感。虽然有server push的技术,但在IE中较难实现。现在webservice给了我们这样一个机会,大家都知道webservice是基于soap的,而soap是xml的应用,如果你曾经用过ms xml sdk3.0的话就会知道里面有个xmlhttp方法,其实在那时我们就已经可以用xmlhttp的方式替代Form了,也是无刷新的,其实准确地说是局部刷新,下面我们来看一下怎样做,先做一个chat webservice, 首先来分析一下,一个聊天室应具备的两个要素人和消息,这样我们可以建立一个类型(记得我在以前说过类也是类型),它包含这样两个要素。
///ChatMessage.cs
using System;
namespace chat
{
/// <summary>
/// ChatMessage类封装了两个string变量:UserLists--用户列表,Messages--要传递的信息
/// </summary>
public class ChatMessage
{
public string UserList, Messages;
}
}
第二个我们要建立的是什么呢?一个聊天室应能存储在线成员的名字及访问时间
///Member.cs
using System;
namespace chat
{
/// <summary>
/// Member类为每个聊天者封装了Server端的变量
/// </summary>
public class Member
{
// 存储消息的队列
public string UserName, MsgQueue;
// 判断滞留事件以便踢人
public System.DateTime LastAccessTime;
// The constructor
public Member(string NickName)
{
this.UserName=NickName;
this.LastAccessTime=DateTime.Now;
}
}
}
接下来我们就应该做这个asmx了
///ChatWebService.asmx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace chat
{
/// <summary>
/// Summary description for ChatWebService.
/// </summary>
[WebService (Namespace = "http://localhost/chat/", Description = "This service provides an chat service")]
public class ChatWebService : System.Web.Services.WebService
{
public ChatWebService()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
}
[WebMethod(Description="接收用户名作为参数存储到Application对象中")]
public string Login(string username)
{
// Ascertain that all the registered chat participants are active
CheckMembersList();
// Synchronization Lock
Application.Lock();
// Get the collection of keys for the Application Variables
String[] Members = Application.AllKeys;
// Are there any registered chat members? & the present request is for a unique nick name?
if ((Members.Length>0)&&(Array.IndexOf(Members,username)>-1))
{
throw new Exception("该用户已存在!");
}
// Create a new Member object for this participant
Member NewMember = new Member(username);
// Add this new member to the collectionof Application Level Variables
Application.Add(username, NewMember);
// Synchronization unlock
Application.UnLock();
// Go and get the list of current chat participants and retrun the list
return GetMembersList();
}
[WebMethod(Description="GetMsg方法用用户名和消息为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
public ChatMessage XchangeMsgs(string username, string Msg)
{
// Ascertain that all the registered chat participants are active
CheckMembersList();
// Synchronization Lock
Application.Lock();
// Get the collection of keys for the Application Variables
String[] Members = Application.AllKeys;
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
// Are there any registered chat members? & the present request is for a unique nick name?
{
throw new Exception("你当前可能没有登陆或登陆超时,请重新登陆!");
}
ChatMessage RetMsg = new ChatMessage();
RetMsg.UserList = GetMembersList();
// Loop through all the Chat Participant's serverside Member Objects and
// add the message just received in their waiting message queue
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member)Application[Members[x]];
temp.MsgQueue+=("<BR><Font color = Red>" + username + " 说:<BR></FONT><Font color = Blue>" + Msg);
if (temp.UserName == username)
{
RetMsg.Messages = temp.MsgQueue;
temp.MsgQueue="";
temp.LastAccessTime=DateTime.Now;
}
}
// Synchronization unlock
Application.UnLock();
return RetMsg;
}
[WebMethod(Description="GetMsg方法用username为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
public ChatMessage GetMsgs(string username)
{
Application.Lock();
CheckMembersList();
Application.Lock();
String[] Members = Application.AllKeys;
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
{
throw new Exception("Unknown User. Please Login with a UserName");
}
ChatMessage RetMsg = new ChatMessage();
RetMsg.UserList = GetMembersList();
Member temp = (Member)Application[username];
RetMsg.Messages = temp.MsgQueue;
temp.MsgQueue="";
temp.LastAccessTime=DateTime.Now;
Application.UnLock();
return RetMsg;
}
public string GetMembersList()
{
Application.Lock();
String UserList = "";
String[] Members = Application.AllKeys;
Application.UnLock();
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member)Application[Members[x]];
UserList += (temp.UserName+"\n");
}
return UserList;
}
private void CheckMembersList()
{
String[] Members = Application.AllKeys;
ArrayList RemoveList = new ArrayList();
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member) Application[Members[x]];
int test = (DateTime.Now.Subtract(temp.LastAccessTime)).Minutes;
if (test > 2)
{
RemoveList.Add(Members[x]);
}
}
// Users = null;
for (int count = 0;count<RemoveList.Count;count++)
{
Application.Remove((String)RemoveList[count]);
}
return;
}
}
}
///ChatMessage.cs
using System;
namespace chat
{
/// <summary>
/// ChatMessage类封装了两个string变量:UserLists--用户列表,Messages--要传递的信息
/// </summary>
public class ChatMessage
{
public string UserList, Messages;
}
}
第二个我们要建立的是什么呢?一个聊天室应能存储在线成员的名字及访问时间
///Member.cs
using System;
namespace chat
{
/// <summary>
/// Member类为每个聊天者封装了Server端的变量
/// </summary>
public class Member
{
// 存储消息的队列
public string UserName, MsgQueue;
// 判断滞留事件以便踢人
public System.DateTime LastAccessTime;
// The constructor
public Member(string NickName)
{
this.UserName=NickName;
this.LastAccessTime=DateTime.Now;
}
}
}
接下来我们就应该做这个asmx了
///ChatWebService.asmx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace chat
{
/// <summary>
/// Summary description for ChatWebService.
/// </summary>
[WebService (Namespace = "http://localhost/chat/", Description = "This service provides an chat service")]
public class ChatWebService : System.Web.Services.WebService
{
public ChatWebService()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
}
[WebMethod(Description="接收用户名作为参数存储到Application对象中")]
public string Login(string username)
{
// Ascertain that all the registered chat participants are active
CheckMembersList();
// Synchronization Lock
Application.Lock();
// Get the collection of keys for the Application Variables
String[] Members = Application.AllKeys;
// Are there any registered chat members? & the present request is for a unique nick name?
if ((Members.Length>0)&&(Array.IndexOf(Members,username)>-1))
{
throw new Exception("该用户已存在!");
}
// Create a new Member object for this participant
Member NewMember = new Member(username);
// Add this new member to the collectionof Application Level Variables
Application.Add(username, NewMember);
// Synchronization unlock
Application.UnLock();
// Go and get the list of current chat participants and retrun the list
return GetMembersList();
}
[WebMethod(Description="GetMsg方法用用户名和消息为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
public ChatMessage XchangeMsgs(string username, string Msg)
{
// Ascertain that all the registered chat participants are active
CheckMembersList();
// Synchronization Lock
Application.Lock();
// Get the collection of keys for the Application Variables
String[] Members = Application.AllKeys;
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
// Are there any registered chat members? & the present request is for a unique nick name?
{
throw new Exception("你当前可能没有登陆或登陆超时,请重新登陆!");
}
ChatMessage RetMsg = new ChatMessage();
RetMsg.UserList = GetMembersList();
// Loop through all the Chat Participant's serverside Member Objects and
// add the message just received in their waiting message queue
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member)Application[Members[x]];
temp.MsgQueue+=("<BR><Font color = Red>" + username + " 说:<BR></FONT><Font color = Blue>" + Msg);
if (temp.UserName == username)
{
RetMsg.Messages = temp.MsgQueue;
temp.MsgQueue="";
temp.LastAccessTime=DateTime.Now;
}
}
// Synchronization unlock
Application.UnLock();
return RetMsg;
}
[WebMethod(Description="GetMsg方法用username为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
public ChatMessage GetMsgs(string username)
{
Application.Lock();
CheckMembersList();
Application.Lock();
String[] Members = Application.AllKeys;
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
{
throw new Exception("Unknown User. Please Login with a UserName");
}
ChatMessage RetMsg = new ChatMessage();
RetMsg.UserList = GetMembersList();
Member temp = (Member)Application[username];
RetMsg.Messages = temp.MsgQueue;
temp.MsgQueue="";
temp.LastAccessTime=DateTime.Now;
Application.UnLock();
return RetMsg;
}
public string GetMembersList()
{
Application.Lock();
String UserList = "";
String[] Members = Application.AllKeys;
Application.UnLock();
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member)Application[Members[x]];
UserList += (temp.UserName+"\n");
}
return UserList;
}
private void CheckMembersList()
{
String[] Members = Application.AllKeys;
ArrayList RemoveList = new ArrayList();
for (int x=0;x<Members.Length;x++)
{
Member temp = (Member) Application[Members[x]];
int test = (DateTime.Now.Subtract(temp.LastAccessTime)).Minutes;
if (test > 2)
{
RemoveList.Add(Members[x]);
}
}
// Users = null;
for (int count = 0;count<RemoveList.Count;count++)
{
Application.Remove((String)RemoveList[count]);
}
return;
}
}
}
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程