Skip to content

Commit

Permalink
Merge pull request #7 from muzzammilshahid/wamp-actions
Browse files Browse the repository at this point in the history
Add functions for basic WAMP actions
  • Loading branch information
muzzammilshahid authored May 22, 2024
2 parents 8d32d51 + 5b25ee7 commit 9060e69
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ Color closeIconColor = Colors.redAccent;
const double iconSize = 15;
const double largeIconSize = 20;
const double horizontalPadding = 10;

/// Serializers ///
const jsonSerializer = "JSON";
const cborSerializer = "CBOR";
const msgPackSerializer = "MsgPack";
6 changes: 3 additions & 3 deletions lib/screens/mobile/mobile_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ class _MobileHomeScaffoldState extends State<MobileHomeScaffold> with TickerProv
value: _tabData[index].selectedSerializer.isEmpty ? null : _tabData[index].selectedSerializer,
hint: const Text("Serializers"),
items: <String>[
"JSON",
"CBOR",
"MsgPack",
jsonSerializer,
cborSerializer,
msgPackSerializer,
].map((String value) {
return DropdownMenuItem<String>(
value: value,
Expand Down
63 changes: 63 additions & 0 deletions lib/wamp_actions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import "package:wampproto/auth.dart";
import "package:wampproto/serializers.dart";
import "package:xconn/exports.dart";

import "package:xconn_ui/constants.dart";

Serializer _getSerializer(String? serializerString) {
switch (serializerString) {
case jsonSerializer:
return JSONSerializer();
case cborSerializer:
return CBORSerializer();
case msgPackSerializer:
return MsgPackSerializer();

default:
throw Exception("invalid serializer $serializerString");
}
}

Future<Session> connect(
String url,
String realm,
String serializerStr, {
String? authid,
String? authrole,
String? ticket,
String? secret,
String? privateKey,
}) async {
var serializer = _getSerializer(serializerStr);
Client client;

if (ticket != null) {
client = Client(serializer: serializer, authenticator: TicketAuthenticator(ticket, authid ?? ""));
} else if (secret != null) {
client = Client(serializer: serializer, authenticator: WAMPCRAAuthenticator(secret, authid ?? "", {}));
} else if (privateKey != null) {
client = Client(serializer: serializer, authenticator: CryptoSignAuthenticator(authid ?? "", privateKey));
} else {
client = Client(serializer: serializer);
}

return client.connect(url, realm);
}

Future<Registration> register(Session session, String procedure) {
return session.register(procedure, (Invocation inv) {
return Result(args: inv.args, kwargs: inv.kwargs);
});
}

Future<Result> call(Session session, String procedure, {List? args, Map<String, dynamic>? kwargs}) {
return session.call(procedure, args: args, kwargs: kwargs);
}

Future<Subscription> subscribe(Session session, String topic) {
return session.subscribe(topic, (Event event) {});
}

Future<void>? publish(Session session, String topic, {List? args, Map<String, dynamic>? kwargs}) {
return session.publish(topic, args: args, kwargs: kwargs);
}

0 comments on commit 9060e69

Please sign in to comment.