From ca3cceaa644f3304c31eb7adb1e32c6a4303e0bc Mon Sep 17 00:00:00 2001 From: Jumper Chen Date: Wed, 19 Feb 2020 11:54:39 +0800 Subject: [PATCH] 1. add more example into example/README.md 2. add @luandnguyen contributor for https://github.com/rikulo/socket.io-client-dart/issues/59 --- README.md | 3 +- example/README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 54fca61..bf15a28 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ To connect the socket manually, set the option `autoConnect: false` and call `.c For example,
-Socket socket = io('http://localhost:3000', {
+Socket socket = io('http://localhost:3000', <String, dynamic>{
     'transports': ['websocket'],
     'autoConnect': false,
     'extraHeaders': {'foo': 'bar'} // optional
@@ -145,3 +145,4 @@ If you are new to Git or GitHub, please read [this guide](https://help.github.co
 * Thanks [@Oskang09](https://github.com/Oskang09) for https://github.com/rikulo/socket.io-client-dart/issues/21
 * Thanks [@bruce3x](https://github.com/bruce3x) for https://github.com/rikulo/socket.io-client-dart/issues/25
 * Thanks [@Kavantix](https://github.com/Kavantix) for https://github.com/rikulo/socket.io-client-dart/issues/26
+* Thanks [@luandnguyen](https://github.com/luandnguyen) for https://github.com/rikulo/socket.io-client-dart/issues/59
diff --git a/example/README.md b/example/README.md
index b79afe5..6d9398b 100644
--- a/example/README.md
+++ b/example/README.md
@@ -39,6 +39,80 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g
         socket.on('fromServer', (_) => print(_));
     }
     
+
+    
+### Connect manually
+To connect the socket manually, set the option `autoConnect: false` and call `.connect()`.
+
+For example,
+
+Socket socket = io('http://localhost:3000', <String, dynamic>{
+    'transports': ['websocket'],
+    'autoConnect': false,
+    'extraHeaders': {'foo': 'bar'} // optional
+  });
+socket.connect();
+
+ +Note that `.connect()` should not be called if `autoConnect: true`, as this will cause all event handlers to get registered/fired twice. See [Issue #33](https://github.com/rikulo/socket.io-client-dart/issues/33). + +### Update the extra headers +``` +Socket socket = ... // Create socket. +socket.io.options['extraHeaders'] = {'foo': 'bar'}; // Update the extra headers. +socket.io..disconnect()..connect(); // Reconnect the socket manually. +``` + +### Emit with acknowledgement +``` +Socket socket = ... // Create socket. +socket.on('connect', (_) { + print('connect'); + socket.emitWithAck('msg', 'init', ack: (data) { + print('ack $data') ; + if (data != null) { + print('from server $data'); + } else { + print("Null") ; + } + }); +}); +``` + +### Socket connection events +These events can be listened on. +``` +const List EVENTS = [ + 'connect', + 'connect_error', + 'connect_timeout', + 'connecting', + 'disconnect', + 'error', + 'reconnect', + 'reconnect_attempt', + 'reconnect_failed', + 'reconnect_error', + 'reconnecting', + 'ping', + 'pong' +]; + +// Replace 'connect' with any of the above events. +socket.on('connect', (_) { + print('connect'); +} +``` + +### Acknowledge with the socket server that an event has been received. +``` +socket.on('eventName', (data) { + final dataList = data as List; + final ack = dataList.last as Function; + ack(null); +}); +``` + ## Usage (Flutter) In Flutter env. it only works with `dart:io` websocket, not with `dart:html` websocket, so in this case you have to add `'transports': ['websocket']` when creates the socket instance. diff --git a/pubspec.yaml b/pubspec.yaml index e1c1ec1..8742854 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: socket_io_client description: Dartlang port of socket.io-client for web, flutter, dartvm to use -version: 0.9.7+2 +version: 0.9.8 author: jumperchen homepage: https://www.zkoss.org repository: https://github.com/rikulo/socket.io-client-dart