Skip to content

Commit

Permalink
Update Makefile, centrifugo-config.json, Spinify code, and smoke_test…
Browse files Browse the repository at this point in the history
….dart
  • Loading branch information
PlugFox committed May 5, 2024
1 parent 0348e02 commit 26e6a7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ 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
# http://localhost:8000
# https://centrifugal.dev/docs/server/console_commands
centrifugo:
@docker run -it --rm --ulimit nofile=65536:65536 -p 8000:8000 --name centrifugo -v $(PWD)/centrifugo-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
@docker run -d --rm --ulimit nofile=65536:65536 -p 8000:8000 --name centrifugo -v $(PWD)/centrifugo-config.json:/centrifugo/config.json centrifugo/centrifugo:latest centrifugo --client_insecure --admin --admin_insecure --log_level=debug

centrifugo-down:
@docker stop centrifugo
Expand Down
1 change: 1 addition & 0 deletions centrifugo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions lib/src/model/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import 'package:meta/meta.dart';

import 'stream_position.dart';

/// Command builder.
typedef SpinifyCommandBuilder = SpinifyCommand Function(
int id,
DateTime timestamp,
);

/// {@template command}
/// Command sent from a client to a server.
///
Expand Down
35 changes: 26 additions & 9 deletions lib/src/spinify_impl.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: avoid_types_on_closure_parameters

import 'dart:async';

import 'package:meta/meta.dart';
Expand Down Expand Up @@ -81,7 +83,7 @@ abstract base class SpinifyCallbacks {
/// - [refresh]
/// - [subRefresh]
@mustCallSuper
Future<void> _onCommand(SpinifyCommand command) async {}
Future<void> _onCommand(SpinifyCommandBuilder builder) async {}

/// On reply received.
@mustCallSuper
Expand Down Expand Up @@ -203,22 +205,37 @@ base mixin SpinifyStateMixin on SpinifyBase {

/// Base mixin for Spinify command sending.
base mixin SpinifyCommandMixin on SpinifyBase {
@override
Future<void> send(List<int> data) async {
//await ready();
//await _sendMessageAsync(SpinifyMes);
}

final Map<int, Completer<SpinifyReply>> _replies =
<int, Completer<SpinifyReply>>{};

@override
Future<void> send(List<int> data) => _bucket.push(
ClientEvent.command,
(int id, DateTime timestamp) => SpinifySendRequest(
id: id,
timestamp: timestamp,
data: data,
));

@override
Future<void> _onCommand(SpinifyCommandBuilder builder) async {
await super._onCommand(builder);
final command = builder(_getNextCommandId(), DateTime.now());
switch (command) {
case SpinifySendRequest send:
await _sendCommandAsync(send);
default:
await _sendCommand(command);
}
}

Future<T> _sendCommand<T extends SpinifyReply>(SpinifyCommand command) async {
final completer = _replies[command.id] = Completer<T>();
await _sendMessageAsync(command);
await _sendCommandAsync(command);
return completer.future;
}

Future<void> _sendMessageAsync(SpinifyCommand command) async {
Future<void> _sendCommandAsync(SpinifyCommand command) async {
assert(command.id > 0, 'Command ID is not set');
assert(_transport != null, 'Transport is not connected');
await _transport?.send(command);
Expand Down
3 changes: 3 additions & 0 deletions test/smoke/smoke_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:spinify/spinify.dart';
import 'package:test/test.dart';

Expand All @@ -8,6 +10,7 @@ void main() {
final client = Spinify();
await client.connect(url);
expect(client.state, isA<SpinifyState$Connected>());
await client.send(utf8.encode('Hello, Spinify!'));
await client.disconnect();
expect(client.state, isA<SpinifyState$Disconnected>());
await client.close();
Expand Down

0 comments on commit 26e6a7d

Please sign in to comment.