entangle 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 47

entangle #

entangle 取至量子纠缠(Quantum Entanglement)

entangle 是基于 websocket 运行在 dart VM 的服务端框架

在 flutter 中, 与之配套的有 entangle-client 可以在客户端更简洁的调用 entangle, 并且处理好了断线重连, 心跳重连等机制

entangle-client 配合 entangle 能让我们像在本机异步处理对象一样访问服务器的函数, 并传递 json 对象

其他非 dart 语言的客户端亦可以直接使用 websocket 进行调用

Server: #

import 'package:entangle/entangle.dart';

class Context {
  String name = 'i am a context data';
}

void main() async {
  var entangle = Entangle({
    'hello-world': hello,
  }, context: Context());

  entangle.listen(port: 5000, path: '/ws');
}

// controller example
void hello(Context ctx, data, send) {
  send({'msg': '${data['msg']}-world', 'ctx-name': ctx.name});

  // if other == 100, send message at 1000ms ago
  if (data['other'] == 100) {
    Future.delayed(Duration(milliseconds: 1000), () {
      send({'other': 'server take the initiative at 1000ms ago'});
    });
  }
}

client: #

import 'package:entangle/entangle-client.dart';

void clientApp() async {
  // wait server runing
  await Future.delayed(Duration(milliseconds: 500), () {});

  await entangleClient.connect('ws://127.0.0.1:5000/ws');

  // heart check at 1500ms, if connect.close, auto conect again;
  entangleClient.connectHeart(1500);

  /// Simple send data, and wait server callback twice
  ///
  /// Example:
  entangleClient.send(
    controller: 'hello-world',
    data: {
      "msg": "helloA",
      "other": 100,
    },
    listen: (data, remove) {
      print('client-get-msg-1: $data');
    },
  );

  ///  Only run once listen ad every send();
  ///
  /// Example:
  entangleClient.send(
    controller: 'hello-world',
    data: {
      "msg": "helloB",
      "other": false,
    },
    listen: (data, remove) {
      print('client-get-msg-2: $data');

      // remove this listen
      remove();
    },
  );

  /// timeOut Feature
  ///
  /// Example:
  entangleClient.send(
    controller: 'hello-world',
    data: {
      "msg": "helloB",
      "other": false,
    },
    listen: (data, clear) {
      print('client-get-msg-2: $data');
    },
    timeOutMilliseconds: 5000,
  );

  /// Auto connect again
  ///
  /// Example:
  Future.delayed(Duration(milliseconds: 1500), () async {
    print('close');
    await entangleClient.client.close();

    // Auto connect at entangleClient.send;
    entangleClient.send(
      controller: 'hello-world',
      data: {"msg": "helloA"},
      listen: (data, clear) {
        print('client-get-msg-again: $data');
      },
    );
  });
}

[0.1.2] - Change API #

[0.1.1] - Change API #

Change API:

old: send(String controller, Map data, {Function listen}) new: send({String controller, Map data, Function listen})

[0.1.0] - Update readme #

[0.0.2] - Make Features #

[0.0.1] - init #

  • TODO: Describe initial release.

example/example.dart

import 'package:entangle/entangle.dart';
import './example-client.dart';

class Context {
  String name = 'i am a context data';
}

void main() async {
  var entangle = Entangle({
    'hello-world': hello,
  }, context: Context());

  entangle.listen(port: 5000, path: '/ws');

  //simulation client
  clientApp();
}

// controller example
void hello(Context ctx, data, send) {
  send({'msg': '${data['msg']}-world', 'ctx-name': ctx.name});

  // if other == 100, send message at 1000ms ago
  if (data['other'] == 100) {
    Future.delayed(Duration(milliseconds: 1000), () {
      send({'other': 'server take the initiative at 1000ms ago'});
    });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  entangle: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:entangle/entangle.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

We analyzed this package on Jun 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.9+1

Analysis suggestions

Package not compatible with runtime flutter-web of web

Because of the import of dart:io via the import chain package:entangle/entangle.dart->dart:io

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:entangle/entangle.dart->dart:io

Health suggestions

Fix lib/entangle.dart. (-1.49 points)

Analysis of lib/entangle.dart reported 3 hints:

line 31 col 9: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 41 col 11: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 59 col 14: Unnecessary new keyword.

Fix lib/entangle-daemon.dart. (-1 points)

Analysis of lib/entangle-daemon.dart reported 2 hints:

line 1 col 8: Unused import: 'dart:io'.

line 4 col 8: Unused import: 'package:entangle/entangle.dart'.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

exitCode: 255 stdout: Documenting entangle... Initialized dartdoc with 40 libraries in 38.4 seconds Generating docs for library entangle from package:entangle/entangle.dart... Generating docs for library entangle from package:entangle/entangle-client.dart... Generating docs for library entangle-daemon from package:entangle/entangle-daemon.dart... Validating docs... Documented 3 public libraries in 6.5 seconds stderr: warning: entangle has no library level documentation comments, from entangle: (file:///tmp/pub-dartlang-dartdocGMRHJQ/pkg/lib/entangle.dart:1:9) warning: entangle has no library level documentation comments, from entangle: (file:///tmp/pub-dartlang-dartdocGMRHJQ/pkg/lib/entangle-client.dart:1:9) error: failed to write file at: entangle/entangle-library.html for symbol entangle: (file:///tmp/pub-dartlang-dartdocGMRHJQ/pkg/lib/entangle-client.dart:1:9) conflicting with file already generated by entangle: (file:///tmp/pub-dartlang-dartdocGMRHJQ/pkg/lib/entangle.dart:1:9) Dartdoc generates a path and filename to write to for each symbol. entangle conflicts with another symbol in the generated path, and therefore can not be written out. Changing the name, library name, or class name (if appropriate) of one of the conflicting items can resolve the conflict. Alternatively, use the @nodoc tag in one symbol's documentation comments to hide it. warning: dartdoc generated a broken link to: entangle/Entangle-class.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/Entangle.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/context.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/controllers.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/handleMessage.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/listen.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/webSocketListen.html (from index.json) warning: dartdoc generated a broken link to: entangle/Entangle/webSocketRequest.html (from index.json) warning: dartdoc generated a broken link to: entangle/entangleGetPaths.html (from index.json) found 11 warnings and 1 error Unhandled exception: dartdoc encountered 1 errors while processing. #0 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:225:9)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0