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