📄 coordinator.xaml.cs
字号:
// **********************************************************************
//
// 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;
using System.Windows.Threading;
namespace ChatDemo
{
public class ChatRoomCallbackI : Chat.ChatRoomCallbackDisp_
{
public ChatRoomCallbackI(Coordinator coordinator)
{
_coordinator = coordinator;
}
public override void init(string[] users, Ice.Current currrent)
{
_coordinator.initEvent(users);
}
public override void send(long timestamp, string name, string message, Ice.Current current)
{
if (!name.Equals(_coordinator.getUsername(), StringComparison.CurrentCultureIgnoreCase))
{
_coordinator.userSayEvent(timestamp, name, message);
}
}
public override void join(long timestamp, string name, Ice.Current current)
{
_coordinator.userJoinEvent(timestamp, name);
}
public override void leave(long timestamp, string name, Ice.Current current)
{
_coordinator.userLeaveEvent(timestamp, name);
}
private Coordinator _coordinator;
}
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)
{
_chatView = HtmlPage.Window.CreateInstance("ChatViewProxy");
}
[ScriptableMember]
public void setDefaultRouter(string defaultRouter)
{
_defaultRouter = defaultRouter;
}
//
// 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.Default.Router", _defaultRouter);
initData.properties.setProperty("Ice.FactoryAssemblies", "ChatDemo,version=1.1.1.0");
//
// Create the communicator.
//
_communicator = Ice.Util.initialize(initData);
System.Threading.Thread loginThread = new System.Threading.Thread(() =>
{
try
{
Ice.RouterPrx defaultRouter = _communicator.getDefaultRouter();
if(defaultRouter == null)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed: Ice.Default.Router not set.");
}
});
return;
}
router = Glacier2.RouterPrxHelper.checkedCast(defaultRouter);
if(router == null)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed: Ice.Default.Router is not a Glacier2 router.");
}
});
return;
}
_session = Chat.ChatSessionPrxHelper.checkedCast(router.createSession(name, password));
lock(this)
{
_connected = true;
}
long sessionPingPeriod = router.getSessionTimeout() * 1000 / 2;
Dispatcher.BeginInvoke(delegate()
{
setConnected(name, sessionPingPeriod);
});
}
catch(Glacier2.CannotCreateSessionException ex)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed (Glacier2.CannotCreateSessionException):\n" +
ex.reason);
}
});
}
catch(Glacier2.PermissionDeniedException ex)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed (Glacier2.PermissionDeniedException):\n"
+ ex.reason);
}
});
}
catch(Ice.ConnectionRefusedException)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError",
"Login failed (Ice.ConnectionRefusedException).\n" +
"Please check your server:\n" +
"a Glacier2 router should be running on " + _defaultRouter + " and\n" +
"the Ice for Silverlight policy server should be running on the same " +
"address the web server is running.");
}
});
}
catch(Ice.TimeoutException)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed (Ice.TimeoutException).\n" +
"Please check your server:\n" +
"a Glacier2 router should be running on " + _defaultRouter + " and\n" +
"the Ice for Silverlight policy server should be running on the same " +
"address the web server is running.");
}
});
}
catch(Ice.SocketException)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError",
"Login failed (Ice.SocketException).\n" +
"Please check your server:\n" +
"a Glacier2 router should be running on " + _defaultRouter + " and\n" +
"the Ice for Silverlight policy server should be running on the same " +
"address the web server is running.");
}
});
}
catch(Ice.DNSException)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed (Ice.DNSException ).\n" +
"Please check your DNS configuration:\n" +
"Host \"" + _defaultRouter +
"\" should point to the IP address of a Glacier2 router\n" +
"that should be listening on port 4502, and the server firewall needs\n" +
"to be configured to allow TCP connections to this port.");
}
});
}
catch(Exception ex)
{
if(_communicator != null)
{
_communicator.destroy();
_communicator = null;
}
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
_chatView.Invoke("setError", "Login failed:\n" + ex.ToString());
}
});
}
});
loginThread.Start();
}
catch(Ice.LocalException ex)
{
lock(this)
{
_chatView.Invoke("setError", "<div>" + ex.ToString() + "</div>");
}
}
}
public void pingSession(object sender, EventArgs args)
{
System.Threading.Thread pingThread = new System.Threading.Thread(() =>
{
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -