Skip to content

Commit

Permalink
Merge pull request #2 from muzzammilshahid/wamp-session-joiner
Browse files Browse the repository at this point in the history
Implement WAMP session joiner
  • Loading branch information
muzzammilshahid authored Apr 23, 2024
2 parents 9de7712 + 9c206d1 commit 38ae763
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 18 deletions.
5 changes: 0 additions & 5 deletions bin/wamp.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/src/wamp_base.dart

This file was deleted.

48 changes: 48 additions & 0 deletions lib/src/wamp_session_joiner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "dart:io";
import "dart:typed_data";

import "package:wampproto/auth.dart";
import "package:wampproto/joiner.dart";
import "package:wampproto/serializers.dart";
import "package:wampproto/session.dart";

String getSubProtocol(Serializer serializer) {
if (serializer is JSONSerializer) {
return WAMPSessionJoiner.jsonSubProtocol;
} else if (serializer is CBORSerializer) {
return WAMPSessionJoiner.cborSubProtocol;
} else if (serializer is MsgPackSerializer) {
return WAMPSessionJoiner.msgpackSubProtocol;
} else {
throw ArgumentError("invalid serializer");
}
}

class WAMPSessionJoiner {
WAMPSessionJoiner(this._authenticator, {Serializer? serializer}) : _serializer = serializer ?? JSONSerializer();

static const String jsonSubProtocol = "wamp.2.json";
static const String cborSubProtocol = "wamp.2.cbor";
static const String msgpackSubProtocol = "wamp.2.msgpack";

final IClientAuthenticator _authenticator;
final Serializer _serializer;
late WebSocket ws;

Future<SessionDetails?> join(String uri, String realm) async {
ws = await WebSocket.connect(uri, protocols: [getSubProtocol(_serializer)]);

final joiner = Joiner(realm, _serializer, _authenticator);
ws.add(joiner.sendHello());

await for (final message in ws) {
final toSend = joiner.receive(Uint8List.fromList((message as String).codeUnits));
if (toSend == null) {
return joiner.getSessionDetails();
}

ws.add(toSend);
}
return null;
}
}
1 change: 0 additions & 1 deletion lib/wamp.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/wamp_session.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export "src/wamp_session_joiner.dart" show WAMPSessionJoiner;
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ name: wamp
description: WAMP v2 Client for Dart.
version: 1.0.0
repository: https://github.com/xconnio/wamp.dart
publish_to: none

environment:
sdk: ^3.3.0

# Add regular dependencies here.
dependencies:
# path: ^1.8.0
wampproto:
git: https://github.com/xconnio/wampproto.dart

dev_dependencies:
lints: ^3.0.0
Expand Down
39 changes: 39 additions & 0 deletions test/wamp_session_joiner_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "dart:typed_data";

import "package:test/test.dart";
import "package:wamp/src/wamp_session_joiner.dart";
import "package:wampproto/messages.dart";
import "package:wampproto/serializers.dart";

class TestSerializer implements Serializer {
@override
Uint8List serialize(final Message message) {
return Uint8List.fromList("".codeUnits);
}

@override
Message deserialize(final Uint8List message) {
return Hello("realm", {}, "", []);
}
}

void main() {
test("jsonSubProtocol", () {
var result = getSubProtocol(JSONSerializer());
expect(result, equals(WAMPSessionJoiner.jsonSubProtocol));
});

test("cborSubProtocol", () {
var result = getSubProtocol(CBORSerializer());
expect(result, equals(WAMPSessionJoiner.cborSubProtocol));
});

test("msgpackSubProtocol", () {
var result = getSubProtocol(MsgPackSerializer());
expect(result, equals(WAMPSessionJoiner.msgpackSubProtocol));
});

test("invalidSerializer", () {
expect(() => getSubProtocol(TestSerializer()), throwsArgumentError);
});
}
8 changes: 0 additions & 8 deletions test/wamp_test.dart

This file was deleted.

0 comments on commit 38ae763

Please sign in to comment.