⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.js

📁 ICE3.3.0--聊天程序服务器端demo
💻 JS
📖 第 1 页 / 共 2 页
字号:
                for(var i = 0; i < response.length; i++)                {                    switch(response[i].jsontype)                    {                        case "PollingChat_InitialUserEvent":                            for(var j = 0; j < response[i].users.length; j++)                            {                                var user = new Element('li', { 'id': '_' + response[i].users[j] }).update(response[i].users[j]);                                $('userList').insert(user);                            }                            break;                        case "PollingChat_UserJoinedEvent":                            var user = new Element('li', { 'id': '_' + response[i].name }).update(response[i].name);                            $('userList').insert(user);                            coordinator._chatView.addMessage("<div>" + formatDate(response[i].timestamp) + " &lt;system-message&gt; - "                                    + response[i].name + " joined.</div>");                            break;                        case "PollingChat_UserLeftEvent":                            $('_' + response[i].name).remove();                            coordinator._chatView.addMessage("<div>" + formatDate(response[i].timestamp) + " &lt;system-message&gt; - "                                    + response[i].name + " left.</div>");                            break;                        case "PollingChat_MessageEvent":                            if(response[i].name != coordinator._username)                            {                                var message = new Element('div').update("<div>" + formatDate(response[i].timestamp) + " - &lt;"                                                                        + response[i].name + "&gt; " + response[i].message                                                                        + "</div>");                                coordinator._chatView.addMessage(message);                            }                            break;                    }                }            }        }        new Ajax.Request('Chat.php', opts);    },    send:function(message)    {        var params = new Hash();        var messageSend = message;        params.set('id', this._sessionId);        params.set('action','send');        params.set('message',message);        var opts =        {            contentType:'application/x-www-form-urlencoded',            method:'post',            encoding:'UTF-8',            parameters:params,            onComplete: function(transport)            {                if(transport.status != 200)                {                    coordinator.connectionLost("<b>Could contact web server HTTP status code: " + transport.status + "</b>");                    return;                }                var response = transport.responseText.evalJSON(true);                if(response.jsontype !== undefined)                {                    if(response.jsontype == "Chat_InvalidMessageException")                    {                        var message = "<div>&lt;system-message&gt; - " + response.reason + "</div>";                        coordinator._chatView.addMessage(message);                        return;                    }                    if(response.jsontype == "Ice_UnknownLocalException")                    {                        coordinator.connectionLost("(Ice_UnknownLocalException) " + response.unknown);                        return;                    }                    coordinator.connectionLost(transport.responseText);                    return;                }                if(isNumber(parseInt(response)))                {                    var message = new Element('div').update("<div>" + formatDate(response) + " - &lt;"                                                            + coordinator._username + "&gt; " + stripHtml(messageSend)                                                            + "</div>");                    coordinator._chatView.addMessage(message);                }            }        }        new Ajax.Request('Chat.php', opts);    },    setError:function(error, info)    {        this.setConnected(false);        this._chatView.appendError(error, info);    },    connectionLost:function(error)    {        if(this._connected)        {            if(this._updater)            {                this._init = false;                this._updater.stop();                this._updater = 0;            }            this._chatView.connectionLost("<div>&lt;system-message&gt;The connection with " +                                          "the server was unexpectedly lost.<br/>" + error +                                          "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");            this._connected = false;        }    },    setConnected:function(connected)    {        this._connected = connected;        if(connected)        {            this.getInitialUsers();            this._updater = new PeriodicalExecuter(this.getUpdates, 3);        }        else        {            if(this._updater)            {                this._init = false;                this._updater.stop();                this._updater = 0;            }        }        this._chatView.setConnected(connected);    },    setSessionId:function(sessionId)    {        this._sessionId = sessionId;    }});var ChatView = Class.create({    initialize:function()    {        this._coordinator = 0;    },    setCoordinator:function(coordinator)    {        this._coordinator = coordinator;    },    login:function()    {        this.clearError();        Element.hide('loginContainer');        Element.show('connectingContainer');        this._coordinator.login($('txtUserName').value, $('txtPassword').value);    },    logout:function()    {        this._coordinator.logout();    },    connectionLost:function(error)    {        var statusBar = new Element('div', { 'id': 'statusBar'}).update('<i class="offline">Offline</i>');        $('statusBar').replace(statusBar);        Element.hide('txtMessage');        Element.hide('logoutLink');        $('loginLink').show();        $('txtMessageReadOnly').show();        this.addMessage(error);    },    setConnected:function(connected)    {        var statusBar = $("statusBar");        if(connected)        {            var statusBar = new Element('div', { 'id': 'statusBar'}).update('<b class="online">Online</b>');            $('statusBar').replace(statusBar);            $('loginContainer').hide();            $('conversationView').show();            $('logoutLink').show();            $('txtMessage').show();            $('txtMessage').focus();            Element.hide('txtMessageReadOnly');        }        else        {            this.clearConversationView();            Element.hide('txtMessageReadOnly');            Element.hide('connectingContainer');            Element.hide('conversationView');            Element.hide('logoutLink');            Element.hide('loginLink');            var statusBar = new Element('div', { 'id': 'statusBar'}).update('<i class="offline">Offline</i>');            $('statusBar').replace(statusBar);            $('loginContainer').show();            $('txtUserName').focus();        }    },    clearError:function()    {        clearChilds('errorView');    },    clearConversationView:function()    {        clearChilds('messageView');        clearChilds('userList');        $('txtMessage').value = "";    },    processInputEvent:function(event)    {        if(event.keyCode == 13)        {            message = $('txtMessage').value;            if(message != "")            {                if(message.length < maxMessageSize)                {                    this._coordinator.send(message);                }                else                {                    var message = new Element('div').update("<div>&lt;system-message&gt; - Message length exceded, " +                                                            "maximum length is " + maxMessageSize +                                                            " characteers. </div>");                    this.addMessage(message);                }            }        }    },    clearInputEvent:function(event)    {        if(event.keyCode == 13)        {            document.getElementById('txtMessage').value = "";        }    },    appendError:function(name, info)    {        $('errorView').insert('<div><b>' + name + '</b></div>');        $('errorView').insert('<div><i>' + info + '</i></div>');    },    addMessage:function(message)    {        $('messageView').insert(message);        $('messageView').scrollTop = $('messageView').scrollHeight;    },    about:function()    {        alert("Chat Demo\n" + "Copyright \u00A9 2005-2008 ZeroC, Inc. All rights reserved.\n");    }});var Chat = Class.create({    initialize:function(chatView)    {        this._chatView = chatView;        this._coordinator = new Coordinator(chatView);        this._chatView.setCoordinator(this._coordinator);    }});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -