国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:16:34
字體:
來源:轉載
供稿:網友
我們在傳統的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;
        }

        
    }
}

商業源碼熱門下載www.html.org.cn

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景德镇市| 青田县| 那坡县| 广元市| 辽宁省| 万盛区| 北票市| 凤庆县| 泰来县| 郴州市| 治多县| 涿州市| 锡林浩特市| 宁远县| 文登市| 华安县| 苍南县| 东宁县| 深水埗区| 吴忠市| 东兰县| 长岭县| 留坝县| 临海市| 安多县| 余姚市| 霞浦县| 宿州市| 峨眉山市| 娱乐| 新营市| 青龙| 江津市| 章丘市| 汉阴县| 盘山县| 潼南县| 新津县| 榆中县| 罗平县| 安康市|