coordinator.xaml.cs
来自「ICE3.3.0--聊天程序服务器端demo」· CS 代码 · 共 639 行 · 第 1/2 页
CS
639 行
_session.ice_ping();
}
catch(Exception ex)
{
Dispatcher.BeginInvoke(delegate()
{
if(ex is Ice.UnknownLocalException)
{
Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
connectionLost("<div><system-message>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><system-message>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>");
}
});
}
});
pingThread.Start();
}
[ScriptableMember]
public void logout()
{
lock(this)
{
_connected = false;
_timer.Tick -= new EventHandler(pingSession);
_timer.Stop();
//
// 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)
{
lock(this)
{
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)
{
_chatView.Invoke("addMessage", "<div><system-message> - " + ex.reason + "</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);
}
});
}
//
// 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><system-message> - " + e.reason + "</div>");
}
}
else if(ex is Ice.UnknownLocalException)
{
Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
connectionLost("<div><system-message>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><system-message>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");
}
});
}
public void initEvent(String[] users)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
if(!_connected)
{
return;
}
for (int i = 0; i < users.Length; ++i)
{
_chatView.Invoke("addUser", users[i]);
}
}
});
}
public void userJoinEvent(long timestamp, string name)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
if(!_connected)
{
return;
}
_chatView.Invoke("addUser", name);
_chatView.Invoke("addMessage", "<div>" + ChatUtils.formatTimestamp(timestamp) +
" - <system-message> - " + name + " joined.</div>");
}
});
}
public void userLeaveEvent(long timestamp, string name)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
if (!_connected)
{
return;
}
_chatView.Invoke("delUser", name);
_chatView.Invoke("addMessage", "<div>" + ChatUtils.formatTimestamp(timestamp) +
" - <system-message> - " + name + " left.</div>");
}
});
}
public void userSayEvent(long timestamp, string name, string message)
{
Dispatcher.BeginInvoke(delegate()
{
lock(this)
{
if(!_connected)
{
return;
}
_chatView.Invoke("userSay", ChatUtils.formatTimestamp(timestamp), name, message);
}
});
}
public string getUsername()
{
return _username;
}
private void setConnected(string name, long timeout)
{
lock(this)
{
_connected = true;
_username = ChatUtils.formatUsername(name);
_chatView.Invoke("setState", "Connected");
_timer.Interval = TimeSpan.FromMilliseconds(timeout);
_timer.Tick += new EventHandler(pingSession);
_timer.Start();
join();
}
}
private void join()
{
System.Threading.Thread joinThread = new System.Threading.Thread(() =>
{
try
{
Ice.ObjectAdapter adapter =
_communicator.createObjectAdapterWithRouter("ChatDemo.Client", router);
Ice.Identity callbackId = new Ice.Identity();
callbackId.name = "ChatDemoCallback";
callbackId.category = router.getCategoryForClient();
Chat.ChatRoomCallbackPrx callback = Chat.ChatRoomCallbackPrxHelper.uncheckedCast(
adapter.add(new ChatRoomCallbackI(this), callbackId));
_session.setCallback(callback);
}
catch(Exception ex)
{
Dispatcher.BeginInvoke(delegate()
{
if(ex is Ice.UnknownLocalException)
{
Ice.UnknownLocalException unknowEx = (Ice.UnknownLocalException)ex;
connectionLost("<div><system-message>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><system-message>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>");
}
});
}
});
joinThread.Start();
}
private void connectionLost(string error)
{
lock(this)
{
_connected = false;
_timer.Tick -= new EventHandler(pingSession);
_timer.Stop();
_chatView.Invoke("connectionLost", error);
}
}
private Ice.Communicator _communicator = null;
private Chat.ChatSessionPrx _session = null;
private string _defaultRouter = "";
private Glacier2.RouterPrx router = null;
private string _username = "";
private bool _connected = false;
private ScriptObject _chatView;
private DispatcherTimer _timer = new DispatcherTimer();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?