public MainViewModel(ISomeService someService)
        {
            receiver = new UdpSocketReceiver();
          
            receiver.MessageReceived += OnMessageReceived;


            Messages = new ObservableCollection<UDPMessage>();

            Task.Run(() => Init().Wait());

        }
示例#2
0
        public async Task<byte[]> Receive(IPEndPoint remoteHost)
        {
            var receiver = new UdpSocketReceiver();
            byte[] data = new byte[1];

            receiver.MessageReceived += (sender, args) =>
            {
                // get the remote endpoint details and convert the received data into a string
                data = args.ByteData;
            };
            await receiver.StartListeningAsync(remoteHost.Port);
            System.Console.WriteLine(System.DateTime.Now.ToString() + "<<< " + remoteHost);
            System.Console.WriteLine(FormatterHelper.Deserialize(data).ToString());
            System.Console.WriteLine();
            return data;
        }
示例#3
0
        public async void Connect()
        {
            try
            {
                if (UdpClient != null)
                {
                    try
                    {
                        await UdpClient.StopListeningAsync();
                        UdpClient.Dispose();
                    }
                    catch(Exception ex)
                    {
                        if (IsDebug)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                    }
                }

                UdpClient = new UdpSocketReceiver();
                UdpClient.MessageReceived += OnMessageReceived;
            }
            catch (SocketException ex)
            {
                if (IsDebug)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    return;
                }
            }

            await UdpClient.StartListeningAsync(LocalEndpoint.Port);

            try
            {
                ConnectRequest();
            }
            catch(Exception ex)
            {
                if (IsDebug)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
        public UdpSocketReceiverPage()
        {
            _receiver = new UdpSocketReceiver();
            _messagesSub = new Subject<Message>();
            _messagesObs = _messagesSub.AsObservable();

            _receiver.MessageReceived += (sender, args) =>
            {
                var data = args.ByteData.ToStringFromUTF8Bytes();
                var from = String.Format("{0}:{1}", args.RemoteAddress, args.RemotePort);

                var msg = new Message
                {
                    Text = data,
                    DetailText = String.Format("<Received from {1} at {0}>", DateTime.Now.ToString("HH:mm:ss"), from)
                };

                _messagesSub.OnNext(msg);
            };

            InitView();
        }
 public ServerConnection()
 {
     _udpReceiver = new UdpSocketReceiver();
     _udpReceiver.StartListeningAsync(9875);
     _udpReceiver.MessageReceived += MessageReceivedCallback;
 }