Skip to content

Commit ca3ccea

Browse files
committed
1. add more example into example/README.md
2. add @luandnguyen contributor for #59
1 parent 325cb50 commit ca3ccea

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ To connect the socket manually, set the option `autoConnect: false` and call `.c
4343

4444
For example,
4545
<pre>
46-
Socket socket = io('http://localhost:3000', <String, dynamic>{
46+
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
4747
'transports': ['websocket'],
4848
<b>'autoConnect': false</b>,
4949
'extraHeaders': {'foo': 'bar'} // optional
@@ -145,3 +145,4 @@ If you are new to Git or GitHub, please read [this guide](https://help.github.co
145145
* Thanks [@Oskang09](https://github.com/Oskang09) for https://github.com/rikulo/socket.io-client-dart/issues/21
146146
* Thanks [@bruce3x](https://github.com/bruce3x) for https://github.com/rikulo/socket.io-client-dart/issues/25
147147
* Thanks [@Kavantix](https://github.com/Kavantix) for https://github.com/rikulo/socket.io-client-dart/issues/26
148+
* Thanks [@luandnguyen](https://github.com/luandnguyen) for https://github.com/rikulo/socket.io-client-dart/issues/59

example/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,80 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g
3939
socket.on('fromServer', (_) => print(_));
4040
}
4141

42+
43+
44+
### Connect manually
45+
To connect the socket manually, set the option `autoConnect: false` and call `.connect()`.
46+
47+
For example,
48+
<pre>
49+
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
50+
'transports': ['websocket'],
51+
<b>'autoConnect': false</b>,
52+
'extraHeaders': {'foo': 'bar'} // optional
53+
});
54+
<b>socket.connect();</b>
55+
</pre>
56+
57+
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).
58+
59+
### Update the extra headers
60+
```
61+
Socket socket = ... // Create socket.
62+
socket.io.options['extraHeaders'] = {'foo': 'bar'}; // Update the extra headers.
63+
socket.io..disconnect()..connect(); // Reconnect the socket manually.
64+
```
65+
66+
### Emit with acknowledgement
67+
```
68+
Socket socket = ... // Create socket.
69+
socket.on('connect', (_) {
70+
print('connect');
71+
socket.emitWithAck('msg', 'init', ack: (data) {
72+
print('ack $data') ;
73+
if (data != null) {
74+
print('from server $data');
75+
} else {
76+
print("Null") ;
77+
}
78+
});
79+
});
80+
```
81+
82+
### Socket connection events
83+
These events can be listened on.
84+
```
85+
const List EVENTS = [
86+
'connect',
87+
'connect_error',
88+
'connect_timeout',
89+
'connecting',
90+
'disconnect',
91+
'error',
92+
'reconnect',
93+
'reconnect_attempt',
94+
'reconnect_failed',
95+
'reconnect_error',
96+
'reconnecting',
97+
'ping',
98+
'pong'
99+
];
100+
101+
// Replace 'connect' with any of the above events.
102+
socket.on('connect', (_) {
103+
print('connect');
104+
}
105+
```
106+
107+
### Acknowledge with the socket server that an event has been received.
108+
```
109+
socket.on('eventName', (data) {
110+
final dataList = data as List;
111+
final ack = dataList.last as Function;
112+
ack(null);
113+
});
114+
```
115+
42116
## Usage (Flutter)
43117
In Flutter env. it only works with `dart:io` websocket, not with `dart:html` websocket, so in this case
44118
you have to add `'transports': ['websocket']` when creates the socket instance.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: socket_io_client
22
description: Dartlang port of socket.io-client for web, flutter, dartvm to use
3-
version: 0.9.7+2
3+
version: 0.9.8
44
author: jumperchen <[email protected]>
55
homepage: https://www.zkoss.org
66
repository: https://github.com/rikulo/socket.io-client-dart

0 commit comments

Comments
 (0)