socket_io_common_client 0.10.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new50

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';
  
Version Uploaded Documentation Archive
0.10.0 Jan 9, 2019 Go to the documentation of socket_io_common_client 0.10.0 Download socket_io_common_client 0.10.0 archive
0.9.5 Jan 8, 2019 Go to the documentation of socket_io_common_client 0.9.5 Download socket_io_common_client 0.9.5 archive
0.9.0+1 Jan 8, 2019 Go to the documentation of socket_io_common_client 0.9.0+1 Download socket_io_common_client 0.9.0+1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.10

Platforms

Detected platforms: web

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

Health suggestions

Format lib/src/client/socket_io_browser_client.dart.

Run dartfmt to format lib/src/client/socket_io_browser_client.dart.

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+1
Transitive dependencies
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
typed_data 1.1.6
uuid 1.0.3 2.0.0-rc1
Dev dependencies
build_runner ^1.0.0
build_web_compilers ^0.4.4
socket_io any
test >=1.3.0 <2.0.0