ProximityDevice.SubscribeForMessage(String, MessageReceivedHandler) 메서드

정의

지정된 메시지 유형에 대한 구독을 만듭니다.

public:
 virtual long long SubscribeForMessage(Platform::String ^ messageType, MessageReceivedHandler ^ messageReceivedHandler) = SubscribeForMessage;
long SubscribeForMessage(winrt::hstring const& messageType, MessageReceivedHandler const& messageReceivedHandler);
public long SubscribeForMessage(string messageType, MessageReceivedHandler messageReceivedHandler);
function subscribeForMessage(messageType, messageReceivedHandler)
Public Function SubscribeForMessage (messageType As String, messageReceivedHandler As MessageReceivedHandler) As Long

매개 변수

messageType
String

Platform::String

winrt::hstring

이 구독에 전달할 메시지 유형입니다.

messageReceivedHandler
MessageReceivedHandler

근접 공급자가 메시지를 전달할 때 호출할 처리기입니다.

반환

Int64

long long

long

구독에 대한 고유 ID입니다.

Windows 요구 사항

앱 기능
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

예제

Windows.Networking.Proximity.ProximityDevice proximityDevice;
long publishedMessageId = -1;
long subscribedMessageId = -1;

private void initializeProximityDevice()
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

    if (proximityDevice != null)
    {
        PublishMessageButton.Click += PublishMessage;
        SubscribeForMessageButton.Click += SubscribeForMessage;
        StopSubscribingForMessageButton.Click += StopSubscribingForMessage;
        StopPublishingMessageButton.Click += StopPublishingMessage;
    }
    else
    {
        WriteMessageText("Failed to initialized proximity device.\n");
    }
}

private void PublishMessage(object sender, RoutedEventArgs e)
{
    // Stop publishing the current message.
    if (publishedMessageId != -1)
    {
        proximityDevice.StopPublishingMessage(publishedMessageId);
    }

    publishedMessageId = 
        proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text);
}

private void SubscribeForMessage(object sender, RoutedEventArgs e)
{
    // Only subscribe for the message one time.
    if (subscribedMessageId == -1)
    {
        subscribedMessageId = 
        proximityDevice.SubscribeForMessage("Windows.SampleMessage", messageReceived);
    }
}

private void messageReceived(
    Windows.Networking.Proximity.ProximityDevice device, 
    Windows.Networking.Proximity.ProximityMessage message)
{
    MessageBlock.Text += "Message received: " + message.DataAsString + "\n";
}

private void StopSubscribingForMessage(object sender, RoutedEventArgs e)
{
    proximityDevice.StopSubscribingForMessage(subscribedMessageId);
    subscribedMessageId = -1;
}

private void StopPublishingMessage(object sender, RoutedEventArgs e)
{
    proximityDevice.StopPublishingMessage(publishedMessageId);
    publishedMessageId = -1;
}

// 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;
        });
}
Private proximityDevice As Windows.Networking.Proximity.ProximityDevice
Private publishedMessageId As Long = -1
Private subscribedMessageId As Long = -1

Private Sub initializeProximityDevice()
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault()

    If proximityDevice IsNot Nothing Then
        AddHandler PublishMessageButton.Click, AddressOf PublishMessage
        AddHandler SubscribeForMessageButton.Click, AddressOf SubscribeForMessage
        AddHandler StopSubscribingForMessageButton.Click, AddressOf StopSubscribingForMessage
        AddHandler StopPublishingMessageButton.Click, AddressOf StopPublishingMessage
    Else
        WriteMessageText("Failed to initialized proximity device." & vbTab)
    End If
End Sub

Private Sub PublishMessage(sender As Object, e As RoutedEventArgs)
    ' Stop publishing the current message.
    If publishedMessageId <> -1 Then
        proximityDevice.StopPublishingMessage(publishedMessageId)
    End If

    publishedMessageId =
        proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text)
End Sub

Private Sub SubscribeForMessage(sender As Object, e As RoutedEventArgs)
    ' Only subscribe for the message one time.
    If subscribedMessageId = -1 Then
        subscribedMessageId =
        proximityDevice.SubscribeForMessage("Windows.SampleMessage", AddressOf messageReceived)
    End If
End Sub

Private Sub messageReceived(
    device As Windows.Networking.Proximity.ProximityDevice,
    message As Windows.Networking.Proximity.ProximityMessage)

    MessageBlock.Text &= "Message received: " & message.DataAsString & vbTab
End Sub

Private Sub StopSubscribingForMessage(sender As Object, e As RoutedEventArgs)
    proximityDevice.StopSubscribingForMessage(subscribedMessageId)
    subscribedMessageId = -1
End Sub

Private Sub StopPublishingMessage(sender As Object, e As RoutedEventArgs)
    proximityDevice.StopPublishingMessage(publishedMessageId)
    publishedMessageId = -1
End Sub

' Write a message to MessageBlock on the UI thread.
Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False)
    Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        Sub()
            If overwrite Then
                MessageBlock.Text = message
            Else
                MessageBlock.Text &= message
            End If
        End Sub)
End Sub

설명

앱이 SubscribeForMessage 메서드를 호출하면 모든 근접 디바이스에서 동일한 메시지 유형( messageType 매개 변수로 표시됨)으로 게시된 메시지를 수신합니다. 다양한 메시지 유형에 대한 자세한 내용은 PublishBinaryMessage(String, IBuffer) 참조 항목의 설명을 참조하세요.

SubscribeForMessage 메서드에서 반환된 구독 ID를 StopSubscribingForMessage 메서드에 전달하여 구독을 취소할 수 있습니다.

PublishMessage, PublishBinaryMessage 또는 PublishUriMessage 메서드를 사용하여 구독자에게 메시지를 게시할 수 있습니다.

중요

근접 API는 인증, 암호화 또는 메시지 무결성을 제공하지 않습니다. 근접성을 사용하여 암호, 재무 데이터, 문자 메시지, 이메일, 사진 또는 정부 ID 번호와 같은 사용자 중요한 정보를 교환하지 마세요.

적용 대상

추가 정보