Share via


PeerRole Énumération

Définition

Décrit le rôle de l’application homologue lorsqu’elle est connectée à plusieurs homologues.

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
Héritage
PeerRole
Attributs

Configuration requise pour Windows

Famille d’appareils
Windows 10 (introduit dans 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduit dans v1.0)
Fonctionnalités de l’application
proximity

Champs

Client 2

L’application est une application homologue cliente dans une connexion multipair.

Host 1

L’application est l’application d’homologue hôte dans une connexion multipair.

Peer 0

L’application fait partie d’une connexion à deux homologues.

Exemples

// 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;
        });
}

Remarques

La propriété Role est utilisée dans les connexions d’application multi-homologues pour déterminer si l’application homologue est l’hôte ou le client, ou si l’application homologue participe à une connexion à deux homologues en tant qu’homologue. Pour les connexions d’application multipair, vous devez définir la propriété Role avant d’appeler la méthode Start . Si la propriété Role n’est pas définie, la valeur par défaut est Peer.

Dans un scénario d’application multipaire, le rôle identifie la capacité des applications à se connecter. Une application hôte peut se connecter à jusqu’à quatre applications clientes . Les applications hôtes peuvent uniquement découvrir les applications qui s’affichent en tant qu’applications clientes . Les applications clientes peuvent uniquement découvrir les applications qui s’affichent en tant qu’applications hôtes . Le rôle Homologue identifie un scénario à deux applications. Par conséquent, les applications homologues peuvent uniquement découvrir d’autres applications homologues . Les mêmes règles s’appliquent aux applications homologues connectées à l’aide d’un mouvement d’appui. Par exemple, les applications publicitaires en tant qu’applications hôtes ne peuvent appuyer que pour se connecter aux applications publicitaires en tant qu’applications clientes .

S’applique à

Voir aussi