Skip to content

Instantly share code, notes, and snippets.

@claudiohilario
Created October 26, 2017 11:44
Show Gist options
  • Save claudiohilario/074368d9e0e88e4f5d2d34cca22db522 to your computer and use it in GitHub Desktop.
Save claudiohilario/074368d9e0e88e4f5d2d34cca22db522 to your computer and use it in GitHub Desktop.
teste UDP
/*
* https://social.msdn.microsoft.com/Forums/en-US/a038defe-2594-4c1a-85ef-7a98a0a409b2/can-i-call-async-methods-of-winmd-from-javascript?forum=winappswithhtml5
* === NOTAS ===
* Os nomes dos metodos devem estar em letra minuscula
*/
using System.IO;
using Windows.Networking.Sockets;
using Windows.Networking;
using System;
using Windows.Foundation;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Net.Http;
using Windows.Storage.Streams;
using System.Text;
namespace TestePlugin
{
public sealed class MyRuntimeComponent
{
// Variables Class
private static string response;
// Constructor
public void myruntimecomponent() {}
public static IAsyncActionWithProgress<Object> testasync()
{
return AsyncInfo.Run<Object>((token, result) =>
Task.Run<Object>(async () =>
{
string resposta = "Estado Inicial";
string message = "xxxxxxxxx";
int port = xxxx;
var socket = new DatagramSocket();
socket.MessageReceived += async (sender, args) =>
{
Stream streamIn = args.GetDataStream().AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
resposta = await reader.ReadLineAsync();
};
using (var stream = await socket.GetOutputStreamAsync(new HostName("255.255.255.255"), port.ToString()))
{
using (var writer = new DataWriter(stream))
{
var data = Encoding.UTF8.GetBytes(message);
writer.WriteBytes(data);
writer.StoreAsync();
}
}
// do this asynchronously ...
//return "Aqui tens a resposta";
Task.Delay(5000);
//response = "Aqui tens a resposta1";
response = resposta;
return "";
}
));
}
public static string getmyresponse()
{
return response;
}
public async void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
//Read the message that was received from the UDP echo server.
Stream streamIn = args.GetDataStream().AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
string message = await reader.ReadLineAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment