项目作者: empirefox

项目描述 :
flutter dial golang
高级语言: Go
项目地址: git://github.com/empirefox/flutter_dial_go.git
创建时间: 2018-09-13T10:36:37Z
项目社区:https://github.com/empirefox/flutter_dial_go

开源协议:Other

下载


flutter_dial_go

A flutter plugin for connecting to golang embeded servers via platform channel.

Getting Started

Example

  1. make -f ./go/Makefile protoc
  2. make -f ./go/Makefile bind-android
  3. make -f ./go/Makefile bind-ios
  4. flutter run

Install for golang

  1. go get -u github.com/empirefox/flutter_dial_go
  2. cd GOPATH/github.com/empirefox/flutter_dial_go/go
  3. make android

Install for flutter project

Add to pubsepc.yml, replace GOPATH with real path.

  1. dependencies:
  2. flutter_dial_go:
  3. path: GOPATH/github.com/empirefox/flutter_dial_go

Develop golang side

When write go code:
Only use github.com/empirefox/flutter_dial_go/go/forgo.
Do not use github.com/empirefox/flutter_dial_go/go/formobile.

  1. import "github.com/empirefox/flutter_dial_go/go/forgo"
  2. // dart usage: var conn = await Conn.dial(9999)
  3. listener, err := forgo.Listen(9999)
  • Implement gomobile package exactly like: Go Example.
  • Copy Makefile to flutter project, then replace the GOMOBILE_PKG, APP_PATH and protoc:.
  • Build with the new Makefile.
  • For Android Studio: import go.aar and add api project(':go') to PROJECT_DIR$/android/app/build.gradle.
  • For Xcode: add PODS_ROOT/../Frameworks to all Framework search paths.

Develop flutter side

Init with no service.

  1. import 'package:flutter_dial_go/flutter_dial_go.dart';
  2. Future initGo() async {
  3. await Conn.startGo();
  4. }

Or init with foreground service. For ios, service will not start and it will work like above.

  1. import 'package:flutter_dial_go/flutter_dial_go.dart';
  2. Future initGo() async {
  3. await Conn.notificationChannel(
  4. channelId: channelId,
  5. importance: importance,
  6. name: 'fdg running',
  7. description: 'keep fdg running',
  8. );
  9. await Conn.startGoWithService(
  10. channelId: channelId,
  11. notificationId: notificationId,
  12. title: 'flutter_dial_go example',
  13. text: 'make flutter dial go',
  14. );
  15. }

Then dial:

  1. // raw http request
  2. // golang: forgo.Listen(9998)
  3. Conn c = await Conn.dial(9998);
  4. print('GET /\n');
  5. c.receiveStream
  6. .fold(BytesBuilder(), (BytesBuilder a, List<int> b) => a..add(b))
  7. .then((a) => setState(() {
  8. _result = 'GET / HTTP/1.0\n\n' + utf8.decode(a.takeBytes());
  9. }))
  10. .catchError((e) => setState(() {
  11. _result = 'Caught http error: e';
  12. }))
  13. .then((_) => c.close());
  14. c.add(utf8.encode('GET / HTTP/1.0\r\n\r\n'));

Or http2:

  1. // golang: forgo.Listen(9997)
  2. Conn c = await Conn.dial(9997);
  3. var transport = ClientTransportConnection.viaStreams(c.receiveStream, c);

Or grpc:

  1. Future<Http2Streams> _connect(String host, int port, ChannelCredentials credentials) async {
  2. // ignore: close_sinks
  3. final conn = await Conn.dial(port);
  4. return Http2Streams(conn.receiveStream, conn);
  5. }
  6. var channel = ClientChannel(
  7. 'go',
  8. port: 9999,
  9. options: ChannelOptions(
  10. credentials: ChannelCredentials.insecure(),
  11. connect: _connect,
  12. ),
  13. );
  14. var stub = GreeterClient(_channel);
  15. ...
  16. channel.terminate();

Flutter Example.