Skip to content

Commit

Permalink
Merge pull request #20 from muzzammilshahid/local-example
Browse files Browse the repository at this point in the history
Add example for local server and client
  • Loading branch information
om26er authored May 15, 2024
2 parents 2857299 + a9f0691 commit da06464
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions example/in_memory_router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import "package:wampproto/serializers.dart";

import "package:xconn/exports.dart";
import "package:xconn/src/types.dart";

const procedureName = "io.xconn.example_procedure";
const topicName = "io.xconn.example_topic";

void main() async {
var router = Router()..addRealm("realm1");

var serializer = JSONSerializer();
var clientSideBase = ClientSideLocalBaseSession(1, "realm1", "local", "local", serializer, router);
var serverSideBase = ServerSideLocalBaseSession(1, "realm1", "local", "local", serializer, other: clientSideBase);

router.attachClient(serverSideBase);

var session = Session(clientSideBase);

// register a procedure
var registration = await session.register(procedureName, (inv) {
return Result(args: inv.args, kwargs: inv.kwargs, details: inv.details);
});

// call a procedure
var result = await session.call(procedureName, args: ["abc"]);
print("Result: args=${result.args}, kwargs=${result.kwargs}, details=${result.details}");

// unregister a procedure
await session.unregister(registration);

// subscribe to a topic
var subscription = await session.subscribe(topicName, (event) {
print("Event: args=${event.args}, kwargs=${event.kwargs}, details=${event.details}");
});

// publish to a topic
await session.publish(topicName, args: ["abc"], kwargs: {"one": 1}, options: {"acknowledge": true});

// unsubscribe from a topic
await session.unsubscribe(subscription);
}
4 changes: 2 additions & 2 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Session {
return completer.future;
}

Future<Registration> register(String procedure, Result Function(Invocation) endpoint) {
Future<Registration> register(String procedure, Result Function(Invocation invocation) endpoint) {
var register = msg.Register(_nextID, procedure);

var completer = Completer<Registration>();
Expand Down Expand Up @@ -174,7 +174,7 @@ class Session {
return null;
}

Future<Subscription> subscribe(String topic, void Function(Event) endpoint) {
Future<Subscription> subscribe(String topic, void Function(Event event) endpoint) {
var subscribe = msg.Subscribe(_nextID, topic);

var completer = Completer<Subscription>();
Expand Down

0 comments on commit da06464

Please sign in to comment.