PeerFinder.Role | role property

Applies to Windows and Windows Phone

Gets or sets the role of the app in peer-to-peer connections.

Syntax


var role = Windows.Networking.Proximity.PeerFinder.role;
Windows.Networking.Proximity.PeerFinder.role = role;

Property value

Type: PeerRole

The role of the app in peer-to-peer connections.

Remarks

The Role property is used in multi-peer app connections to identify whether the peer app is the Host or Client, or if the peer app is participating in a two-peer connection as a Peer. For multi-peer app connections, you must set the Role property before calling the Start method. If the Role property is not set, the default is Peer.

In a multi-peer app scenario, the Role identifies the capability of the apps to connect. A Host app can connect to up to five Client apps. Host apps can only discover apps that advertise as Client apps. Client apps can only discover apps that advertise as Host apps. The Peer role identifies a two-app scenario. Therefore, Peer apps can only discover other Peer apps. The same rules apply for peer apps connected using a tap gesture. For example, apps advertising as Host apps can only tap to connect with apps advertising as Client apps.

Examples


// Default to the peer role.
var appRole = Windows.Networking.Proximity.PeerRole.peer;
var launchedFromTap = false; 

app.onactivated = function (args) {
    if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
        Windows.Networking.Proximity.PeerFinder.addEventListener("connectionrequested", connectionRequested);

        // Detect if app launched from a tap.
        if ((args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) &&
            (args.detail.arguments === "Windows.Networking.Proximity.PeerFinder:StreamSocket")) {

            launchedFromTap = true; 
 
            if (args.detail.arguments.search("Role=Host") !== -1) { 
                appRole = Windows.Networking.Proximity.PeerRole.host; 
            } 
            else if (args.detail.arguments.search("Role=Client") !== -1) { 
                appRole = Windows.Networking.Proximity.PeerRole.client; 
            } 
            else { 
                appRole = Windows.Networking.Proximity.PeerRole.peer; 
            } 
        } 
    }

    advertiseForPeers(true);

    args.setPromise(WinJS.UI.processAll());
}

var started = false;

// Click event for "Advertise" button.
function advertiseForPeers(launchedFromTap) {
    Windows.Networking.Proximity.PeerFinder.displayName = displayNameTextBox.Text;

    if (Windows.Networking.Proximity.PeerFinder.supportedDiscoveryTypes &
        Windows.Networking.Proximity.PeerDiscoveryTypes.triggered) {

        Windows.Networking.Proximity.PeerFinder.addEventListener(
            "triggeredconnectionstatechanged", triggeredConnectionStateChanged);

        id("messageDiv").innerHTML +=
            "You can tap to connect a peer device that is " +
            "also advertising for a connection.<br />";
    } else {
        id("messageDiv").innerHTML +=
            "Tap to connect is not supported.<br />";
    }

    if (!launchedFromTap) {
        if (!(Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
              Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)) {
            id("messageDiv").innerHTML +=
                "Peer discovery using Wi-Fi Direct is not supported.<br />";
        }
    }       

    if (!started) {
        // 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 = 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;
    }
}


Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Networking.Proximity
Windows::Networking::Proximity [C++]

Metadata

Windows.winmd

Capabilities

proximity
ID_CAP_PROXIMITY [Windows Phone]
ID_CAP_NETWORKING [Windows Phone]

See also

PeerFinder
Proximity and Tapping (JavaScript)
Proximity and Tapping (C#/VB/C++)
Samples
Proximity sample

 

 

Show:
© 2015 Microsoft