socket_io_common_client 0.10.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 63

socket.io-client-dart #

Port of awesome JavaScript Node.js library - Socket.io-client v2.0.1 - in Dart

Usage(For not browser platform) #

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:socket_io_common_client/socket_io_client.dart' as IO;
import 'package:logging/logging.dart';

class ReadSender implements StreamConsumer<List<int>> {
  IO.Socket socket;

  ReadSender(IO.Socket this.socket);

  @override
  Future addStream(Stream<List<int>> stream) {
    return stream.transform(utf8.decoder).forEach((content){
      print(content);
      this.socket.emit("chat message",content);
    }).timeout(Duration(days: 30));
  }

  @override
  Future close() {
    return null;
  }
}

main() async {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });
  stdout.writeln('Type something');

  List<String> cookie = null;

  IO.Socket socket = IO.io('https://sandbox.ssl2.duapps.com', {
    'secure': false,
    'path': '/socket.io',
    'transports': ['polling'],
    'request-header-processer': (requestHeader) {
      print("get request header " + requestHeader.toString());
      if (cookie != null) {
        requestHeader.add('cookie', cookie);
        print("set cookie success");
      }else{
        print("set cookie faield");
      }
    },
    'response-header-processer': (responseHeader) {
      print("get response header " + responseHeader.toString());
      if ( responseHeader['set-cookie'] != null) {
        cookie = responseHeader['set-cookie'];
        print("receive cookie success");
      } else {
        print("receive cookie failed");
      }
    },
  });
  socket.on('connect', (_) {
    print('connect happened');
    socket.emit('chat message', 'init');
  });
  socket.on('req-header-event', (data) {
    print("req-header-event " + data.toString());
  });
  socket.on('resp-header-event', (data) {
    print("resp-header-event " + data.toString());
  });
  socket.on('event', (data) => print("received " + data));
  socket.on('disconnect', (_) => print('disconnect'));
  socket.on('fromServer', (_) => print(_));
  await stdin.pipe(ReadSender(socket));
}

Usage(For Browser) #

import 'package:socket_io_common_client/socket_io_browser_client.dart' as
BrowserIO;
import 'package:logging/logging.dart';

main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });

  BrowserIO.Socket socket = BrowserIO.io('ws://localhost:3000', {
    'transports': ['polling','websocket'],
    'secure': false
  });

  socket.on('connect', (_) {
    print('connect happened');
    socket.emit('chat message', 'init');
  });
  socket.on('event', (data) => print("received "+data));
  socket.on('disconnect', (_) => print('disconnect'));
  socket.on('fromServer', (_) => print(_));
}

Notes to Contributors #

Notes to Users #

This tool is develope for my team which try to use flutter build an app.
Main for internal use.Open source for other who want to use it. 

Thanks #

0.10.0 #

New Features #

  • Allow user deal with polling request/response headers

Breaking Changes #

  • change file name and method name for web usage:
socket_io_browser_client.dart => socket_io_client_for_browser.dart
io => ioBrowser

0.9.5 #

  • 一个能用的版本

example/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:socket_io_common_client/socket_io_client.dart' as IO;
import 'package:logging/logging.dart';

class ReadSender implements StreamConsumer<List<int>> {
  IO.Socket socket;

  ReadSender(IO.Socket this.socket);

  @override
  Future addStream(Stream<List<int>> stream) {
    return stream.transform(utf8.decoder).forEach((content) {
      print(content);
      this.socket.emit("chat message", content);
    }).timeout(Duration(days: 30));
  }

  @override
  Future close() {
    return null;
  }
}

main() async {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });
  stdout.writeln('Type something');

  List<String> cookie = null;

  IO.Socket socket = IO.io('https://localhost:3000', {
    'secure': false,
    'path': '/socket.io',
    'transports': ['polling'],
    'request-header-processer': (requestHeader) {
      print("get request header " + requestHeader.toString());
      if (cookie != null) {
        requestHeader.add('cookie', cookie);
        print("set cookie success");
      }else{
        print("set cookie faield");
      }
    },
    'response-header-processer': (responseHeader) {
      print("get response header " + responseHeader.toString());
      if (responseHeader['set-cookie'] != null) {
        cookie = responseHeader['set-cookie'];
        print("receive cookie success");
      } else {
        print("receive cookie failed");
      }
    },
  });
  socket.on('connect', (_) {
    print('connect happened');
    socket.emit('chat message', 'init');
  });
  socket.on('req-header-event', (data) {
    print("req-header-event " + data.toString());
  });
  socket.on('resp-header-event', (data) {
    print("resp-header-event " + data.toString());
  });
  socket.on('event', (data) => print("received " + data));
  socket.on('disconnect', (_) => print('disconnect'));
  socket.on('fromServer', (_) => print(_));
  await stdin.pipe(ReadSender(socket));
}

Use this package as a library

1. Depend on it

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


dependencies:
  socket_io_common_client: ^0.10.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:socket_io_common_client/socket_io_client.dart';
import 'package:socket_io_common_client/socket_io_client_for_browser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
61
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
49
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/manager.dart. (-22.95 points)

Analysis of lib/src/manager.dart reported 52 hints, including:

line 14 col 23: Use lowercase_with_underscores when specifying a library prefix.

line 21 col 68: Use lowercase_with_underscores when specifying a library prefix.

line 22 col 57: Use lowercase_with_underscores when specifying a library prefix.

line 24 col 24: Unnecessary new keyword.

line 26 col 1: Prefer using /// for doc comments.

Fix lib/src/engine/socket.dart. (-14.39 points)

Analysis of lib/src/engine/socket.dart reported 31 hints, including:

line 23 col 24: Unnecessary new keyword.

line 25 col 1: Prefer using /// for doc comments.

line 95 col 7: DO use curly braces for all flow control structures.

line 103 col 27: Unnecessary new keyword.

line 126 col 9: DO use curly braces for all flow control structures.

Fix lib/src/engine/transport/fe/fe_xhr_transport.dart. (-10.89 points)

Analysis of lib/src/engine/transport/fe/fe_xhr_transport.dart reported 23 hints, including:

line 9 col 1: Prefer using /// for doc comments.

line 22 col 24: Unnecessary new keyword.

line 31 col 3: Prefer using /// for doc comments.

line 54 col 3: Prefer using /// for doc comments.

line 59 col 3: Prefer using /// for doc comments.

Fix additional 14 files with analysis or formatting issues. (-57.66 points)

Additional issues in the following files:

  • lib/src/socket.dart (23 hints)
  • lib/src/engine/transport/vmv/xhr_transport.dart (22 hints)
  • lib/src/engine/transport/polling_transport.dart (12 hints)
  • lib/src/engine/transport/fe/jsonp_transport.dart (11 hints)
  • lib/src/engine/transport/transports.dart (10 hints)
  • lib/src/client/socket_io_browser_client.dart (9 hints)
  • lib/src/client/socket_io_vm_client.dart (7 hints)
  • lib/src/engine/transport/fe/fe_websocket_transport.dart (7 hints)
  • lib/src/engine/transport/vmv/websocket_transport.dart (7 hints)
  • lib/src/client/socket_io_basic_client.dart (4 hints)
  • lib/src/engine/parseqs.dart (3 hints)
  • lib/src/on.dart (2 hints)
  • lib/socket_io_client.dart (1 hint)
  • lib/socket_io_client_for_browser.dart (1 hint)

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

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

exitCode: 255 stdout: parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/socket_io_client.dart... parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/socket_io_client_for_browser.dart... parsing /dart-sdk/lib/core/core.dart... parsing /dart-sdk/lib/collection/collection.dart... parsing /dart-sdk/lib/internal/internal.dart... parsing /dart-sdk/lib/async/async.dart... parsing /dart-sdk/lib/convert/convert.dart... parsing /dart-sdk/lib/typed_data/typed_data.dart... parsing /dart-sdk/lib/math/math.dart... parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/client/socket_io_vm_client.dart... parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/engine/transport/vmv/websocket_transport.dart... parsing /dart-sdk/lib/io/io.dart... parsing /dart-sdk/lib/developer/developer.dart... parsing /dart-sdk/lib/isolate/isolate.dart... parsing /dart-sdk/lib/_http/http.dart... parsing /tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/logging-0.11.4/lib/logging.dart... parsing /tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/logging-0.11.4/lib/src/log_record.dart... parsing /tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/logging-0.11.4/lib/src/level.dart... parsing /tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/logging-0.11.4/lib/src/logger.dart... parsing /tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/engine/parser/parser.dart... [...] parsing /dart-sdk/lib/svg/dart2js/svg_dart2js.dart... parsing /dart-sdk/lib/web_audio/dart2js/web_audio_dart2js.dart... parsing /dart-sdk/lib/web_sql/dart2js/web_sql_dart2js.dart... parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/engine/transport/fe/fe_xhr_transport.dart... parsing /tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/engine/transport/fe/jsonp_transport.dart... parsing /dart-sdk/lib/js/dart2js/js_dart2js.dart... parsing /dart-sdk/lib/js/_js.dart... parsing /dart-sdk/lib/ffi/ffi.dart... parsing /dart-sdk/lib/mirrors/mirrors.dart... parsing /dart-sdk/lib/html/dartium/nativewrappers.dart... parsing /dart-sdk/lib/cli/cli.dart... parsing /dart-sdk/lib/wasm/wasm.dart... parsing /dart-sdk/lib/_internal/js_runtime/lib/js_primitives.dart... parsing /dart-sdk/lib/_internal/js_runtime/lib/shared/async_await_error_codes.dart... Initialized dartdoc with 64 libraries in 29.9 seconds documenting socket_io_common_client Generating docs for library socket_io_client from package:socket_io_common_client/socket_io_client.dart... Generating docs for library socket_io_client from package:socket_io_common_client/socket_io_client_for_browser.dart... Validating docs... Documented 2 public libraries in 2.3 seconds stderr: Documenting socket_io_common_client... warning: ambiguous reexport of socket.EVENTS, canonicalization candidates: (socket_io_client, socket_io_client) -> socket_io_client (confidence 0.000) from socket.EVENTS: (file:///tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/socket.dart:26:12) socket_io_client: 1.010 - embeds package name (+1.000), name is long (+0.01000) socket_io_client: 1.010 - embeds package name (+1.000), name is long (+0.01000) Use {@canonicalFor socket.EVENTS} in the desired library's documentation to resolve the ambiguity and/or override dartdoc's decision, or structure your package so the reexport is less ambiguous. The symbol will still be referenced in all candidates -- this only controls the location where it will be written and which library will be displayed in navigation for the relevant pages. The flag --ambiguous-reexport-scorer-min-confidence allows you to set the threshold at which this warning will appear. warning: ambiguous reexport of socket.Socket, canonicalization candidates: (socket_io_client, socket_io_client) -> socket_io_client (confidence 0.000) from socket.Socket: (file:///tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/src/socket.dart:49:7) socket_io_client: 1.010 - embeds package name (+1.000), name is long (+0.01000) socket_io_client: 1.010 - embeds package name (+1.000), name is long (+0.01000) warning: no canonical library found for socket_io_client.Socket.hashCode, not linking, from socket_io_client.Socket.hashCode: (file:///dart-sdk/lib/core/object.dart:94:20) warning: no canonical library found for socket_io_client.Socket.runtimeType, not linking, from socket_io_client.Socket.runtimeType: (file:///dart-sdk/lib/core/object.dart:116:21) warning: no canonical library found for socket_io_client.Socket.clearListeners, not linking, from socket_io_client.Socket.clearListeners: (file:///tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/util/event_emitter.dart💯8) warning: no canonical library found for socket_io_client.Socket.hasListeners, not linking, from socket_io_client.Socket.hasListeners: (file:///tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/util/event_emitter.dart:108:8) warning: no canonical library found for socket_io_client.Socket.noSuchMethod, not linking, from socket_io_client.Socket.noSuchMethod: (file:///dart-sdk/lib/core/object.dart:111:20) warning: no canonical library found for socket_io_client.Socket.off, not linking, from socket_io_client.Socket.off: (file:///tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/util/event_emitter.dart:81:8) warning: no canonical library found for socket_io_client.Socket.on, not linking, from socket_io_client.Socket.on: (file:///tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/util/event_emitter.dart:63:8) warning: no canonical library found for socket_io_client.Socket.once, not linking, from socket_io_client.Socket.once: (file:///tmp/pub-cache-dirQEPOHI/hosted/pub.dartlang.org/socket_io_common-0.9.1/lib/src/util/event_emitter.dart:73:8) warning: no canonical library found for socket_io_client.Socket.toString, not linking, from socket_io_client.Socket.toString: (file:///dart-sdk/lib/core/object.dart:99:19) warning: no canonical library found for socket_io_client.Socket.==, not linking, from socket_io_client.Socket.==: (file:///dart-sdk/lib/core/object.dart:62:26) error: failed to write file at: socket_io_client/socket_io_client-library.html for symbol socket_io_client: (file:///tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/socket_io_client_for_browser.dart:14:9) conflicting with file already generated by socket_io_client: (file:///tmp/pub-dartlang-dartdocWNZNOR/pkg/lib/socket_io_client.dart:14:9) Dartdoc generates a path and filename to write to for each symbol. socket_io_client 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. found 12 warnings and 1 error Unhandled exception: dartdoc encountered 1 errors while processing. #0 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:202:7)

Package is getting outdated. (-21.10 points)

The package was last published 63 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
logging ^0.11.3+2 0.11.4
socket_io_common ^0.9.0+1 0.9.1
Dev dependencies
build_runner ^1.0.0
build_web_compilers ^0.4.4
socket_io any
test >=1.3.0 <2.0.0