現在我們來着手創建一個基於UCMA API 的簡單聊天程序,然大家來感受下通過UCMA開發lyncServer的便捷性
1. 新建控制台程序,引Microsoft.RTC.Collaboration 庫(此dll文件在SDK的安裝路徑下)
2. 新建類,用於簡單的通訊
3. 創建UserEndPoint

UserEndpointSettings userEndpointSetting = new UserEndpointSettings("sip:stephenwang@biview.org", "lync.biview.org"); userEndpointSetting.AutomaticPresencePublicationEnabled = true;
userEndpointSetting.Credential = new NetworkCredential("stephenwang", "abc!@#123", "biview.org");
ClientPlatformSettings clientPlatformSetting = new ClientPlatformSettings("test", SipTransportType.Tls);
CollaborationPlatform collaborationPlatform = new CollaborationPlatform(clientPlatformSetting);
UserEndpoint userEndPoint = new UserEndpoint(collaborationPlatform, userEndpointSetting);
4.啟動客戶端終結點與服務器的連接

userEndPoint.Platform.BeginStartup(CallStarttupComplete, userEndPoint);
PlatformStartUpComplete.WaitOne();
Console.WriteLine("Platform start..."); userEndPoint.BeginEstablish(CallEstablishUserEndpointComplete,userEndPoint);
UserEndpointEstablishComplete.WaitOne();
Console.WriteLine("Userendpoint start...");
5.開啟會話

ConversationSettings conversationSettings = new ConversationSettings();
conversationSettings.Priority = ConversationPriority.Urgent;
conversationSettings.Subject = "StephenTestTopic";
Conversation conversation = new Conversation(userEndPoint, conversationSettings);
6.定義消息

InstantMessagingCall messageCall = new InstantMessagingCall(conversation); messageCall.InstantMessagingFlowConfigurationRequested +=
new EventHandler<InstantMessagingFlowConfigurationRequestedEventArgs> (messageCall_InstantMessagingFlowConfigurationRequested);
messageCall.BeginEstablish("sip:hanleilei@biview.org", new ToastMessage("測試Topic"), null, CallEstablishCompleted, messageCall);
CallEstablishComplete.WaitOne();
Console.WriteLine("輸入你聊天內容!");
string chatString = Console.Read().ToString(); flow.BeginSendInstantMessage(chatString,CallSendInstantMessageComplete, flow);
RecievedMessageComplete.WaitOne();
7.回調方法,在UCMA中大部分的方法都是異步的所以我們要定義回調

private void CallSendInstantMessageComplete(IAsyncResult result)
{
Console.WriteLine("Message have been send");
}
private void CallEstablishCompleted(IAsyncResult result)
{
try
{
InstantMessagingCall messageCall = result.AsyncState as InstantMessagingCall;
messageCall.EndEstablish(result);
CallEstablishComplete.Set();
}
catch (Exception e)
{ throw e; }
}
private void CallStarttupComplete(IAsyncResult result)
{
UserEndpoint userEndPoint = result.AsyncState as UserEndpoint;
CollaborationPlatform collabPlatform = userEndPoint.Platform;
try
{
collabPlatform.EndStartup(result);
PlatformStartUpComplete.Set();
}
catch(Exception e)
{ throw e; }
}
private void CallEstablishUserEndpointComplete(IAsyncResult result)
{
try
{
UserEndpoint userEndpoint = result.AsyncState as UserEndpoint;
userEndpoint.EndEstablish(result);
UserEndpointEstablishComplete.Set();
}
catch (Exception e)
{ throw e; }
}
8.接受對方返回消息

void flow_MessageReceived(object sender, InstantMessageReceivedEventArgs e) {
Console.WriteLine(e.Sender.ToString() + "說:" + e.TextBody.ToString());
RecievedMessageComplete.Set();
}
9.執行結果
通過上面的程序我們可以簡單了解下發起一個會話到返回一個消息的詳細步驟並且實現簡單基於LYNC Server的簡單聊天系統,下面我們來簡單看整個程序運行的步驟。
下一節內容中我們開始逐步展開,學習UCMA3.0 中每一模塊重點功能及實現原理。