Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
Rebase on upstream
Browse files Browse the repository at this point in the history
Rebase
  • Loading branch information
Curvel authored Mar 18, 2020
2 parents f9ca673 + 6482bd6 commit 81951e8
Show file tree
Hide file tree
Showing 27 changed files with 2,070 additions and 1,522 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ doc/api/
*.iml
*.ipr
*.iws

*/node_modules
42 changes: 42 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## 0.9.9

**Bug fix:**

* [#67](https://github.com/rikulo/socket.io-client-dart/issues/67) Retry connection backoff after 54 tries reconnections every 0 second

## 0.9.8

**Bug fix:**

* [#33](https://github.com/rikulo/socket.io-client-dart/issues/33) socket.on('receiveMessage',(data)=>print("data")) called twice


## 0.9.7+2

**New Feature:**

* [#48](https://github.com/rikulo/socket.io-client-dart/issues/48) add links to github repo in pubspec.yaml


## 0.9.7+1

**New Feature:**

* [#38](https://github.com/rikulo/socket.io-client-dart/issues/38) Improve pub.dev score


## 0.9.6+3

**Bug fix:**

* [#42](https://github.com/rikulo/socket.io-client-dart/issues/42) Error when using emitWithAck

## 0.9.5

**New Feature:**

* [#34](https://github.com/rikulo/socket.io-client-dart/issues/34) Add support for extraHeaders

**Bug fix:**

* [#39](https://github.com/rikulo/socket.io-client-dart/issues/39) The factor of Backoff with 54 retries causes an overflow
91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g

## Usage


import 'package:socket_io/socket_io.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

Expand Down Expand Up @@ -38,6 +37,90 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g
socket.on('disconnect', (_) => print('disconnect'));
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.

For example,
```
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'extraHeaders': {'foo': 'bar'} // optional
});
```

## Notes to Contributors

Expand All @@ -58,4 +141,8 @@ If you are new to Git or GitHub, please read [this guide](https://help.github.co


## Contributors
* Thanks [@felangel](https://github.com/felangel) for https://github.com/rikulo/socket.io-client-dart/issues/7
* Thanks [@felangel](https://github.com/felangel) for https://github.com/rikulo/socket.io-client-dart/issues/7
* 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
3 changes: 2 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include: package:pedantic/analysis_options.yaml

analyzer:
strong-mode: true
# exclude:
# - path/to/excluded/files/**

Expand Down
126 changes: 126 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# socket.io-client-dart example

Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://github.com/socketio/socket.io-client) - in Dart

## Usage


import 'package:socket_io/socket_io.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

main() {
// Dart server
var io = new Server();
var nsp = io.of('/some');
nsp.on('connection', (Socket client) {
print('connection /some');
client.on('msg', (data) {
print('data from /some => $data');
client.emit('fromServer', "ok 2");
});
});
io.on('connection', (Socket client) {
print('connection default namespace');
client.on('msg', (data) {
print('data from default => $data');
client.emit('fromServer', "ok");
});
});
io.listen(3000);

// Dart client
IO.Socket socket = IO.io('http://localhost:3000');
socket.on('connect', (_) {
print('connect');
socket.emit('msg', 'test');
});
socket.on('event', (data) => print(data));
socket.on('disconnect', (_) => print('disconnect'));
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.

For example,
```
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'extraHeaders': {'foo': 'bar'} // optional
});
```
65 changes: 33 additions & 32 deletions lib/socket_io_client.dart
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
/**
* socket_io_client.dart
*
* Purpose:
*
* Description:
*
* History:
* 26/04/2017, Created by jumperchen
*
* Copyright (C) 2017 Potix Corporation. All Rights Reserved.
*/
///
/// socket_io_client.dart
///
/// Purpose:
///
/// Description:
///
/// History:
/// 26/04/2017, Created by jumperchen
///
/// Copyright (C) 2017 Potix Corporation. All Rights Reserved.
///
library socket_io_client;

import 'package:logging/logging.dart';
import 'package:socket_io_common/src/engine/parser/parser.dart' as Parser;
import 'package:socket_io_client/src/socket.dart';
import 'package:socket_io_common/src/engine/parser/parser.dart' as parser;
import 'package:socket_io_client/src/engine/parseqs.dart';
import 'package:socket_io_client/src/manager.dart';

export 'package:socket_io_client/src/socket.dart';

// Protocol version
final protocol = Parser.protocol;
final protocol = parser.protocol;

final Map<String, dynamic> cache = {};

final Logger _logger = new Logger('socket_io_client');
final Logger _logger = Logger('socket_io_client');

/**
* Looks up an existing `Manager` for multiplexing.
* If the user summons:
*
* `io('http://localhost/a');`
* `io('http://localhost/b');`
*
* We reuse the existing instance based on same scheme/port/host,
* and we initialize sockets for each namespace.
*
* @api public
*/
io(uri, [opts]) => _lookup(uri, opts);
///
/// Looks up an existing `Manager` for multiplexing.
/// If the user summons:
///
/// `io('http://localhost/a');`
/// `io('http://localhost/b');`
///
/// We reuse the existing instance based on same scheme/port/host,
/// and we initialize sockets for each namespace.
///
/// @api public
///
Socket io(uri, [opts]) => _lookup(uri, opts);

_lookup(uri, opts) {
Socket _lookup(uri, opts) {
opts = opts ?? <dynamic, dynamic>{};

Uri parsed = Uri.parse(uri);
var parsed = Uri.parse(uri);
var id = '${parsed.scheme}://${parsed.host}:${parsed.port}';
var path = parsed.path;
var sameNamespace = cache.containsKey(id) && cache[id].nsps.containsKey(path);
Expand All @@ -57,9 +58,9 @@ _lookup(uri, opts) {

if (newConnection) {
_logger.fine('ignoring socket cache for $uri');
io = new Manager(uri: uri, options: opts);
io = Manager(uri: uri, options: opts);
} else {
io = cache[id] ??= new Manager(uri: uri, options: opts);
io = cache[id] ??= Manager(uri: uri, options: opts);
}
if (parsed.query.isNotEmpty && opts['query'] == null) {
opts['query'] = parsed.query;
Expand Down
Loading

0 comments on commit 81951e8

Please sign in to comment.