现在我们来着手创建一个基于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 中每一模块重点功能及实现原理。