PeerRole 枚举

定义

描述连接到多个对等方时对等应用的角色。

public enum class PeerRole
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
enum class PeerRole
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public enum PeerRole
var value = Windows.Networking.Proximity.PeerRole.peer
Public Enum PeerRole
继承
PeerRole
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
应用功能
proximity

字段

Client 2

该应用是多对等连接的客户端对等应用。

Host 1

该应用是多对等连接中的主机对等应用。

Peer 0

该应用是两对等连接的一部分。

示例

// Default to the peer role.
Windows.Networking.Proximity.PeerRole appRole = Windows.Networking.Proximity.PeerRole.Peer;
bool launchedByTap = false;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    LaunchActivatedEventArgs launchArgs = e.Parameter as LaunchActivatedEventArgs;

    if (launchArgs.Arguments.Contains("Windows.Networking.Proximity.PeerFinder:StreamSocket"))
    {
        launchedByTap = true;

        if (launchArgs.Arguments.Contains("Role=Host"))
        {
            appRole = Windows.Networking.Proximity.PeerRole.Host;
        }
        else if (launchArgs.Arguments.Contains("Role=Client"))
        {
            appRole = Windows.Networking.Proximity.PeerRole.Client;
        }
        else
        {
            appRole = Windows.Networking.Proximity.PeerRole.Peer;
        }
    }
}

bool started = false;

// Click event for "Advertise" button.
void AdvertiseForPeers(object sender, RoutedEventArgs e)
{
    if (!started)
    {
        Windows.Networking.Proximity.PeerFinder.DisplayName = DisplayNameTextBox.Text;

        if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
        {
            Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged +=
                TriggeredConnectionStateChanged;

            WriteMessageText("You can tap to connect a peer device that is " +
                             "also advertising for a connection.\n");
        }
        else
        {
            WriteMessageText("Tap to connect is not supported.\n");
        }

        if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
             Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) !=
             Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
        {
            WriteMessageText("Peer discovery using Wi-Fi Direct is not supported.\n");
        }

        // Set the peer role selected by the user.  
        if (launchedByTap)
        {
            Windows.Networking.Proximity.PeerFinder.Role = appRole;
        }
        else
        {
            switch (GetRoleFromUser())
            {
                case "Peer":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Peer;
                    break;
                case "Host":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Host;
                    break;
                case "Client":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Client;
                    break;
            }
        }

        // Set discoveryData property with user supplied text. 
        var discData = GetDiscoveryDataFromUser();
        var writer = new Windows.Storage.Streams.DataWriter(
            new Windows.Storage.Streams.InMemoryRandomAccessStream());
        writer.WriteString(discData);
        Windows.Networking.Proximity.PeerFinder.DiscoveryData =
            writer.DetachBuffer();

        Windows.Networking.Proximity.PeerFinder.Start();
        started = true;
    }
}

// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
    await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (overwrite)
                MessageBlock.Text = message;
            else
                MessageBlock.Text += message;
        });
}

注解

Role 属性用于多对等应用连接,用于标识对等应用是主机还是客户端,或者对等应用是否以对等互连身份参与对等连接。 对于多对等应用连接,必须在调用 Start 方法之前设置 Role 属性。 如果未设置 Role 属性,则默认值为 Peer

在多对等应用方案中,角色标识要连接的应用的功能。 主机应用最多可以连接到四个客户端应用。 主机 应用只能发现作为 客户端 应用播发的应用。 客户端 应用只能发现作为 主机 应用播发的应用。 对等角色标识两个应用场景。 因此, 对等 应用只能发现其他 对等 应用。 相同的规则适用于使用点击手势连接的对等应用。 例如,作为 主机 应用进行广告的应用只能点击以将应用广告作为 客户端 应用进行连接。

适用于

另请参阅