Skip to content

Commit 918aebb

Browse files
committed
Fix sending message
1 parent e52c425 commit 918aebb

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

api/example/host.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ Future<void> main(List<String> args) async {
88
final connection = SwampConnection.build(
99
Uri.parse(args.elementAtOrNull(0) ?? 'ws://localhost:8080/ws'),
1010
);
11+
await Future.delayed(Duration(seconds: 5));
12+
Timer? timer;
13+
1114
connection.onRoomInfo.listen((event) {
1215
print('Room created: ${encodeRoomCode(event.roomId)}');
16+
timer?.cancel();
17+
var i = 0;
18+
timer = Timer.periodic(Duration(seconds: 2), (timer) {
19+
final message = 'Hello, World! ${i++}';
20+
connection.messagePipe.sendMessage(utf8.encode(message));
21+
print('Sent message: $message');
22+
});
1323
});
1424
connection.onCreationFailed.listen((event) {
1525
print('Failed to create room ($event)');
1626
});
1727
connection.onClosed.listen((event) {
1828
print('Connection closed');
29+
timer?.cancel();
30+
timer = null;
1931
});
2032
await connection.init();
2133
print('Connected to ${connection.server}');
22-
await Future.delayed(Duration(seconds: 5));
23-
var i = 0;
24-
Timer.periodic(Duration(seconds: 2), (timer) {
25-
final message = 'Hello, World! ${i++}';
26-
connection.sendNamedFunction(SwampCommand.message, utf8.encode(message));
27-
print('Sent message: $message');
28-
});
2934
}

api/lib/connection.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SwampConnection extends NetworkerPipe<Uint8List, RpcNetworkerPacket>
2424
final Uri server;
2525
final Uint8List? roomId;
2626
final E2EENetworkerPipe? e2eePipe;
27+
final RawNetworkerPipe messagePipe;
2728

2829
WebSocketChannel? _channel;
2930

@@ -56,18 +57,18 @@ class SwampConnection extends NetworkerPipe<Uint8List, RpcNetworkerPacket>
5657

5758
RoomInfo? get roomInfo => _onRoomInfo.valueOrNull;
5859

59-
NetworkerPipe<Uint8List, Uint8List> get messagePipe =>
60-
e2eePipe ?? registerNamedFunction(SwampEvent.message);
61-
6260
SwampConnection({
6361
required this.server,
6462
this.roomId,
6563
this.roomCodeEncoder = encodeRoomCode,
6664
this.e2eePipe,
67-
}) {
65+
}) : messagePipe = InternalChannelPipe(bytes: 2, channel: kAnyChannel) {
6866
if (e2eePipe != null) {
6967
registerNamedFunction(SwampEvent.message).connect(e2eePipe!);
7068
}
69+
(e2eePipe ?? registerNamedFunction(SwampEvent.message)).connect(
70+
messagePipe,
71+
);
7172
_initFunctions();
7273
}
7374

api/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ packages:
309309
dependency: "direct main"
310310
description:
311311
path: "packages/networker/networker"
312-
ref: "08928a4dc5ef638809b18c84f4f9bd64108fe18d"
313-
resolved-ref: "08928a4dc5ef638809b18c84f4f9bd64108fe18d"
312+
ref: "213c6382f196ee93c4c43ca911cdb26a38317e39"
313+
resolved-ref: "213c6382f196ee93c4c43ca911cdb26a38317e39"
314314
url: "https://github.com/LinwoodDev/dart_pkgs.git"
315315
source: git
316316
version: "1.0.0"

api/pubspec.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ environment:
99

1010
# Add regular dependencies here.
1111
dependencies:
12-
networker:
13-
git:
14-
url: https://github.com/LinwoodDev/dart_pkgs.git
15-
path: packages/networker/networker
16-
ref: 08928a4dc5ef638809b18c84f4f9bd64108fe18d
12+
networker: any
1713
networker_crypto:
1814
git:
1915
url: https://github.com/LinwoodDev/dart_pkgs.git
@@ -27,3 +23,9 @@ dev_dependencies:
2723
build_runner: ^2.4.15
2824
lints: ^5.0.0
2925
test: ^1.24.0
26+
dependency_overrides:
27+
networker:
28+
git:
29+
url: https://github.com/LinwoodDev/dart_pkgs.git
30+
path: packages/networker/networker
31+
ref: 213c6382f196ee93c4c43ca911cdb26a38317e39

server/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ packages:
318318
dependency: "direct main"
319319
description:
320320
path: "packages/networker/networker"
321-
ref: "08928a4dc5ef638809b18c84f4f9bd64108fe18d"
322-
resolved-ref: "08928a4dc5ef638809b18c84f4f9bd64108fe18d"
321+
ref: "213c6382f196ee93c4c43ca911cdb26a38317e39"
322+
resolved-ref: "213c6382f196ee93c4c43ca911cdb26a38317e39"
323323
url: "https://github.com/LinwoodDev/dart_pkgs.git"
324324
source: git
325325
version: "1.0.0"

server/pubspec.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ environment:
1010
dependencies:
1111
swamp_api:
1212
path: ../api
13-
networker:
14-
git:
15-
url: https://github.com/LinwoodDev/dart_pkgs.git
16-
path: packages/networker/networker
17-
ref: 08928a4dc5ef638809b18c84f4f9bd64108fe18d
13+
networker: any
1814
networker_crypto:
1915
git:
2016
url: https://github.com/LinwoodDev/dart_pkgs.git
@@ -36,3 +32,9 @@ dev_dependencies:
3632
http: ^1.1.0
3733
lints: ^5.0.0
3834
test: ^1.24.0
35+
dependency_overrides:
36+
networker:
37+
git:
38+
url: https://github.com/LinwoodDev/dart_pkgs.git
39+
path: packages/networker/networker
40+
ref: 213c6382f196ee93c4c43ca911cdb26a38317e39

0 commit comments

Comments
 (0)