如果文章对您有用,请随意打赏。

您的支持将鼓励我做的更好!

不懈探索,简单生活

第二节 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 中每一模块重点功能及实现原理。

posted @   帅虫哥  阅读(3171)  评论(3编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示