-
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.
Merge pull request #2 from muzzammilshahid/wamp-session-joiner
Implement WAMP session joiner
- Loading branch information
Showing
8 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export "src/wamp_session_joiner.dart" show WAMPSessionJoiner; |
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
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,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); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.