第二節 UCMA Hello World!


現在我們來着手創建一個基於UCMA API 的簡單聊天程序,然大家來感受下通過UCMA開發lyncServer的便捷性

1. 新建控制台程序,引Microsoft.RTC.Collaboration (dll文件在SDK的安裝路徑下)

 

2. 新建類,用於簡單的通訊

 

3. 創建UserEndPoint

 

View Code
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.啟動客戶端終結點與服務器的連接

 

View Code
userEndPoint.Platform.BeginStartup(CallStarttupComplete, userEndPoint);                
PlatformStartUpComplete.WaitOne();
Console.WriteLine("Platform start..."); userEndPoint.BeginEstablish(CallEstablishUserEndpointComplete,userEndPoint);
UserEndpointEstablishComplete.WaitOne();
Console.WriteLine("Userendpoint start...");

5.開啟會話

 

View Code
ConversationSettings conversationSettings = new ConversationSettings();            
conversationSettings.Priority = ConversationPriority.Urgent;
conversationSettings.Subject = "StephenTestTopic";
Conversation conversation = new Conversation(userEndPoint, conversationSettings);

6.定義消息

 

View Code
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中大部分的方法都是異步的所以我們要定義回調

 

View Code
     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.接受對方返回消息

 

View Code
 void flow_MessageReceived(object sender, InstantMessageReceivedEventArgs e)            {
Console.WriteLine(e.Sender.ToString() + "說:" + e.TextBody.ToString());
RecievedMessageComplete.Set();
}

9.執行結果

 

 


通過上面的程序我們可以簡單了解下發起一個會話到返回一個消息的詳細步驟並且實現簡單基於LYNC Server的簡單聊天系統,下面我們來簡單看整個程序運行的步驟

 

 

下一節內容中我們開始逐步展開,學習UCMA3.0 中每一模塊重點功能及實現原理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM