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

📄 coordinator.xaml.cs

📁 ICE3.3.0--聊天程序服务器端demo
💻 CS
📖 第 1 页 / 共 2 页
字号:
        }

        //
        // Handler for send_async exception.
        //
        private void sendMessageException(Ice.Exception ex)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                if(ex is Chat.InvalidMessageException)
                {
                    Chat.InvalidMessageException e = (Chat.InvalidMessageException)ex;
                    lock(this)
                    {
                        _chatView.Invoke("addMessage", "<div>&lt;system-message&gt; - " + e.reason + "</div>");
                    }
                }
                else if(ex is Ice.UnknownLocalException)
                {
                    Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost (Ice.UnknownLocalException).<br/>" +
                                   unknowEx.unknown +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
                else
                {
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost.<br/>" + ex.ToString() +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
            });
        }

        //
        // Handler for destroySession_async response.
        //
        private void destroySessionResponse()
        {
            Dispatcher.BeginInvoke(delegate()
            {
                if(_communicator != null)
                {
                    try
                    {
                        _communicator.destroy();
                    }
                    catch(Ice.LocalException)
                    {
                    }
                    _communicator = null;
                }
                lock(this)
                {
                    _connected = false;
                    _chatView.Invoke("setState", "Disconnected");
                }
            });
        }

        //
        // Handler for destroySession_async exception.
        //
        private void destroySessionException(Ice.Exception ex)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                if(_communicator != null)
                {
                    try
                    {
                        _communicator.destroy();
                    }
                    catch(Ice.LocalException)
                    {
                    }
                    _communicator = null;
                }
                lock(this)
                {
                    _connected = false;
                    _chatView.Invoke("setState", "Disconnected");
                }
            });
        }

        private void getInitialUsersResponse(String[] users)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                lock(this)
                {
                    if(!_connected)
                    {
                        return;
                    }
    
                    for (int i = 0; i < users.Length; ++i)
                    {
                        _chatView.Invoke("addUser", users[i]);
                    }
    
                    //
                    // The first time we invoke updates, don't wait for the timeout.
                    //
                    getUpdates(null, new EventArgs());
    
                    //
                    // Initalize the timer.
                    //
                    Timer.Duration = TimeSpan.FromSeconds(3);
    
                    //
                    // Attach an EventHandler to the timer completed event.
                    //
                    Timer.Completed += new EventHandler(this.getUpdates);
                    Timer.Begin();
                }
            });
        }

        private void getInitialUsersException(Ice.Exception ex)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                if(!_connected)
                {
                    return;
                }
                if(ex is Ice.UnknownLocalException)
                {
                    Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost. (Ice.UnknownLocalException)<br/>" +
                                   unknowEx.unknown +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
                else
                {
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost.<br/>" + ex.ToString() +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
            });

        }

        //
        // Handler for getUpdates_async response.
        //
        private void getUpdatesResponse(PollingChat.ChatRoomEvent[] events)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                lock(this)
                {
                    if(!_connected)
                    {
                        return;
                    }
    
                    //
                    // Iterate over events and proccess each event.
                    //
                    for(int i = 0; i < events.Length; ++i)
                    {
                        if(events[i] is PollingChat.MessageEvent)
                        {
                            PollingChat.MessageEvent e = (PollingChat.MessageEvent)events[i];
                            if(!_username.Equals(e.name, StringComparison.CurrentCultureIgnoreCase))
                            {
                                _chatView.Invoke("userSay", ChatUtils.formatTimestamp(e.timestamp), e.name, e.message);
                            }
                        }
                        else if(events[i] is PollingChat.UserJoinedEvent)
                        {
                            PollingChat.UserJoinedEvent e = (PollingChat.UserJoinedEvent)events[i];
                            _chatView.Invoke("addUser", e.name);
                            _chatView.Invoke("addMessage", "<div>" + ChatUtils.formatTimestamp(e.timestamp) + 
                                            " - &lt;system-message&gt; - " + e.name + " joined.</div>");
                        }
                        else if(events[i] is PollingChat.UserLeftEvent)
                        {
                            PollingChat.UserLeftEvent e = (PollingChat.UserLeftEvent)events[i];
                            _chatView.Invoke("delUser", e.name);
                            _chatView.Invoke("addMessage", "<div>" + ChatUtils.formatTimestamp(e.timestamp) +
                                            " - &lt;system-message&gt; - " + e.name + " left.</div>");
                        }
                    }
                    Timer.Begin(); // Restart the timer.
                }
            });
        }

        //
        // Handler for getUpdates_async exception.
        //
        private void getUpdatesException(Ice.Exception ex)
        {
            Dispatcher.BeginInvoke(delegate()
            {
                lock(this)
                {
                    if(!_connected)
                    {
                        return;
                    }
                }
                if(ex is Ice.UnknownLocalException)
                {
                    Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost. (Ice.UnknownLocalException)<br/>" +
                                   unknowEx.unknown +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
                else
                {
                    connectionLost("<div>&lt;system-message&gt;The connection with " +
                                   "the server was unexpectedly lost.<br/>" + ex.ToString() +
                                   "<br/><b>You can try to login again from the Login link in the top menu.</b></div>");
                }
            });
        }

        //
        // This method is an event handler to get updates and is attached 
        // to the Timer.Completed event.
        //
        private void getUpdates(object sender, EventArgs e)
        {
            _session.getUpdates_async(getUpdatesResponse, getUpdatesException);
        }

        private void connectionLost(string error)
        {
            lock(this)
            {
                _connected = false;
                //
                // Stop the timer and detach event handler.
                //
                Timer.Stop();
                Timer.Completed -= new EventHandler(this.getUpdates);
                _chatView.Invoke("connectionLost", error);
            }
        }



        private Ice.Communicator _communicator = null;
        private PollingChat.PollingChatSessionPrx _session = null;
        private string _sessionFactoryEndpoints = "";
        private string _bridgeUri = "";
        private string _username = "";
        private bool _connected = false;
        private ScriptObject _chatView;
    }
}

⌨️ 快捷键说明

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