Skip to content

Commit

Permalink
Revert, fetch JWT and payload data only after connection
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jul 27, 2023
1 parent afbffb9 commit cd69c87
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 49 deletions.
4 changes: 1 addition & 3 deletions lib/src/client/centrifuge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ base mixin CentrifugeConnectionMixin on CentrifugeBase, CentrifugeErrorsMixin {
Future<void> connect(String url) async {
logger.fine('Interactively connecting to $url');
try {
final token = await _config.getToken?.call();
final payload = await _config.getPayload?.call();
await _transport.connect(url: url, token: token, payload: payload);
await _transport.connect(url);
} on CentrifugeException catch (error, stackTrace) {
_emitError(error, stackTrace);
rethrow;
Expand Down
6 changes: 1 addition & 5 deletions lib/src/transport/transport_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ abstract interface class ICentrifugeTransport {
/// Connect to the server.
/// [url] is a URL of endpoint.
/// {@nodoc}
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
});
Future<void> connect(String url);

/// Send asynchronous message to a server. This method makes sense
/// only when using Centrifuge library for Go on a server side. In Centrifuge
Expand Down
52 changes: 11 additions & 41 deletions lib/src/transport/ws_protobuf_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ abstract base class CentrifugeWSPBTransportBase

@override
@mustCallSuper
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
}) async {}
Future<void> connect(String url) async {}

@override
@mustCallSuper
Expand Down Expand Up @@ -309,21 +305,15 @@ base mixin CentrifugeWSPBConnectionMixin
CentrifugeWSPBSenderMixin,
CentrifugeWSPBStateHandlerMixin {
@override
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
}) async {
Future<void> connect(String url) async {
try {
await super.connect(
url: url,
token: token,
payload: payload,
);
await super.connect(url);
await _webSocket.connect(url);
final request = pb.ConnectRequest();
final token = await _config.getToken?.call();
assert(token == null || token.length > 5, 'Centrifuge JWT is too short');
if (token != null) request.token = token;
final payload = await _config.getPayload?.call();
if (payload != null) request.data = payload;
request
..name = _config.client.name
Expand Down Expand Up @@ -443,23 +433,15 @@ base mixin CentrifugeWSPBStateHandlerMixin
}

@override
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
}) {
Future<void> connect(String url) {
// Change state to connecting before connection.
_setState(CentrifugeState$Connecting(url: url));
// Subscribe to websocket state after initialization.
_webSocketClosedStateSubscription ??= _webSocket.stateChanges.closed.listen(
_handleWebSocketClosedStates,
cancelOnError: false,
);
return super.connect(
url: url,
token: token,
payload: payload,
);
return super.connect(url);
}

@override
Expand Down Expand Up @@ -488,21 +470,13 @@ base mixin CentrifugeWSPBHandlerMixin
StreamSubscription<List<int>>? _webSocketMessageSubscription;

@override
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
}) {
Future<void> connect(String url) {
// Subscribe to websocket messages after first connection.
_webSocketMessageSubscription ??= _webSocket.stream.bytes.listen(
_handleWebSocketMessage,
cancelOnError: false,
);
return super.connect(
url: url,
token: token,
payload: payload,
);
return super.connect(url);
}

/// {@nodoc}
Expand Down Expand Up @@ -590,13 +564,9 @@ base mixin CentrifugeWSPBPingPongMixin on CentrifugeWSPBTransportBase {
Timer? _pingTimer;

@override
Future<void> connect({
required String url,
required String? token,
required List<int>? payload,
}) async {
Future<void> connect(String url) async {
_tearDownPingTimer();
await super.connect(url: url, token: token, payload: payload);
await super.connect(url);
_restartPingTimer();
}

Expand Down

0 comments on commit cd69c87

Please sign in to comment.