Skip to content

Commit

Permalink
Merge branch 'felangel-dart2-upgrade'
Browse files Browse the repository at this point in the history
fix #7
  • Loading branch information
jumperchen committed Nov 20, 2018
2 parents 53b0774 + e3bf03d commit 873175f
Show file tree
Hide file tree
Showing 23 changed files with 442 additions and 434 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Files and directories created by pub
.dart_tool
.packages
.pub/
build/
Expand Down
2 changes: 1 addition & 1 deletion lib/socket_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export 'src/engine/transport/jsonp_transport.dart' show JSONPTransport;
export 'src/engine/transport/polling_transport.dart' show PollingTransport;
export 'src/engine/transport/websocket_transport.dart' show WebSocketTransport;

export 'package:socket_io/src/engine/parser/parser.dart' show PacketParser;
export 'package:socket_io/src/engine/parser/parser.dart' show PacketParser;
27 changes: 13 additions & 14 deletions lib/src/adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ abstract class Adapter {
clientRooms(String id, [fn(err, [_])]);

static Adapter newInstance(String key, Namespace nsp) {
if ('default' == key ) {
if ('default' == key) {
return new _MemoryStoreAdapter(nsp);
}
throw new UnimplementedError('not supported other adapter yet.');
}
}

class _MemoryStoreAdapter extends EventEmitter implements Adapter {
Map nsps = {};
Map<String, _Room> rooms;
Expand All @@ -59,7 +60,7 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
add(String id, String room, [fn([_])]) {
this.sids[id] = this.sids[id] ?? {};
this.sids[id][room] = true;
this.rooms[room] = this.rooms[room] ?? new _Room();
this.rooms[room] = this.rooms[room] ?? new _Room();
this.rooms[room].add(id);
if (fn != null) scheduleMicrotask(() => fn(null));
}
Expand All @@ -77,8 +78,7 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
this.sids[id].remove(room);
if (this.rooms.containsKey(room)) {
this.rooms[room].del(id);
if (this.rooms[room].length == 0)
this.rooms.remove(room);
if (this.rooms[room].length == 0) this.rooms.remove(room);
}

if (fn != null) scheduleMicrotask(() => fn(null));
Expand All @@ -97,14 +97,13 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
for (var room in rooms.keys) {
if (this.rooms.containsKey(room)) {
this.rooms[room].del(id);
if (this.rooms[room].length == 0)
this.rooms.remove(room);
if (this.rooms[room].length == 0) this.rooms.remove(room);
}
}
}
this.sids.remove(id);

if (fn != null) scheduleMicrotask(() => fn(null));
if (fn != null) scheduleMicrotask(() => fn(null));
}

/**
Expand Down Expand Up @@ -151,9 +150,9 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
}
} else {
for (var id in this.sids.keys) {
if (except.indexOf(id) >= 0) continue;
socket = this.nsp.connected[id];
if (socket != null) socket.packet(encodedPackets, packetOpts);
if (except.indexOf(id) >= 0) continue;
socket = this.nsp.connected[id];
if (socket != null) socket.packet(encodedPackets, packetOpts);
}
}
});
Expand Down Expand Up @@ -191,8 +190,8 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
}
} else {
for (var id in this.sids.keys) {
socket = this.nsp.connected[id];
if (socket != null) sids.add(id);
socket = this.nsp.connected[id];
if (socket != null) sids.add(id);
}
}

Expand All @@ -211,7 +210,7 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
if (fn != null) scheduleMicrotask(() => fn(null, rooms?.keys));
}
}
/**
/**
* Room constructor.
*
* @api private
Expand Down Expand Up @@ -250,4 +249,4 @@ class _Room {
this.length--;
}
}
}
}
20 changes: 10 additions & 10 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ class Client {
connect(name, [query]) {
_logger.fine('connecting to namespace $name');
if (!this.server.nsps.containsKey(name)) {
this.packet(
{'type': ERROR, 'nsp': name, 'data': 'Invalid namespace'});
this.packet(<dynamic, dynamic>{'type': ERROR, 'nsp': name, 'data': 'Invalid namespace'});
return;
}
var nsp = this.server.of(name);
Expand All @@ -92,7 +91,6 @@ class Client {
* @api private
*/
disconnect() {
var socket;
// we don't use a for loop because the length of
// `sockets` changes upon each iteration
this.sockets.toList().forEach((socket) {
Expand Down Expand Up @@ -146,18 +144,20 @@ class Client {
writeToEngine(encodedPackets) {
if (opts['volatile'] != null && !self.conn.transport.writable) return;
for (var i = 0; i < encodedPackets.length; i++) {
self.conn.write(encodedPackets[i], { 'compress': opts['compress']});
self.conn.write(encodedPackets[i], {'compress': opts['compress']});
}
}

if ('open' == this.conn.readyState) {
_logger.fine('writing packet $packet');
if (opts['preEncoded'] != true) { // not broadcasting, need to encode
this.encoder.encode(
packet, (encodedPackets) { // encode, then write results to engine
if (opts['preEncoded'] != true) {
// not broadcasting, need to encode
this.encoder.encode(packet, (encodedPackets) {
// encode, then write results to engine
writeToEngine(encodedPackets);
});
} else { // a broadcast pre-encodes a packet
} else {
// a broadcast pre-encodes a packet
writeToEngine(packet);
}
} else {
Expand All @@ -174,8 +174,8 @@ class Client {
// try/catch is needed for protocol violations (GH-1880)
try {
this.decoder.add(data);
} catch (e) {
_logger.severe(e, (e as Error).stackTrace);
} catch (e, st) {
_logger.severe(e, st);
this.onerror(e);
}
}
Expand Down
21 changes: 10 additions & 11 deletions lib/src/engine/connect.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* connect.dart
*
Expand All @@ -21,7 +20,8 @@ class SocketConnect extends HttpConnectWrapper {
bool _completed;
SocketConnect(HttpConnect origin) : super(origin);

SocketConnect.fromWebSocket(HttpConnect origin, WebSocket socket) : super(origin) {
SocketConnect.fromWebSocket(HttpConnect origin, WebSocket socket)
: super(origin) {
_socket = socket;
}

Expand All @@ -45,13 +45,12 @@ class SocketConnect extends HttpConnectWrapper {
* Closes the current connection.
*/
void close() {
if (_done != null) {
_done.complete('done');
} else if (_socket != null) {
_socket.close();
} else {
_completed = true;
}
if (_done != null) {
_done.complete('done');
} else if (_socket != null) {
_socket.close();
} else {
_completed = true;
}
}

}
}
3 changes: 2 additions & 1 deletion lib/src/engine/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Engine extends EventEmitter {
engine.attachTo(server, options);
return engine;
}

dynamic operator [](Object key) {}

/**
Expand All @@ -35,4 +36,4 @@ class Engine extends EventEmitter {
close() {}
// handleRequest() {}
// handshake() {}
}
}
Loading

0 comments on commit 873175f

Please sign in to comment.