Ejemplo n.º 1
0
        /// <summary>
        /// 接收UDP广播数据
        /// </summary>
        /// <returns></returns>
        private async Task UdpReceive()
        {
            while (true)
            {
                try
                {
                    var receivedResult = await udpSocket.ReceiveAsync();

                    string recvData = Encoding.ASCII.GetString(receivedResult.Buffer);
                    //解析返回数据
                    Dictionary <string, string> headers = new Dictionary <string, string>();
                    foreach (string line in recvData.Split(new string[] { "\r\n" }, StringSplitOptions.None))
                    {
                        if (line.IndexOf(':') != -1)
                        {
                            string key   = line.Substring(0, line.IndexOf(": "));
                            string value = line.Substring(line.IndexOf(": ") + 2);
                            headers.Add(key, value);
                        }
                    }

                    Lamp lamp = new Lamp(headers);

                    LampDiscoveryEventArgs eventArgs = new LampDiscoveryEventArgs(lamp);
                    OnDiscoveryLamp?.Invoke(this, eventArgs);
                }
                catch (ObjectDisposedException ex)
                {
                    // UDP Socket已释放 退出
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        private void OnDiscoveryLamp(object sender, EventArgs e)
        {
            LampDiscoveryEventArgs lampDiscoveryEvent = (LampDiscoveryEventArgs)e;

            lamp = lampDiscoveryEvent.lamp;
            lampDiscovery.Stop();
            MessageBox.Show("OK");
        }