socket_io_common_client 0.10.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

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]
72
Health:
Code health derived from static analysis. [more]
42
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: web

Platform components identified in package: html, io, js.

Health suggestions

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

Analysis of lib/src/manager.dart reported 38 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 26 col 1: Prefer using /// for doc comments.

line 62 col 44: Don't type annotate initializing formals.

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

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

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

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

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

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

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

Fix lib/src/socket.dart. (-9.99 points)

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

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

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

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

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

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

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

Additional issues in the following files:

  • lib/src/engine/transport/fe/fe_xhr_transport.dart (20 hints)
  • lib/src/engine/transport/vmv/xhr_transport.dart (20 hints)
  • lib/src/engine/transport/polling_transport.dart (10 hints)
  • lib/src/engine/transport/transports.dart (8 hints)
  • lib/src/engine/transport/fe/jsonp_transport.dart (7 hints)
  • lib/src/engine/transport/vmv/websocket_transport.dart (5 hints)
  • lib/src/engine/transport/fe/fe_websocket_transport.dart (4 hints)
  • lib/src/client/socket_io_basic_client.dart (3 hints)
  • lib/src/client/socket_io_browser_client.dart (3 hints)
  • lib/src/engine/parseqs.dart (3 hints)
  • lib/src/client/socket_io_vm_client.dart (2 hints)
  • lib/socket_io_client.dart (1 hint)
  • lib/socket_io_client_for_browser.dart (1 hint)
  • lib/src/on.dart (1 hint)

Dependencies

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