コード例 #1
0
        private void HandleMethodCall(MethodCall call, MethodChannel.IResult result)
        {
            // Return an error if Flutter is invoking method calls through method channel
            // when bridge is configured for WebSocket communication
            if (Mode == FlutnetBridgeMode.WebSocket)
            {
                result.NotImplemented();
                return;
            }

            // Extract target method information from MethodCall.Method
            FlutnetMethodInfo methodInfo;
            Object            dartReturnValue;

            try
            {
                methodInfo      = JsonConvert.DeserializeObject <FlutnetMethodInfo>(call.Method, FlutterInterop.JsonSerializerSettings);
                dartReturnValue = FlutterInterop.ToMethodChannelResult(0);
            }
            catch (Exception ex)
            {
                result.Error(FlutnetErrorCode.OperationNotImplemented.ToString(), ex.Message, null);
                return;
            }

            // Send an empty - successful - response to immediately free Flutter thread
            result.Success(dartReturnValue);
            Task.Run(() => { BackgroundHandleMethodCall(methodInfo, call); });
        }
            // callback from Flutter with a method...
            public async void OnMethodCall(MethodCall methodCall, MethodChannel.IResult result)
            {
                try
                {
                    Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "Vistian.Flutter.Remoting.Droid.Example", $"Message received {methodCall.Method} for {_handler.GetType().Name}");
                    Stopwatch stopWatch = new Stopwatch();

                    stopWatch.Start();

                    // decode the method details from the method call
                    var arg = methodCall.Argument(DetailsArgumentName);


                    var t0 = stopWatch.ElapsedTicks;
                    var ns = arg.ToString();


                    var t1 = stopWatch.ElapsedTicks;
                    // invoke the registered handler for this method and the message details
                    var messagingResult = await _handler.Execute(methodCall.Method, ns).ConfigureAwait(false);

                    var t2 = stopWatch.ElapsedTicks;
                    // serialize up the message result,
                    var jsonMessagingResult = JsonConvert.SerializeObject(messagingResult);
                    var t3 = stopWatch.ElapsedTicks;
                    stopWatch.Stop();

                    Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "Vistian.Flutter.Remoting.Droid.Example", $"Messaging Handling took {stopWatch.ElapsedMilliseconds} {t0} {t1} {t2} {t3} freq: {Stopwatch.Frequency}");
                    // and send it back
                    result.Success(jsonMessagingResult);
                }
                catch (Exception exception)
                {
                    // report the error back to flutter
                    result.Error(exception.GetType().Name, exception.Message, exception.Source);
                }
            }