Exemple #1
0
        private void button1_Click(object sender1, EventArgs e1)
        {
            try
            {
                string destIP    = this.cmbIP.Text;
                string patternIp = @"\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b";
                if (destIP.Length <= 0 || !Regex.IsMatch(destIP, patternIp))
                {
                    return;
                }
                //ws = new WebSocket("ws://localhost:6110/Client");
                ws = new WebSocket("ws://" + destIP + ":4650");

                ws.OnOpen += (sender, e) =>
                {
                    //ws.Send("Hi, all!");
                    Debug.WriteLine("OnOpen => ...");
                };

                ws.OnMessage += (sender, e) =>
                {
                    string msg = e.Data;
                    Debug.WriteLine("OnMessage => " + msg);
                    command cmd_temp = (command)JsonConvert.DeserializeObject(msg, typeof(command));
                    IDevice device   = findDevice(cmd_temp.TargetDevice);
                    if (device == null)
                    {
                        switch (cmd_temp.TargetDevice)
                        {
                        case TargetDeiveName.电风扇:
                            device = new FanDevice();
                            break;

                        case TargetDeiveName.电机:
                            device = new EngineDevice();
                            break;

                        case TargetDeiveName.绿灯:
                            device = new GreenLightDevice();
                            break;

                        case TargetDeiveName.红灯:
                            device = new RedLightDevice();
                            break;

                        case TargetDeiveName.黄灯:
                            device = new YellowLightDevice();
                            break;

                        case TargetDeiveName.GPS:
                            device = new GPSDevice();
                            break;

                        case TargetDeiveName.UHF:
                            device = new UHFDevice();
                            break;
                        }
                    }
                    if (device != null)
                    {
                        deviceList.Add(device);
                        device.setDevice(cmd_temp, cmd =>
                        {
                            string back = JsonConvert.SerializeObject(cmd);
                            Debug.WriteLine("*****  OnMessage <= " + back);
                            ws.Send(back);
                        });
                    }
                    //ws.Send(JsonConvert.SerializeObject(cmd_temp));
                };

                ws.OnError += (sender, e) =>
                {
                    Debug.WriteLine("OnError => " + e.Message);
                };

                ws.OnClose += (sender, e) =>
                {
                    Debug.WriteLine("OnClose => " + e.Data);
                };

                ws.Connect();


                //command cmd1 = new command(stateName.打开, "");
                //cmd1.TargetDevice = TargetDeiveName.绿灯;
                //cmd1.Initializing = "true";
                //this.initialCommandList.Add(cmd1);
                //sendInitialInfo(ws);
                //MCserver = new WebSocketServer(MCServerUrl);
                //MCserver.Start(socket =>
                //{
                //    socket.OnOpen = () =>
                //    {
                //        host.OnOpenWebSocket(socket);
                //    };
                //    socket.OnClose = () =>
                //    {
                //        host.OnCloseWebSocket(socket);
                //    };
                //    socket.OnMessage = message =>
                //    {
                //        host.OnMessage(message, socket);
                //    };
                //});


                this.initial_udp_server(Program.inputPort);
                检查设备状态(Program.getRemoteIPEndPoint(), 3000);

                this.button2.Enabled = true;
                this.button1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #2
0
        private void button1_Click(object sender1, EventArgs e1)
        {
            try
            {
                string destIP = this.cmbIP.Text;
                string patternIp = @"\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b";
                if (destIP.Length <= 0 || !Regex.IsMatch(destIP, patternIp))
                {
                    return;
                }
                //ws = new WebSocket("ws://localhost:6110/Client");
                ws = new WebSocket("ws://" + destIP + ":4650");

                ws.OnOpen += (sender, e) =>
                {
                    //ws.Send("Hi, all!");
                    Debug.WriteLine("OnOpen => ...");
                };

                ws.OnMessage += (sender, e) =>
                {
                    string msg = e.Data;
                    Debug.WriteLine("OnMessage => " + msg);
                    command cmd_temp = (command)JsonConvert.DeserializeObject(msg, typeof(command));
                    IDevice device = findDevice(cmd_temp.TargetDevice);
                    if (device == null)
                    {
                        switch (cmd_temp.TargetDevice)
                        {
                            case TargetDeiveName.电风扇:
                                device = new FanDevice();
                                break;
                            case TargetDeiveName.电机:
                                device = new EngineDevice();
                                break;
                            case TargetDeiveName.绿灯:
                                device = new GreenLightDevice();
                                break;
                            case TargetDeiveName.红灯:
                                device = new RedLightDevice();
                                break;
                            case TargetDeiveName.黄灯:
                                device = new YellowLightDevice();
                                break;
                            case TargetDeiveName.GPS:
                                device = new GPSDevice();
                                break;
                            case TargetDeiveName.UHF:
                                device = new UHFDevice();
                                break;
                        }
                    }
                    if (device != null)
                    {
                        deviceList.Add(device);
                        device.setDevice(cmd_temp, cmd =>
                        {
                            string back = JsonConvert.SerializeObject(cmd);
                            Debug.WriteLine("*****  OnMessage <= " + back);
                            ws.Send(back);
                        });
                    }
                    //ws.Send(JsonConvert.SerializeObject(cmd_temp));
                };

                ws.OnError += (sender, e) =>
                {
                    Debug.WriteLine("OnError => " + e.Message);

                };

                ws.OnClose += (sender, e) =>
                {
                    Debug.WriteLine("OnClose => " + e.Data);

                };

                ws.Connect();

                //command cmd1 = new command(stateName.打开, "");
                //cmd1.TargetDevice = TargetDeiveName.绿灯;
                //cmd1.Initializing = "true";
                //this.initialCommandList.Add(cmd1);
                //sendInitialInfo(ws);
                //MCserver = new WebSocketServer(MCServerUrl);
                //MCserver.Start(socket =>
                //{
                //    socket.OnOpen = () =>
                //    {
                //        host.OnOpenWebSocket(socket);
                //    };
                //    socket.OnClose = () =>
                //    {
                //        host.OnCloseWebSocket(socket);
                //    };
                //    socket.OnMessage = message =>
                //    {
                //        host.OnMessage(message, socket);
                //    };
                //});

                this.initial_udp_server(Program.inputPort);
                检查设备状态(Program.getRemoteIPEndPoint(), 3000);

                this.button2.Enabled = true;
                this.button1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }