-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export "src/realm.dart" show Realm; | ||
export "src/router.dart" show Router; | ||
export "src/types.dart" show IBaseSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import "package:wamp/src/types.dart"; | ||
import "package:wampproto/broker.dart"; | ||
import "package:wampproto/dealer.dart"; | ||
import "package:wampproto/messages.dart"; | ||
import "package:wampproto/types.dart"; | ||
|
||
class Realm { | ||
Dealer dealer = Dealer(); | ||
Broker broker = Broker(); | ||
|
||
Map<int, IBaseSession> clients = {}; | ||
|
||
void attachClient(IBaseSession base) { | ||
clients[base.id()] = base; | ||
dealer.addSession(base.id()); | ||
broker.addSession(base.id()); | ||
} | ||
|
||
void detachClient(IBaseSession base) { | ||
clients.remove(base.id()); | ||
broker.removeSession(base.id()); | ||
dealer.removeSession(base.id()); | ||
} | ||
|
||
void stop() { | ||
// stop will disconnect all clients. | ||
} | ||
|
||
void receiveMessage(int sessionID, Message msg) { | ||
switch (msg.messageType()) { | ||
case Call.id || Yield.id || Register.id || UnRegister.id: | ||
MessageWithRecipient recipient = dealer.receiveMessage(sessionID, msg); | ||
var client = clients[recipient.recipient]; | ||
client?.sendMessage(recipient.message); | ||
|
||
case Publish.id || Subscribe.id || UnSubscribe.id: | ||
List<MessageWithRecipient>? recipients = broker.receiveMessage(sessionID, msg); | ||
if (recipients == null) { | ||
return; | ||
} | ||
|
||
for (final recipient in recipients) { | ||
var client = clients[recipient.recipient]; | ||
client?.sendMessage(msg); | ||
} | ||
case Goodbye.id: | ||
dealer.removeSession(sessionID); | ||
broker.removeSession(sessionID); | ||
var client = clients[sessionID]; | ||
client?.ws.close(); | ||
clients.remove(sessionID); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import "package:wamp/src/realm.dart"; | ||
import "package:wamp/src/types.dart"; | ||
import "package:wampproto/messages.dart"; | ||
|
||
class Router { | ||
Map<String, Realm> realms = {}; | ||
|
||
void addRealm(String name) { | ||
realms[name] = Realm(); | ||
} | ||
|
||
void removeRealm(String name) { | ||
realms.remove(name); | ||
} | ||
|
||
bool hasRealm(String name) { | ||
return realms.containsKey(name); | ||
} | ||
|
||
void attachClient(IBaseSession baseSession) { | ||
String realm = baseSession.realm(); | ||
if (!realms.containsKey(realm)) { | ||
throw Exception("cannot attach client to non-existent realm $realm"); | ||
} | ||
|
||
realms[realm]?.attachClient(baseSession); | ||
} | ||
|
||
void detachClient(IBaseSession baseSession) { | ||
String realm = baseSession.realm(); | ||
if (!realms.containsKey(realm)) { | ||
throw Exception("cannot detach client from non-existent realm $realm"); | ||
} | ||
|
||
realms[realm]?.detachClient(baseSession); | ||
} | ||
|
||
void receiveMessage(IBaseSession baseSession, Message msg) { | ||
String realm = baseSession.realm(); | ||
if (!realms.containsKey(realm)) { | ||
throw Exception("cannot process message for non-existent realm $realm"); | ||
} | ||
|
||
realms[realm]?.receiveMessage(baseSession.id(), msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import "dart:html"; | ||
import "dart:typed_data"; | ||
|
||
import "package:wampproto/messages.dart"; | ||
|
||
abstract class IBaseSession { | ||
late final WebSocket ws; | ||
|
||
int id() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
String realm() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
String authid() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
String authrole() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
void send(Uint8List data) { | ||
throw UnimplementedError(); | ||
} | ||
|
||
Uint8List receive() { | ||
throw UnimplementedError(); | ||
} | ||
|
||
void sendMessage(Message msg) { | ||
throw UnimplementedError(); | ||
} | ||
|
||
Message receiveMessage() { | ||
throw UnimplementedError(); | ||
} | ||
} |