From 86f56934c8c6b3411a5fbffb2ff3cdacefc51fa3 Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Mon, 29 Apr 2024 12:46:34 +0500 Subject: [PATCH] Implement Register --- lib/src/session.dart | 27 +++++++++++++++++++++++++++ lib/src/types.dart | 14 ++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/src/session.dart b/lib/src/session.dart index 325d968..32288f1 100644 --- a/lib/src/session.dart +++ b/lib/src/session.dart @@ -27,12 +27,28 @@ class Session { Map> callRequests = {}; + final Map registerRequests = {}; + final Map registrations = {}; + void processIncomingMessage(Message msg) { if (msg is Result) { var request = callRequests.remove(msg.requestID); if (request != null) { request.complete(msg); } + } else if (msg is Registered) { + var request = registerRequests.remove(msg.requestID); + if (request != null) { + registrations[msg.registrationID] = request.endpoint; + request.future.complete(Registration(msg.registrationID)); + } + } else if (msg is Invocation) { + var endpoint = registrations[msg.registrationID]; + if (endpoint != null) { + Yield yield = endpoint(msg); + Uint8List data = wampSession.sendMessage(yield); + baseSession.send(data); + } } } @@ -51,4 +67,15 @@ class Session { return completer.future; } + + Future register(String procedure, Yield Function(Invocation) endpoint) { + var register = Register(nextID, procedure); + + var completer = Completer(); + registerRequests[register.requestID] = RegisterRequest(completer, endpoint); + + baseSession.send(wampSession.sendMessage(register)); + + return completer.future; + } } diff --git a/lib/src/types.dart b/lib/src/types.dart index 6dff09b..391ec0c 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -1,6 +1,7 @@ import "dart:async"; import "dart:io"; +import "package:wampproto/messages.dart"; import "package:wampproto/serializers.dart"; import "package:wampproto/session.dart"; @@ -24,3 +25,16 @@ class BaseSession { await _ws.close(); } } + +class Registration { + Registration(this.registrationID); + + final int registrationID; +} + +class RegisterRequest { + RegisterRequest(this.future, this.endpoint); + + final Completer future; + final Yield Function(Invocation) endpoint; +}