entangle 0.1.2

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]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

We analyzed this package on Aug 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:entangle/entangle.dart with components: io.

Health issues and suggestions

Document public APIs. (-1 points)

16 out of 16 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

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'.

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

Analysis of lib/entangle.dart reported 2 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.

Dependencies

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