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

标签 lync

我有一个服务器应用程序,在极少数情况下出现意外错误时,它应该向 lync 用户(端点)发送即时消息。

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

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

最佳答案

我建议使用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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aaccd8c5c7eacec5c7cbc3c484c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>";
        private const string username = "USERNAME";
        private const string password = "PASSWORD";
        private const string domain = "DOMAIN";
        private const string destinationSip = "sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bffe4e6cbefe4e6eae2e5a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>";
        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;
        }


    }
}

关于lync - 哪个 Lync SDK?从托管代码发送 IM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16300082/

相关文章:

c# - 使用 Lync ContactManager 获取所有联系人

c# - Lync ContactList WPF 控制热键/快捷键从 WebBrowser 窃取按键

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

string - 在Powershell中迭代字符串的通用列表(Lync聊天室成员)

rest - 是否可以让 Lync 与 REST API 进行通信?

silverlight - 如何在Lync上制作外置表情

javascript - 在 javascript 中打开 lync 聊天窗口

lync-2010 - lync 扩展开发

java - 在 java 中编辑 MS Lync 对话聊天

c# - 在与 Lync 的远程 session 中执行标准 PowerShell cmdlet