直到今天我还没有使用 gRPC。
由于我花时间尝试模拟此错误,因此我将在此处发布我的答案,但我所有的英特尔都由 @ishann 的答案领导,我已经投票了,这应该是接受了一个。
我刚刚试过dart hello world example。
server 在我的机器上运行,client 作为 Flutter 应用程序运行。
当我不运行服务器时,我得到了错误
gRPC Error (14, Error connecting: SocketException:
但是一旦服务器启动,一切都开始按预期工作,但后来我意识到我每次都在重新创建频道,所以这不是 OP 场景。
这是我的第一个 Flutter 代码:
void _foo() async {
final channel = new ClientChannel('192.168.xxx.xxx',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new GreeterClient(channel);
final name = 'world';
var _waitHelloMessage = true;
while (_waitHelloMessage) {
try {
final response = await stub.sayHello(new HelloRequest()..name = name);
print('Greeter client received: ${response.message}');
_waitHelloMessage = false;
} catch (e) {
print('Caught error: $e');
sleep(Duration(seconds: 1));
}
}
print('exiting');
await channel.shutdown();
}
如果我将设备置于 airplain 模式,然后再切换回正常的 wifi/lte 连接,则行为相同。
使用这个其他游乐场项目,我已经复制了任一
Caught error: gRPC Error (14, Error making call: Bad state: The http/2 connection is no longer active and can therefore not be used to make new streams.)
如果不重新创建频道,您就无法从中提出,并且
Caught error: gRPC Error (14, Error connecting: SocketException: OS Error: Connection refused, errno = 111, address = 192.168.1.58, port = 38120)
(例如关闭服务器),您可以从中重新启动,而无需重新创建频道。
前一个错误码不太容易得到,因为似乎wifi和lte连接之间的通道节流。
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_test_grpc/grpc/generated/helloworld.pbgrpc.dart';
import 'package:grpc/grpc.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
ClientChannel _channel;
@override
void dispose() {
_shutdown();
super.dispose();
}
void _shutdown() async {
if (null != _channel) {
print('shutting down...');
await _channel.shutdown();
print('shut down');
_channel = null;
} else {
print ('connect first');
}
}
void _connect() {
print('connecting...');
_channel = new ClientChannel('192.168.xxx.xxx',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
print('connected');
}
void _sayHello() async {
if (_channel != null) {
final stub = new GreeterClient(_channel);
final name = 'world';
try {
final response = await stub.sayHello(new HelloRequest()..name = name);
print('Greeter client received: ${response.message}');
} catch (e) {
print('Caught error: $e');
//sleep(Duration(seconds: 2));
}
//print('exiting');
//await channel.shutdown();
} else {
print('connect first!');
}
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: Padding(
padding: const EdgeInsets.only(left: 36.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
onPressed: _connect,
tooltip: 'Increment',
child: Icon(Icons.wifi),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
onPressed: _sayHello,
tooltip: 'Increment',
child: Icon(Icons.send),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
onPressed: _shutdown,
tooltip: 'Increment',
child: Icon(Icons.close),
),
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
这是我的flutter doctor -v,如果有帮助的话:
$ flutter doctor -v
[✓] Flutter (Channel beta, v1.0.0, on Mac OS X 10.14.1 18B75, locale en-IT)
• Flutter version 1.0.0 at /Users/shadowsheep/flutter/flutter
• Framework revision 5391447fae (6 weeks ago), 2018-11-29 19:41:26 -0800
• Engine revision 7375a0f414
• Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
• Android SDK at /Users/shadowsheep/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.1, Build version 10B61
• ios-deploy 1.9.4
• CocoaPods version 1.5.3
[✓] Android Studio (version 3.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 31.3.3
• Dart plugin version 182.5124
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
[✓] VS Code (version 1.30.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 2.21.1
[✓] Connected device (1 available)
[...]
• No issues found!