One thing we compare the headache in the traditional web program is the hot sense of the screen. Although there is a SERVER PUSH technology, it is more difficult to implement in IE. Now WebService gives us such a chance, everyone knows that WebService is SOAP, and SOAP is an XML application. If you have used MS XML SDK3.0, you will know that there is an XMLHTTP method inside, in fact, we are at that time It is already possible to replace FORM in XMLHTTP. It is also no refresh. In fact, it is important to be partially refreshed. Let's take a look at how to do it, first make a chat webservice, first to analyze, a chat room should have two Estabilists and messages so we can build a type (remember that I have said it is also a type), which contains two elements.
///ChatMessage.cs
Using system;
Namespace chat
{
///
/// ChatMessage class package two string variables: userlists - user list, Messages - information to pass
/// summary>
Public class chatmessage
{
Public String UserList, Messages;
}
}
What is the second we have to build? A chat room should be able to store the name and access time of online members
//Member.cs
Using system;
Namespace chat
{
///
/// MEMBER class encapsulates the variable of the Server end for each chat
/// summary>
Public Class Member
{
// Store the queue of the message
Public String Username, Msgqueue;
// Judgment the retention event for kick
Public system.datetime lastaccesstime;
// THE Constructor
Public Member (String Nickname)
{
THIS.USERNAME = Nickname;
THIS.LASTACCESSTIME = DATETIME.NOW;
}
}
}
Next, we should do this 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 Description for ChatWebService.
/// summary>
[WebSpace (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
InitializationComponent ();
}
#Region Component Designer Generated Code
///
/// The contents of this method with the code editor.
/// summary>
Private vidinitiRizeComponent ()
{
}
#ndregion
///
/// Clean Up Any Resources Being Used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
}
[WebMethod (Description = "receive username as a parameter stored in Application object")]
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 is for a unique NICK Name? & THE PRETERS?
IF ((MEMBERS.LENGTH> 0) && (Array.indexof (MEMBERS, Username)> - 1))
{
Throw New Exception ("This user already exists!");
}
// Create a new Member Object for this Participant
MEWMEMBER = New Member (username);
// Add this new member to the collectionof application level company
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 method Returns a ChatMessage object with the user name and message, including the message and user list to be passed")]
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 The ANY REGISTERED CHAT MEMBERS? & THE PRESENT REQUEST IS for a Unique NICK NAME?
{
Throw new Exception ("You may not log in or log in, please re-land!");
}
ChatMessage Retmsg = new chatmessage ();
RETMSG.UserList = getmemberslist ();
// Loop Through all the chat participant's serverside Member Objects and
// add the message just receific in their waiting message queue
For (int x = 0; x { MEMBER TEMP = (MEMBER) Application [MEMBERS [X]]; Temp.msgqueue = (" IF (Temp.userName == UserName) { RETMSG.MESSAGES = TEMP.MSGQUEUE; TEMP.MSGQUE = "" Temp.lastaccesstime = DATETIME.NOW; } } // Synchronization UNLOCK Application.unlock (); Return RETMSG; } [WebMethod (Description = "getMSG method Returns a ChatMessage object for parameters, including messages and users to pass on")] 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.MSGQUE = "" 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 { MEMBER TEMP = (MEMBER) Application [MEMBERS [X]]; UserList = (Temp.username "/ N"); } Return UserList; } Private void checkmemberslist () { String [] MEMBERS = Application.AllKeys; ArrayList Removel (); For (int x = 0; 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 { Application.remove (String) RemovelSt [count]); } Return; } } } Ok, until this, our webservice is complete, everyone may be dissatisfied, still did not realize no refresh, don't worry, this is the client. Let's do this. Generally speaking, we can do an HTML page instead of server page, but in order to explain how to make components, I decided to make a Server Control, first look at the code. Using system; Using system.Web.ui; Using system.Web.ui.webcontrols; Using system.Web.ui.htmlcontrols; Using system.componentmodel; Namespace michael.web.ui.controls { /// /// summary description for chat. /// summary> [DefaultProperty ("Text"), ToolboxData ("<{0}: chat runat = server> {0}: chat>")] Public class chat: system.Web.ui.WebControls.table { PRIVATE STRING DOC; PRIVATE STRING TEXT; [Bindable (TRUE), Category ("APPEARANCE"), DEFAULTVALUE ("")] Public String Text { get { Return TEXT; } set { TEXT = VALUE; } } /// /// render this control to the output parameter specified. /// summary> /// The HTML Writer to Write Out to param> protected override void render (HTMLTextWriter Output) { // The script block is written to the client Output.write (doc); Base.Render (Output); } Private string serviceurl = "http://localhost/chat/chatwebservice.asmx? WSDL"; [Bindable (TRUE), Category ("WebServiceProperty", DefaultValue ("http://localhost/chat/chatwebservice.asmx? Wsdl")]] Public String ServiceURL { get { Return Serviceur1; } set { ServiceURL = Value; } } Private string behaviorurl = "http://localhost/chat/webservice.htc"; [Bindable (TRUE), Category ("WebServiceProperty", DEFAULTVALUE ("")] Public String BehaviorURL { get { Return BehaviorURL; } set { Behaviorurl = Value; } } PRIVATE STRING TABECSSCLASS; [Bindable (TRUE), Category ("LayoutProperty"), DEFAULTVALUE ("")] Public String TablecsSclass { get { Return TablecssClass; } set { TablecssClass = value; } } PRIVATE STRING TITECSSCLASS; [Bindable (TRUE), Category ("LayoutProperty"), DEFAULTVALUE ("")] Public String TitlecsclassClassClassClass { get { Return Titlecsclass; } set { TitlecssClass = Value; } } PRIVATE STRING OnlineCSSClass; [Bindable (TRUE), Category ("LayoutProperty"), DEFAULTVALUE ("")] Public String OnlineCSSClassClass { get { Return onlinecssclass; } set { OnlinecssClass = Value; } } PRIVATE STRING MSGCSSCLASS; [Bindable (TRUE), Category ("LayoutProperty"), DEFAULTVALUE ("")] Public String MsgcssclassClassClass { get { Return msgcssclass; } set { Msgcssclass = value; } } Private string selusercssclass; [Bindable (TRUE), Category ("LayoutProperty", DEFAULTVALUE ("")] Public String SELUSERCSSCLASSCSSCLASS { get { Return SELUSERCSSCLASS; } set { SELUSERCSSCLASS = VALUE; } } Protected Override Void OnNit (Eventargs E) { THIS.ID = "service"; THIS.Style ["Behavior"] = "URL ('" behaviorurl ")" THIS.Style ["Table-Layout"] = "fixed"; IF (this.attributes ["class"] == null) this.attributes ["class"] = TableCSSCLASS; This.attributes ["onResult"] = UNIQUEID "_ONMYRESULT ();" TABLEROW TR; // and also create 7 Table Cell Elements One by one Tablecell Cell = New TableCell (); Cell.attributes ["class"] = title; Cell.attributes ["align"] = "left"; // set the capen of the control Cell.Text = "Portal chat room"; // instantiate a Table ROA and Attach THE FIRST CELL TO IT Tr = new tablerow (); Tr.cells.Add (Cell); // add the table row tour control this.Rows.Add (TR); // row no 2 starts here Cell = new TableCell (); Cell.attributes ["class"] = onlinecssclass; Cell.Text = "Online people"; Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // Row No 3 Starts Here Cell = new TableCell (); Cell.Style ["Height"] = "25%"; //WE CREATE A DIV Element Using HTMLGENERICCONTROL OBJECT // We can also do this using the panel object HtmlGenericControl D = New HTMLGENERICCONTROL ("DIV"); D. ID = uniqueid "_chatmsgs"; D.Style ["Height"] = "100%"; D.Style ["width"] = "100%"; D.Style ["overflow"] = "auto"; D.Style ["padding-left"] = "15%"; D. ID = UNIQUEID "_Chatlist"; // adding the div element to the Table Cell Cell.Controls.Add (d); Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // row no 4 starts here Cell = new TableCell (); Cell.attributes ["class"] = msgcssclass; Cell.Text = "Message:"; Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // Row No 5 Starts Here Cell = new TableCell (); Cell.Style ["Height"] = "35%"; D = New HTMLGENERICCONTROL ("DIV"); D. ID = uniqueid "_chatmsgs"; D.Style ["Height"] = "100%"; D.Style ["width"] = "100%"; D.Style ["overflow"] = "auto"; Cell.Controls.Add (d); Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // Row No 6 Starts Here Cell = new TableCell (); Cell.attributes ["class"] = soldusercssclass; Cell.id = UNIQUEID "_prompt"; Cell.Text = "Select a user:"; Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // Row No 7 Starts Here Cell = new TableCell (); Cell.Text = " / r / n"; Cell.Text = " Cell.Text = " Cell.Text = " Cell.Style ["Background-color"] = "gainsboro"; Tr = new tablerow (); Tr.cells.Add (Cell); this.Rows.Add (TR); // First Script Block Is Written Into 'Doc' Variable
" username "said:
font> " msg);
/ r / n";