private static void OnMessageReceived(object sender, MessageEventArgs e) { if (e.IsPing) { Console.WriteLine("We were pinged"); } else if (e.IsBinary) { Console.WriteLine("Got binary response from websocket"); var response = BytesResponsePayload.Deserialize(e.RawData); Console.WriteLine($"Success: {response.IsSuccess}"); Console.WriteLine($"Metadata: {response.Metadata.Serialize()}"); if (response.Metadata.Source == typeof(DisplayScreen)) { using var ms = new MemoryStream(response.Payload); var displayCapture = Image.FromStream(ms); DrawImage.ConsoleWriteImage((Bitmap)displayCapture); } else { Console.WriteLine($"Raw Payload: {GetPrettyByteArrayString(response.Payload)}"); } } else if (e.IsText) { Console.WriteLine("Got text response from websocket"); Console.WriteLine($"Response: {e.Data}"); } }
public RemoteControlPage() { InitializeComponent(); //ScreenCapture.BitmapOptimizations = true; //ScreenCapture.DownsampleToViewSize = true; //ScreenCapture.Finish += ScreenCapture_Finish; //ScreenCapture.Source = new StreamImageSource() //{ // Stream = async (token) => await Task.FromResult<Stream>(new MemoryStream(imageBytes)) //}; canvasView = new SKCanvasView(); canvasView.PaintSurface += OnCanvasViewPaintSurface; Content = canvasView; ClientConnection.Instance.Connection.OnMessageReceived += async(sender, e) => { await Task.Run(() => { Debug.WriteLine("Got message"); if (e.IsBinary) { var response = BytesResponsePayload.Deserialize(e.RawData); if (response.Metadata.Source == typeof(DisplayScreen)) { // TODO - This won't work. High GC overhead and serious latency. Take a look at SkiaSharp! //ScreenCapture.Source = ImageSource.FromStream(() => new MemoryStream(response.Payload)); //ScreenCapture.ReloadImage(); //viewModel.UpdateImage(response.Payload); skBitmap = SKBitmap.Decode(response.Payload); canvasView.InvalidateSurface(); } } }); }; //Commands.ExecuteRemotely(ClientConnection.Instance.Connection, new Request(nameof(DisplayScreen))); cancellationToken = new CancellationToken(); Task.Run(async() => { while (!cancellationToken.IsCancellationRequested) { Commands.ExecuteRemotely(ClientConnection.Instance.Connection, new Request(nameof(DisplayScreen))); await Task.Delay(16); } }); }