TriggeredConnectionStateChangedEventArgs.Socket Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft den StreamSocket-instance für die Verbindung ab.
public:
property StreamSocket ^ Socket { StreamSocket ^ get(); };
StreamSocket Socket();
public StreamSocket Socket { get; }
var streamSocket = triggeredConnectionStateChangedEventArgs.socket;
Public ReadOnly Property Socket As StreamSocket
Eigenschaftswert
Die StreamSocket-instance für die Verbindung.
Windows-Anforderungen
App-Funktionen |
ID_CAP_PROXIMITY [Windows Phone]
proximity
ID_CAP_PROXIMITY [Windows Phone]
|
Beispiele
// Click event for "Start" button.
private void StartFindingPeersButton_Click(object sender, RoutedEventArgs e)
{
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
{
Windows.Networking.Proximity.PeerFinder.Start();
Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged +=
TriggeredConnectionStateChanged;
WriteMessageText("Waiting for peers. Enter proximity to connect. " +
"Call PeerFinder.start() on the peer to send and receive messages.\n");
}
else {
WriteMessageText("Peer discovery using tapping is not supported.\n");
}
}
private void TriggeredConnectionStateChanged(object sender,
Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs e)
{
if (e.State == Windows.Networking.Proximity.TriggeredConnectState.Completed)
{
WriteMessageText("Connected. You may now send a message.\n");
SendMessage(e.Socket);
}
}
Windows.Networking.Sockets.StreamSocket proximitySocket;
Windows.Storage.Streams.DataWriter dataWriter;
// Reference socket streams for writing and reading messages.
private void SendMessage(Windows.Networking.Sockets.StreamSocket socket) {
// Get the network socket from the proximity connection.
proximitySocket = socket;
// Create DataWriter for writing messages to peers.
dataWriter = new Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream);
// Listen for messages from peers.
Windows.Storage.Streams.DataReader dataReader =
new Windows.Storage.Streams.DataReader(proximitySocket.InputStream);
StartReader(proximitySocket, dataReader);
}
// Send a message to the socket.
private async void SendMessageText() {
string msg = SendMessageTextBlock.Text;
if (msg.Length > 0) {
var msgLength = dataWriter.MeasureString(msg);
dataWriter.WriteInt32(msg.Length);
dataWriter.WriteString(msg);
try
{
await dataWriter.StoreAsync();
WriteMessageText("Message sent: " + msg + "\n");
}
catch (Exception e)
{
WriteMessageText("Send error: " + e.Message + "\n");
}
}
}
// Read out and print the message received from the socket.
private async void StartReader(
Windows.Networking.Sockets.StreamSocket socket,
Windows.Storage.Streams.DataReader reader)
{
uint initialLength = 4;
try
{
await reader.LoadAsync(initialLength);
uint msgLength = (uint)reader.ReadInt32();
try
{
await reader.LoadAsync(msgLength);
string message = reader.ReadString(msgLength);
WriteMessageText("Received message: " + message + "\n");
// After receiving a message, listen for the next message.
StartReader(socket, reader);
}
catch (Exception e)
{
WriteMessageText("Error: " + e.Message + "\n");
socket.Dispose();
}
}
catch (Exception e)
{
WriteMessageText("Error: " + e.Message + "\n");
socket.Dispose();
}
}
// 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;
});
}
' Click event for "Start" button.
Private Sub StartFindingPeersButton_Click(sender As Object, e As RoutedEventArgs)
If ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) =
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) Then
Windows.Networking.Proximity.PeerFinder.Start()
AddHandler Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged,
AddressOf TriggeredConnectionStateChanged
WriteMessageText("Waiting for peers. Enter proximity to connect. " &
"Call PeerFinder.start() on the peer to send and receive messages." & vbTab)
Else
WriteMessageText("Peer discovery using tapping is not supported." & vbTab)
End If
End Sub
Private Sub TriggeredConnectionStateChanged(sender As Object,
e As Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs)
If e.State = Windows.Networking.Proximity.TriggeredConnectState.Completed Then
WriteMessageText("Connected. You may now send a message." & vbTab)
SendMessage(e.Socket)
End If
End Sub
Private proximitySocket As Windows.Networking.Sockets.StreamSocket
Private dataWriter As Windows.Storage.Streams.DataWriter
' Reference socket streams for writing and reading messages.
Private Sub SendMessage(socket As Windows.Networking.Sockets.StreamSocket)
' Get the network socket from the proximity connection.
proximitySocket = socket
' Create DataWriter for writing messages to peers.
dataWriter = New Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream)
' Listen for messages from peers.
Dim dataReader = New Windows.Storage.Streams.DataReader(proximitySocket.InputStream)
StartReader(proximitySocket, dataReader)
End Sub
' Send a message to the socket.
Private Async Sub SendMessageText()
Dim msg = SendMessageTextBlock.Text
If msg.Length > 0 Then
Dim msgLength = dataWriter.MeasureString(msg)
dataWriter.WriteInt32(msg.Length)
dataWriter.WriteString(msg)
Try
Await dataWriter.StoreAsync()
WriteMessageText("Message sent: " & msg & vbTab)
Catch e As Exception
WriteMessageText("Send error: " & e.Message & vbTab)
End Try
End If
End Sub
' Read out and print the message received from the socket.
Private Async Sub StartReader(
socket As Windows.Networking.Sockets.StreamSocket,
reader As Windows.Storage.Streams.DataReader)
Dim initialLength As UInteger = 4
Try
Await reader.LoadAsync(initialLength)
Dim msgLength = CType(reader.ReadInt32(), UInteger)
Try
Await reader.LoadAsync(msgLength)
Dim message = reader.ReadString(msgLength)
WriteMessageText("Received message: " & message & vbTab)
' After receiving a message, listen for the next message.
StartReader(socket, reader)
Catch e As Exception
WriteMessageText("Error: " & e.Message & vbTab)
socket.Dispose()
End Try
Catch e As Exception
WriteMessageText("Error: " & e.Message & vbTab)
socket.Dispose()
End Try
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
Hinweise
Die Socket-Eigenschaft ist NULL, bis eine erfolgreiche Verbindung hergestellt wurde. Wenn eine erfolgreiche Verbindung hergestellt wurde, ist Status gleich Abgeschlossen.