哪个 Lync SDK?从托管代码发送 IM

Posted

技术标签:

【中文标题】哪个 Lync SDK?从托管代码发送 IM【英文标题】:Which Lync SDK? Send IMs from managed code 【发布时间】:2013-04-30 12:36:21 【问题描述】:

我有一个服务器应用程序 - 在发生意外错误的罕见情况下 - 应该向 lync 用户(端点)发送即时消息。

根据我的阅读,我无法使用 Lync Client SDK,因为它依赖于应用程序服务器上正在运行的 Lync 客户端。然而这是不可能的。 UCWA 似乎是一个公平的选择,但我真的不想开始编写自己的 Lync API,将所有 HttpClient 闲聊隐藏在托管代码包装器中。

对于这个简单的用例,我最好的选择是什么?

【问题讨论】:

【参考方案1】:

我建议使用UCMA - 统一通信托管 API。如果它是您发送的一次性即时消息,并且您不需要可扩展以处理许多同时对话等的应用程序,您可以使用 UserEndpoint,因为它比 ApplicationEndpoint 的工作和设置要少一些。

我的博客上有一个这样做的工作示例:http://thoughtstuff.co.uk/2013/03/creating-ucma-applications-with-a-userapplication-instance-example-sending-ims/,其中还包含有关您拥有的不同选项的更多信息。

但是,为了完整起见(因为 SO 会比我预期的更长!)这里是代码:

using Microsoft.Rtc.Collaboration;
using System;

namespace SimpleUserUCMA

    class Program
    
        private const string sipaddress = "sip:from@domain.com";
        private const string username = "USERNAME";
        private const string password = "PASSWORD";
        private const string domain = "DOMAIN";
        private const string destinationSip = "sip:tom@domain.com";
        private const string IMMessage = "Hello!";

        static CollaborationPlatform _collabPlatform  get; set; 
        static UserEndpoint _endpoint  get; set; 
        static bool _OKToQuit = false;


        static void Main(string[] args)
        
            string userAgent = "ClientPlatformExample";

            var platformSettings = new ClientPlatformSettings(userAgent, Microsoft.Rtc.Signaling.SipTransportType.Tls);
            _collabPlatform = new CollaborationPlatform(platformSettings);

            //Start up the platform, calling back asynchronously once it's done.
            _collabPlatform.BeginStartup(EndCollabPlatformStartup, null);

            //In this example, wait for everything to finish before exiting
            while (!_OKToQuit)
            
                System.Threading.Thread.Sleep(2000);
            
        

        private static void EndCollabPlatformStartup(IAsyncResult ar)
        
            _collabPlatform.EndStartup(ar);

            //A collaboration plaform can have one or more Endpoints. An Endpoint is tied to a SIP Address.            
            UserEndpointSettings settings = new UserEndpointSettings(sipaddress);
            settings.Credential = new System.Net.NetworkCredential(username, password, domain);
            settings.AutomaticPresencePublicationEnabled = true;

            _endpoint = new UserEndpoint(_collabPlatform, settings);
            _endpoint.BeginEstablish(UserEndpointEstablishCompleted, null);

        


        private static void UserEndpointEstablishCompleted(IAsyncResult ar)
        
            _endpoint.EndEstablish(ar);

            //Once the endpoint is in place, create a Conversation and an IM Call. 
            var Conversation = new Conversation(_endpoint);
            var Call = new InstantMessagingCall(Conversation);

            //When the call is established, Flow will be created. Flow is how you sent IMs around. Therefore, just before
            //establishing, we attach an event handler to catch the flow being setup (it's state will change to Active)
            Call.InstantMessagingFlowConfigurationRequested += Call_InstantMessagingFlowConfigurationRequested;

            Call.BeginEstablish(destinationSip, new CallEstablishOptions(), EndBeginEstablish, Call);
        

        private static void EndBeginEstablish(IAsyncResult ar)
        
            Call call = (Call)ar.AsyncState;
            call.EndEstablish(ar);
        

        static void Call_InstantMessagingFlowConfigurationRequested(object sender, InstantMessagingFlowConfigurationRequestedEventArgs e)
        
            //Once we're notified about this, we get a handle to the newly created Flow. Let's use this to register for state changes.
            e.Flow.StateChanged += Flow_StateChanged;
        

        static void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
        
            if (e.State == MediaFlowState.Active)
            
                //The flow is now active! We can use it to send messages.
                InstantMessagingFlow flow = (InstantMessagingFlow)sender;
                flow.BeginSendInstantMessage(IMMessage, EndBeginSendInstanceMessage, flow);
            
        

        private static void EndBeginSendInstanceMessage(IAsyncResult ar)
        
            InstantMessagingFlow flow = (InstantMessagingFlow)ar.AsyncState;
            flow.EndSendInstantMessage(ar);

            //Having sent the message, terminate the conversation
            flow.Call.Conversation.BeginTerminate(EndBeginTerminate, flow.Call.Conversation);
        

        private static void EndBeginTerminate(IAsyncResult ar)
        
            Conversation conversation = (Conversation)ar.AsyncState;
            conversation.EndTerminate(ar);

            _OKToQuit = true;
        


    

【讨论】:

非常好。它有效:) 现在我只需要弄清楚如何直接发送消息而不是“邀请某人进行对话”。谢谢! 很好,我很高兴。请问你能把它标记为选择的答案吗?谢谢! 对 SO 来说有点新 :) 我实际上想将此标记为答案,但据我所见,评分为 +1。无论如何,我现在将其标记为答案。您是否碰巧知道如何在没有事先邀请的情况下直接向端点发送消息?

以上是关于哪个 Lync SDK?从托管代码发送 IM的主要内容,如果未能解决你的问题,请参考以下文章

使用 lync SDK 2013 (c#) 接收 IM 的 ASP.NET 代码

iOS即时通讯SDK中,腾讯,网易,环信,融云IM SDK对比,哪个更好

在 Lync IM 对话中解析文本?

如何从 lync 客户端 2013 通信中检索 IM 消息

我可以在 WPF 应用程序中使用 Lync 2013 SDK 或 API 发送即时消息和呼叫吗?

检索 Lync 状态