zoukankan      html  css  js  c++  java
  • 第二节 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 中每一模块重点功能及实现原理。

    以上内容有任何错误或不准确的地方请大家指正,不喜勿喷! 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
  • 相关阅读:
    收藏的一些前端学习的网址
    使用box-shadow 实现水波、音波的效果
    asyncjs,waterfall的使用
    兼容opacity的方法
    在php框架中写正规则表达式时的磕绊
    浏览器的渲染原理
    正规则表达式判断数字
    ie6,7,8不兼容rgba,写background时候不要写成rgba
    jquery-ias插件详解
    制作手机页面过程中遇到的一点问题
  • 原文地址:https://www.cnblogs.com/vipyoumay/p/2320826.html
Copyright © 2011-2022 走看看