Hello, UCMA to Lync!

源代码下载

1. 安装VS2010 + SP1

2. 安装UCMA SDK 3.0,下载地址:http://download.microsoft.com/download/B/A/4/BA468720-FE78-46CD-9976-A5190CE47084/UcmaSdkSetup.exe

3. 新建.Net Framework 4.0版的控制台应用程序:HelloUCMA

  app.config配置:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

4. 添加对Microsoft.Rtc.Collaboration的应用:x:\Program Files\Microsoft UCMA 3.0\SDK\Core\Bin\Microsoft.Rtc.Collaboration.dll

5. Program.cs类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Rtc.Collaboration;
using System.Net;
using Microsoft.Rtc.Signaling;
using System.Threading;

namespace HelloUCMA
{
    class Program
    {

//设置自己的身份信息

private static AutoResetEvent _platformStartupCompleted = new AutoResetEvent(false);

private static AutoResetEvent _userEndpointEstablishComplete = new AutoResetEvent(false);

private static AutoResetEvent _callEstablishComplete = new AutoResetEvent(false);

 

static void Main(string[] args)
{

//设置自己的Lync账号与服务器地址
UserEndpointSettings userEndpointSetting = new UserEndpointSettings("sip:xx@kinglan.info", "lync.kinglan.info");

 

//启动Lync状态自动发布
userEndpointSetting.AutomaticPresencePublicationEnabled = true;

 

//设置登陆的账号信息,未加入域方式:
userEndpointSetting.Credential = new NetworkCredential("xx", "Password", "kinglan.info");

//域成员计算机方式

//userEndpointSetting.Credential = CredentialCache.DefaultNetworkCredentials;

 

//设置客户端应用程序信息及通信方式
ClientPlatformSettings clientPlatformSetting = new ClientPlatformSettings("AppTest", SipTransportType.Tls);

//实例化一个协同平台
CollaborationPlatform collaborationPlatform = new CollaborationPlatform(clientPlatformSetting);

 

//实例化用户终结点

UserEndpoint userEndPoint = new UserEndpoint(collaborationPlatform, userEndpointSetting);

//启动协同平台
userEndPoint.Platform.BeginStartup(CallStarttupComplete, userEndPoint);

//等待启动完成
_platformStartupCompleted.WaitOne();
Console.WriteLine("Platform Started!");

//建立自己的连接
userEndPoint.BeginEstablish(CallEstablishUserEndpointComplete, userEndPoint);

//等待连接完成,非域成员计算机需要下载并安装一个用户证书
_userEndpointEstablishComplete.WaitOne();
Console.WriteLine("UserEndPoint Started!");

/*

Lync客户端导入根证书的方法

1、打开浏览器输入http://证书服务器的ip/certsrv,要求身份验证
2、点击 下载一个 CA 证书,证书链或 CRL 
3、点击下载 CA 证书链 ,保存到硬盘
4、在需要导入的计算机上,开始--运行--mmc--添加删除管理单元--添加--选择证书--本地计算机帐号--本地计算机--确定
5、展开证书--受信任的根颁发机构--右键导入--选择刚才下载的证书--下一部---下一步--完成,导入成功

也可在证书文件上右键,选择安装证书,同意安装到受信任的根颁发机构中即可。 

*/

//设置会话参数

ConversationSettings conversationSettings = new ConversationSettings();

//优先级
conversationSettings.Priority = ConversationPriority.Normal;

//会话主题,显示在会话窗口标题栏上
conversationSettings.Subject = "TestTopic";

//实例化会话
Conversation conversation = new Conversation(userEndPoint, conversationSettings);

 

//实例化一个即时消息呼叫

InstantMessagingCall messageCall = new InstantMessagingCall(conversation);
//设置目标用户信息,建立连接并等待连接建立完成
messageCall.BeginEstablish("sip:yy@kinglan.info", null, null, CallEstablishCompleted, messageCall);
_callEstablishComplete.WaitOne();

//取出流控制对象
InstantMessagingFlow flow = messageCall.Flow;

//收到消息时的处理方法
flow.MessageReceived += new EventHandler<InstantMessageReceivedEventArgs>(flow_MessageReceived);

//目标用户输入状态变更时的处理方式
flow.RemoteComposingStateChanged += new EventHandler<ComposingStateChangedEventArgs>(flow_RemoteComposingStateChanged);

//开始会话
Console.WriteLine("输入你聊天内容!");
while (true)
{//主循环,可不断输出发出消息
    string chatString = Console.ReadLine();
    flow.BeginSendInstantMessage(chatString, CallSendInstantMessageComplete, flow);
}

}

 

static void flow_RemoteComposingStateChanged(object sender, ComposingStateChangedEventArgs e)
{//目标用户输入状态变更时
    Console.WriteLine(e.Participant.Uri + " is " + e.ComposingState.ToString());
}

 

static void flow_MessageReceived(object sender, InstantMessageReceivedEventArgs e)
{//收到消息时
    Console.WriteLine(e.Sender.ToString() + "说:" + e.TextBody.ToString());
}

static void CallSendInstantMessageComplete(IAsyncResult result)
{//提示消息已发出
    Console.WriteLine("消息已发出.");
}

static void CallEstablishCompleted(IAsyncResult result)
{//连接目标用户成功
    try
    {
        InstantMessagingCall messageCall = result.AsyncState as InstantMessagingCall;
        messageCall.EndEstablish(result);
        _callEstablishComplete.Set();
    }
    catch (Exception e)
    {
        throw e;
    }
}

static void CallStarttupComplete(IAsyncResult result)
{//协同平台启动完成
    UserEndpoint userEndPoint = result.AsyncState as UserEndpoint;
    CollaborationPlatform collabPlatform = userEndPoint.Platform;
    try
    {
        collabPlatform.EndStartup(result);
        _platformStartupCompleted.Set();
    }
    catch (Exception e)
    {
        throw e;
    }
}

static void CallEstablishUserEndpointComplete(IAsyncResult result)
{//自己的连接建立成功
    try
    {
        UserEndpoint userEndpoint = result.AsyncState as UserEndpoint;
        userEndpoint.EndEstablish(result);
        _userEndpointEstablishComplete.Set();
    }
    catch (Exception e)
    {
        throw e;
    }
}
    }
}

posted @ 2012-10-11 11:13  StarKong  阅读(945)  评论(0编辑  收藏  举报