Skip to content

Commit

Permalink
1. add more example into example/README.md
Browse files Browse the repository at this point in the history
2. add @luandnguyen contributor for #59
  • Loading branch information
jumperchen committed Feb 19, 2020
1 parent 325cb50 commit ca3ccea
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ To connect the socket manually, set the option `autoConnect: false` and call `.c

For example,
<pre>
Socket socket = io('http://localhost:3000', <String, dynamic>{
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
'transports': ['websocket'],
<b>'autoConnect': false</b>,
'extraHeaders': {'foo': 'bar'} // optional
Expand Down Expand Up @@ -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
74 changes: 74 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
<pre>
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
'transports': ['websocket'],
<b>'autoConnect': false</b>,
'extraHeaders': {'foo': 'bar'} // optional
});
<b>socket.connect();</b>
</pre>

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.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://www.zkoss.org
repository: https://github.com/rikulo/socket.io-client-dart
Expand Down

0 comments on commit ca3ccea

Please sign in to comment.