---
title: "UCMA4中连接的建立"
author: "somefuture"
date: "2014-05-29T16:04:58Z"
tags: []
categories: ["行业应用"]
slug: "ucma4"
draft: false
lastmod: "2015-03-22T20:52:39Z"
id: 2073491
url: "https://somefuture.iteye.com/blog/2073491"
---

[UCMA4初体验](/blog/2073465)中说到了用UCMASampleHelper帮助建立连接，现在来看自己如何建立。

前文说到了，过程分四步：创建终端、认证终端、会话建立、连接建立。

1.UserEndpoint的构造方法只有一个：

```
public UserEndpoint(CollaborationPlatform platform, UserEndpointSettings settings);
```

 第一个参数是平台，第二个是配置。还记得UCMA对象基本都需要配置信息吗？而且一般都是第二个（最后一个）参数。我们先创建UserEndpointSettings的实例，UserEndpointSettings有四个构造方法，我们选择下面这个：

```
public UserEndpointSettings(string ownerUri, string serverName);
```

 也就是说“由谁登录，登录到哪”。然后指定它的两个属性：

```
using System.Net;
userEndpointSettings.AutomaticPresencePublicationEnabled = true;
userEndpointSettings.Credential = new NetworkCredential("i", "**", "ads.assk.com");
```

 AutomaticPresencePublicationEnabled设为true 能使对方跟踪你的状态信息。第二个是用来认证的信息，参数分别是用户名、密码、域。

然后来创建平台。CollaborationPlatform有三个构造方法：

```
public CollaborationPlatform(ClientPlatformSettings platformSettings);
public CollaborationPlatform(ProvisionedApplicationPlatformSettings platformSettings);
public CollaborationPlatform(ServerPlatformSettings platformSettings);
```

 我们传一个ClientPlatformSettings实例：

```
ClientPlatformSettings clientSettings = new ClientPlatformSettings("test", SipTransportType.Tls);
```

 SipTransportType有三个枚举值：None，Tcp，Tls。

然后终端就建好了：

```
UserEndpoint endPoint = new UserEndpoint(platform, userEndpointSettings);
```

 2.和服务器连接分两步：

```
endPoint.Platform.BeginStartup(CallStarttupComplete, endPoint);
endPoint.BeginEstablish(CallEstablishCompleted, endPoint);
```

 需要用线程信号控制一下，第一个执行完了才能执行第二个。

```
private void CallStarttupComplete(IAsyncResult result)
        {
            UserEndpoint userEndPoint = result.AsyncState as UserEndpoint;
            CollaborationPlatform collabPlatform = userEndPoint.Platform;
            {
                collabPlatform.EndStartup(result);
            }
        }
private void CallEstablishCompleted(IAsyncResult result)
        {
            {
                UserEndpoint messageCall = result.AsyncState as UserEndpoint;
                messageCall.EndEstablish(result);
                _callEstablishComplete.Set();
            }
        }
```

 3.开启会话前一篇文章说了，这里使用一下其他的构造方法：

```
ConversationSettings settings = new ConversationSettings();
settings.Priority = ConversationPriority.Normal;
settings.Subject = "Test 01.";
Conversation conversation = new Conversation(endPoint, settings);
```

 4.开始通信的连接建立也提过：

```
InstantMessagingCall imCall = new InstantMessagingCall(conversation);
imCall.BeginEstablish("sip:i@fd.com", null, null, CallEstablishCompleted, imCall);
```

 建立后才可以继续其他的操作，所以也要在这里暂停。