人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

無刷新的聊天室的制作兼談組件制作與ClientSide Script(一)

無刷新的聊天室的制作兼談組件制作與ClientSide Script(一)

更新時間:2022-06-07 文章作者:未知 信息來源:網絡 閱讀次數:

我們在傳統的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;
}


 &nb

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 东海县| 汉阴县| 通辽市| 浠水县| 东阳市| 古丈县| 收藏| 新沂市| 曲周县| 山丹县| 罗山县| 郧西县| 仙游县| 屯留县| 卢湾区| 德令哈市| 石屏县| 长寿区| 滨海县| 乌兰浩特市| 常熟市| 凤山市| 陈巴尔虎旗| 剑阁县| 白河县| 阿城市| 松江区| 玉山县| 洪江市| 林芝县| 定州市| 丰都县| 郴州市| 堆龙德庆县| 体育| 三原县| 乐山市| 永定县| 平乐县| 东宁县| 康乐县|