Skip to content

Commit 7e728f5

Browse files
committed
Resolve #46 Make this library more "Darty"
1 parent b281763 commit 7e728f5

File tree

6 files changed

+236
-45
lines changed

6 files changed

+236
-45
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.9.12
2+
3+
**New Feature:**
4+
5+
* [#46](https://github.com/rikulo/socket.io-client-dart/issues/46) Make this library more "Darty"
6+
17
## 0.9.11
28

39
**New Feature:**

README.md

+27-22
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ main() {
3030
3131
// Dart client
3232
IO.Socket socket = IO.io('http://localhost:3000');
33-
socket.on('connect', (_) {
33+
socket.onConnect((_) {
3434
print('connect');
3535
socket.emit('msg', 'test');
3636
});
3737
socket.on('event', (data) => print(data));
38-
socket.on('disconnect', (_) => print('disconnect'));
38+
socket.onDisconnect((_) => print('disconnect'));
3939
socket.on('fromServer', (_) => print(_));
4040
}
4141
```
@@ -47,15 +47,18 @@ To connect the socket manually, set the option `autoConnect: false` and call `.c
4747
For example,
4848

4949
<pre>
50-
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
51-
'transports': ['websocket'],
52-
<b>'autoConnect': false</b>,
53-
'extraHeaders': {'foo': 'bar'} // optional
50+
Socket socket = io('http://localhost:3000',
51+
OptionBuilder()
52+
.setTransports(['websocket']) // for Flutter or Dart VM
53+
.<b>disableAutoConnect()</b> // disable auto-connection
54+
.setExtraHeaders({'foo': 'bar'}) // optional
55+
.build())
5456
});
5557
<b>socket.connect();</b>
5658
</pre>
5759

58-
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).
60+
Note that `.connect()` should not be called if `autoConnect: true`
61+
(by default, it's enabled to 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).
5962

6063
### Update the extra headers
6164

@@ -69,7 +72,7 @@ socket.io..disconnect()..connect(); // Reconnect the socket manually.
6972

7073
```dart
7174
Socket socket = ... // Create socket.
72-
socket.on('connect', (_) {
75+
socket.onConnect((_) {
7376
print('connect');
7477
socket.emitWithAck('msg', 'init', ack: (data) {
7578
print('ack $data') ;
@@ -103,10 +106,10 @@ const List EVENTS = [
103106
'pong'
104107
];
105108
106-
// Replace 'connect' with any of the above events.
107-
socket.on('connect', (_) {
109+
// Replace 'onConnect' with any of the above events.
110+
socket.onConnect((_) {
108111
print('connect');
109-
}
112+
});
110113
```
111114

112115
### Acknowledge with the socket server that an event has been received.
@@ -121,16 +124,18 @@ socket.on('eventName', (data) {
121124

122125
## Usage (Flutter)
123126

124-
In Flutter env. it only works with `dart:io` websocket, not with `dart:html` websocket, so in this case
125-
you have to add `'transports': ['websocket']` when creates the socket instance.
127+
In Flutter env. not (Flutter Web env.) it only works with `dart:io` websocket,
128+
not with `dart:html` websocket or Ajax (XHR), so in this case
129+
you have to add `setTransports(['websocket'])` when creates the socket instance.
126130

127131
For example,
128132

129133
```dart
130-
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
131-
'transports': ['websocket'],
132-
'extraHeaders': {'foo': 'bar'} // optional
133-
});
134+
IO.Socket socket = IO.io('http://localhost:3000',
135+
OptionBuilder()
136+
.setTransports(['websocket']) // for Flutter or Dart VM
137+
.setExtraHeaders({'foo': 'bar'}) // optional
138+
.build());
134139
```
135140

136141
## Usage with stream and streambuilder in Flutter
@@ -156,18 +161,18 @@ StreamSocket streamSocket =StreamSocket();
156161
157162
//STEP2: Add this function in main function in main.dart file and add incoming data to the stream
158163
void connectAndListen(){
159-
IO.Socket socket = IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
160-
'transports': ['websocket'],
161-
});
164+
IO.Socket socket = IO.io('http://localhost:3000',
165+
OptionBuilder()
166+
.setTransports(['websocket']).build());
162167
163-
socket.on('connect', (_) {
168+
socket.onConnect((_) {
164169
print('connect');
165170
socket.emit('msg', 'test');
166171
});
167172
168173
//When an event recieved from server, data is added to the stream
169174
socket.on('event', (data) => streamSocket.addResponse);
170-
socket.on('disconnect', (_) => print('disconnect'));
175+
socket.onDisconnect((_) => print('disconnect'));
171176
172177
}
173178

example/README.md

+38-22
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ main() {
3030
3131
// Dart client
3232
IO.Socket socket = IO.io('http://localhost:3000');
33-
socket.on('connect', (_) {
33+
socket.onConnect((_) {
3434
print('connect');
3535
socket.emit('msg', 'test');
3636
});
3737
socket.on('event', (data) => print(data));
38-
socket.on('disconnect', (_) => print('disconnect'));
38+
socket.onDisconnect((_) => print('disconnect'));
3939
socket.on('fromServer', (_) => print(_));
4040
}
4141
```
@@ -47,15 +47,18 @@ To connect the socket manually, set the option `autoConnect: false` and call `.c
4747
For example,
4848

4949
<pre>
50-
Socket socket = io('http://localhost:3000', &lt;String, dynamic>{
51-
'transports': ['websocket'],
52-
<b>'autoConnect': false</b>,
53-
'extraHeaders': {'foo': 'bar'} // optional
50+
Socket socket = io('http://localhost:3000',
51+
OptionBuilder()
52+
.setTransports(['websocket']) // for Flutter or Dart VM
53+
.<b>disableAutoConnect()</b> // disable auto-connection
54+
.setExtraHeaders({'foo': 'bar'}) // optional
55+
.build())
5456
});
5557
<b>socket.connect();</b>
5658
</pre>
5759

58-
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).
60+
Note that `.connect()` should not be called if `autoConnect: true`
61+
(by default, it's enabled to 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).
5962

6063
### Update the extra headers
6164

@@ -69,7 +72,7 @@ socket.io..disconnect()..connect(); // Reconnect the socket manually.
6972

7073
```dart
7174
Socket socket = ... // Create socket.
72-
socket.on('connect', (_) {
75+
socket.onConnect((_) {
7376
print('connect');
7477
socket.emitWithAck('msg', 'init', ack: (data) {
7578
print('ack $data') ;
@@ -103,10 +106,10 @@ const List EVENTS = [
103106
'pong'
104107
];
105108
106-
// Replace 'connect' with any of the above events.
107-
socket.on('connect', (_) {
109+
// Replace 'onConnect' with any of the above events.
110+
socket.onConnect((_) {
108111
print('connect');
109-
}
112+
});
110113
```
111114

112115
### Acknowledge with the socket server that an event has been received.
@@ -121,16 +124,18 @@ socket.on('eventName', (data) {
121124

122125
## Usage (Flutter)
123126

124-
In Flutter env. it only works with `dart:io` websocket, not with `dart:html` websocket, so in this case
125-
you have to add `'transports': ['websocket']` when creates the socket instance.
127+
In Flutter env. not (Flutter Web env.) it only works with `dart:io` websocket,
128+
not with `dart:html` websocket or Ajax (XHR), so in this case
129+
you have to add `setTransports(['websocket'])` when creates the socket instance.
126130

127131
For example,
128132

129133
```dart
130-
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
131-
'transports': ['websocket'],
132-
'extraHeaders': {'foo': 'bar'} // optional
133-
});
134+
IO.Socket socket = IO.io('http://localhost:3000',
135+
OptionBuilder()
136+
.setTransports(['websocket']) // for Flutter or Dart VM
137+
.setExtraHeaders({'foo': 'bar'}) // optional
138+
.build());
134139
```
135140

136141
## Usage with stream and streambuilder in Flutter
@@ -156,18 +161,18 @@ StreamSocket streamSocket =StreamSocket();
156161
157162
//STEP2: Add this function in main function in main.dart file and add incoming data to the stream
158163
void connectAndListen(){
159-
IO.Socket socket = IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
160-
'transports': ['websocket'],
161-
});
164+
IO.Socket socket = IO.io('http://localhost:3000',
165+
OptionBuilder()
166+
.setTransports(['websocket']).build());
162167
163-
socket.on('connect', (_) {
168+
socket.onConnect((_) {
164169
print('connect');
165170
socket.emit('msg', 'test');
166171
});
167172
168173
//When an event recieved from server, data is added to the stream
169174
socket.on('event', (data) => streamSocket.addResponse);
170-
socket.on('disconnect', (_) => print('disconnect'));
175+
socket.onDisconnect((_) => print('disconnect'));
171176
172177
}
173178
@@ -221,6 +226,17 @@ void main() {
221226
- Refer to https://github.com/rikulo/socket.io-client-dart/issues/108 issue.
222227
Please use `socket.dispose()` instead of `socket.close()` or `socket.disconnect()` to solve the memory leak issue on iOS.
223228

229+
### Connect_error on MacOS with SocketException: Connection failed
230+
* Refer to https://github.com/flutter/flutter/issues/47606#issuecomment-568522318 issue.
231+
232+
By adding the following key into the to file `*.entitlements` under directory `macos/Runner/`
233+
```
234+
<key>com.apple.security.network.client</key>
235+
<true/>
236+
```
237+
238+
For more details, please take a look at https://flutter.dev/desktop#setting-up-entitlements
239+
224240
## Notes to Contributors
225241

226242
### Fork socket.io-client-dart

lib/socket_io_client.dart

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:socket_io_client/src/engine/parseqs.dart';
2020
import 'package:socket_io_client/src/manager.dart';
2121

2222
export 'package:socket_io_client/src/socket.dart';
23+
export 'package:socket_io_client/src/darty.dart';
2324

2425
// Protocol version
2526
final protocol = parser.protocol;

0 commit comments

Comments
 (0)