第二节 UCMA Hello World!
现在我们来着手创建一个基于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 中每一模块重点功能及实现原理。
以上内容有任何错误或不准确的地方请大家指正,不喜勿喷!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架