Skip to content

Commit

Permalink
Update Makefile and Spinify code
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed May 5, 2024
1 parent 3392e87 commit 0348e02
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ publish: generate

deploy: publish

centrifugo-it:
@docker run -it --rm --ulimit nofile=65536:65536 -p 8000:8000 --name centrifugo -v $(PWD)/config.json:/centrifugo/config.json centrifugo/centrifugo:latest centrifugo --client_insecure --admin --admin_insecure --log_level=debug -c config.json

centrifugo-up:
@docker run -d --rm --ulimit nofile=65536:65536 -p 8000:8000 --name centrifugo centrifugo/centrifugo:latest centrifugo --client_insecure --admin --admin_insecure --log_level=debug

Expand Down
30 changes: 15 additions & 15 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ base mixin SpinifyCommandMixin on SpinifyBase {
_replies.remove(reply.id)?.complete(reply);
await super._onReply(reply);
}

@override
Future<void> _onDisconnect(({int? code, String? reason}) arg) async {
for (final completer in _replies.values) {
completer.completeError(StateError('Client is disconnected'));
}
await super._onDisconnect(arg);
}
}

/// Base mixin for Spinify client connection management (connect & disconnect).
Expand All @@ -244,9 +252,7 @@ base mixin SpinifyConnectionMixin
await super._onConnect(url);
try {
// Disconnect previous transport if exists.
_transport
?.disconnect(SpinifyConnectingCode.connectCalled, 'Reconnecting')
.ignore();
_transport?.disconnect(1000, 'Reconnecting').ignore();

// Create new transport.
_transport = await _createTransport(url, config.headers)
Expand Down Expand Up @@ -274,15 +280,10 @@ base mixin SpinifyConnectionMixin
_readyCompleter?.complete();
_readyCompleter = null;
} on Object catch (error, stackTrace) {
_transport
?.disconnect(
SpinifyConnectingCode.transportClosed, 'Failed to connect')
.ignore();
_transport = null;
_setState(SpinifyState$Disconnected(
closeCode: SpinifyConnectingCode.connectCalled,
closeReason: 'Failed to connect',
timestamp: DateTime.now()));
await _onDisconnect((
code: SpinifyConnectingCode.transportClosed,
reason: 'Failed to connect'
)).catchError((_) {});
_readyCompleter?.completeError(error, stackTrace);
rethrow;
}
Expand Down Expand Up @@ -317,15 +318,14 @@ base mixin SpinifyConnectionMixin

@override
Future<void> _onDisconnect(({int? code, String? reason}) arg) async {
await _transport?.disconnect(arg.code, arg.reason);
await _transport?.disconnect(1000, arg.reason);
_transport = null;
await super._onDisconnect(arg);
}

@override
Future<void> _onClose() async {
await _transport?.disconnect(
SpinifyDisconnectedCode.disconnectCalled, 'Client closing');
await _transport?.disconnect(1000, 'Client closing');
_transport = null;
await super._onClose();
}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/transport_ws_pb_vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Future<ISpinifyTransport> $create$WS$PB$Transport(
Map<String, String> headers,
) async {
// ignore: close_sinks
final socket = await io.WebSocket.connect(url, headers: headers);
final socket = await io.WebSocket.connect(
url,
headers: headers,
protocols: <String>{'centrifuge-protobuf'},
);
final transport = SpinifyTransport$WS$PB$VM(socket);
// 0 CONNECTING Socket has been created. The connection is not yet open.
// 1 OPEN The connection is open and ready to communicate.
Expand Down Expand Up @@ -66,6 +70,6 @@ final class SpinifyTransport$WS$PB$VM implements ISpinifyTransport {
Future<void> disconnect([int? code, String? reason]) async {
await _subscription.cancel();
await _socket.close(code, reason);
assert(_socket.readyState == io.WebSocket.closed, 'Socket is not closed');
//assert(_socket.readyState == io.WebSocket.closed, 'Socket is not closed');
}
}
17 changes: 17 additions & 0 deletions test/smoke/smoke_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:spinify/spinify.dart';
import 'package:test/test.dart';

void main() {
group('Smoke test', () {
const url = 'ws://localhost:8000/connection/websocket';
test('Connection', () async {
final client = Spinify();
await client.connect(url);
expect(client.state, isA<SpinifyState$Connected>());
await client.disconnect();
expect(client.state, isA<SpinifyState$Disconnected>());
await client.close();
expect(client.state, isA<SpinifyState$Closed>());
});
});
}
9 changes: 0 additions & 9 deletions test/unit/spinify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,5 @@ void main() {
isA<SpinifyState$Closed>()
]));
});

/* const url = 'ws://localhost:8000/connection/websocket';
test('Connection', () async {
final client = Spinify();
await client.connect(url);
expect(client.state, isA<SpinifyState$Connected>());
await client.disconnect();
expect(client.state, isA<SpinifyState$Disconnected>());
}); */
});
}

0 comments on commit 0348e02

Please sign in to comment.