Ejemplo n.º 1
0
        static void MessengerListener()
        {
            ImageCodecInfo    encoder    = ImageCodecInfo.GetImageEncoders().First(i => i.FormatID == ImageFormat.Jpeg.Guid);
            EncoderParameters parameters = new EncoderParameters(1);

            parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 30L);

            using (MessengerClient client = new MessengerClient("api.ahacafe.vn", 100))
            {
                MessengerRegister(client);

                DateTime lastScreenRequest = DateTime.UtcNow;

                Parallel.Start(() =>
                {
                    while (true)
                    {
                        DateTime start = DateTime.UtcNow;

                        if ((DateTime.UtcNow - lastScreenRequest).TotalSeconds < 10)
                        {
                            using (Bitmap screen = Device.Screen)
                            {
                                if (screen == null)
                                {
                                    client.Delete("screen").Wait();
                                }
                                else
                                {
                                    using (MemoryStream ms = new MemoryStream())
                                    {
                                        screen.Save(ms, encoder, parameters);
                                        client.Pusblish("screen", Convert.ToBase64String(ms.ToArray())).Wait();
                                    }
                                }
                            }
                        }

                        Thread.CurrentThread.Join(Math.Max(1, 40 - (int)(DateTime.UtcNow - start).TotalMilliseconds));
                    }
                });

                client.ChannelTerminated += () => MessengerRegister(client);

                client.PackageReceived += pkg =>
                {
                    switch (pkg.Type)
                    {
                    case "pull": lastScreenRequest = DateTime.UtcNow; break;

                    case "push":

                        string[] cmds = pkg.Data.Split(' ');

                        if (cmds.Length == 3 && int.TryParse(cmds[1], out int x) && int.TryParse(cmds[2], out int y))
                        {
                            switch (cmds[0])
                            {
                            case "tap": Device.Tap(x, y); break;

                            case "press": Device.Press(x, y); break;

                            case "move": Device.MoveTo(x, y); break;

                            case "release": Device.Release(); break;
                            }
                        }

                        break;

                    case "kill":

                        try
                        {
                            foreach (Process process in Process.GetProcessesByName("MEmu"))
                            {
                                process.Kill();
                            }
                        }
                        catch (Exception e)
                        {
                        }

                        break;
                    }
                };

                while (true)
                {
                    Thread.CurrentThread.Join(1000);
                }
            }
        }