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

📄 coordinator.xaml.cs

📁 ICE3.3.0--聊天程序服务器端demo
💻 CS
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Chat Demo is licensed to you under the terms
// described in the CHAT_DEMO_LICENSE file included in this
// distribution.
//
// **********************************************************************

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Browser;

namespace ChatDemo
{
    public class AMI_ChatSessionFactory_createI
    {
        public AMI_ChatSessionFactory_createI(Coordinator coordinator, string username)
        {
            _coordinator = coordinator;
            _username = username;
        }

        public void createSessionResponse(PollingChat.PollingChatSessionPrx session)
        {
            _coordinator.createSessionResponse(_username, session);
        }

        private Coordinator _coordinator;
        private string _username;
    }

    public class AMI_ChatSession_sendI
    {
        public AMI_ChatSession_sendI(Coordinator coordinator, string message)
        {
            _coordinator = coordinator;
            _message = message;
        }

        public void sendMessageResponse(long timestamp)
        {
            _coordinator.sendMessageResponse(timestamp, ChatUtils.stripHtml(_message));
        }

        private Coordinator _coordinator;
        private string _message;
    }

    //
    // The Coordinator class controls the logic of the demo. Is a ScriptableType
    // class, which means that parts of the class can be accessed from JavaScript code
    // using the Silverlight plugin. 
    // 
    // This is a partial class that is partially implemented in the Coordinator.xaml file.
    //
    [ScriptableType]
    public partial class Coordinator : UserControl
    {
        public Coordinator()
        {
            InitializeComponent();
            //
            // Register our cordinator with the scripting engine. This is needed to
            // call scriptable methods from the JavaScript Silverlight control.
            //
            HtmlPage.RegisterScriptableObject("ChatCoordinator", this);
        }

        public void Page_Loaded(object o, EventArgs e)
        {
            lock(this)
            {
                _chatView = HtmlPage.Window.CreateInstance("ChatViewProxy");
            }
        }

        [ScriptableMember]
        public void setBridgeUri(string bridgeUri)
        {
            _bridgeUri = bridgeUri;
        }

        [ScriptableMember]
        public void setSessionFactoryEndpoints(string endpoints)
        {
            _sessionFactoryEndpoints = endpoints;
        }

        //
        // This method is scriptable can be called from JavaScript.
        //
        [ScriptableMember]
        public void login(string name, string password)
        {
            lock(this)
            {
                _chatView.Invoke("setState", "Connecting");
            }
            if(_communicator != null)
            {
                try
                {
                    _communicator.destroy();
                }
                catch(Ice.LocalException)
                {
                }
            }

            try
            {
                //
                // Configure properties needed by Ice for Silverlight
                //
                Ice.InitializationData initData = new Ice.InitializationData();
                initData.properties = Ice.Util.createProperties();
                initData.properties.setProperty("Ice.BridgeUri", _bridgeUri);
                initData.properties.setProperty("Ice.FactoryAssemblies", "ChatDemo,version=1.1.1.0");

                //
                // Create the communicator.
                //
                _communicator = Ice.Util.initialize(initData);

                //
                // Create a proxy for the PollingChatSessionFactory object.
                //
                PollingChat.PollingChatSessionFactoryPrx sessionFactory = 
                    PollingChat.PollingChatSessionFactoryPrxHelper.uncheckedCast(
                        _communicator.stringToProxy(_sessionFactoryEndpoints));
                AMI_ChatSessionFactory_createI callback = new AMI_ChatSessionFactory_createI(this, name);
                sessionFactory.create_async(callback.createSessionResponse, createSessionException, name, password);
            }
            catch(Ice.LocalException ex)
            {
                lock(this)
                {
                    _chatView.Invoke("setError", "<div>" + ex.ToString() + "</div>");
                }
            }
        }

        [ScriptableMember]
        public void logout()
        {
            lock(this)
            {
                _connected = false;
    
                //
                // Stop the timer and detach event handler.
                //
                Timer.Stop();
                Timer.Completed -= new EventHandler(this.getUpdates);
    
                //
                // Destroy the session asynchronously.
                //
    
                _session.destroy_async(destroySessionResponse, destroySessionException);
    
                _chatView.Invoke("setState", "Disconnecting");
                _username = "";
            }
        }

        //
        // This method invokes the send_async operation.
        //
        [ScriptableMember]
        public void send(string message)
        {
            if(_session != null)
            {
                try
                {
                    ChatUtils.validateMessage(message);
                    AMI_ChatSession_sendI callback = new AMI_ChatSession_sendI(this, message);
                    _session.send_async(callback.sendMessageResponse, sendMessageException, message);
                }
                catch(Chat.InvalidMessageException ex)
                {
                    lock(this)
                    {
                        _chatView.Invoke("addMessage", "<div>&lt;system-message&gt; - " + ex.reason + "</div>");
                    }
                }
            }
        }

        //
        // Called from the handler for createSession_async response.
        //
        public void createSessionResponse(string username, PollingChat.PollingChatSessionPrx session)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                lock(this)
                {
                    _connected = true;
                    _username = ChatUtils.formatUsername(username);
                    _session = session;
                    _session.getInitialUsers_async(getInitialUsersResponse, getInitialUsersException);
                    _chatView.Invoke("setState", "Connected");
                }
            });
        }

        //
        // Handler for createSession_async exception.
        //
        private void createSessionException(Ice.Exception ex)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                if(ex is PollingChat.CannotCreateSessionException)
                {
                    PollingChat.CannotCreateSessionException e = (PollingChat.CannotCreateSessionException)ex;
                    lock(this)
                    {
                        _chatView.Invoke("setError", "<div><b>Login Failed:</b><br/>" + e.reason + "</div>");
                    }
                }
                else if(ex is Ice.UnknownLocalException)
                {
                    Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
                    lock(this)
                    {
                        _chatView.Invoke("setError", 
                                         "<div><b>Communication with server failed (Ice.UnknownLocalException)</b> <br/>" + 
                                         unknowEx.unknown + "</div>");
                    }
                }
                else
                {
                    lock(this)
                    {
                        _chatView.Invoke("setError", "<div><b>Communication with server failed:</b><br/>" + ex.ToString() +
                                                     "</div>");
                    }
                }
            });
        }

        //
        // Handler for send_async response.
        //
        public void sendMessageResponse(long timestamp, string message)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                lock(this)
                {
                    _chatView.Invoke("userSay", ChatUtils.formatTimestamp(timestamp), _username, message);
                }
            });

⌨️ 快捷键说明

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