Skip to content

Commit 2d56d8b

Browse files
authored
Merge pull request #12 from muzzammilshahid/add-example
Add example
2 parents be1678f + 90e8826 commit 2d56d8b

File tree

12 files changed

+100
-0
lines changed

12 files changed

+100
-0
lines changed

example/build.gradle.kts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.20"
3+
}
4+
5+
group = "io.xconn"
6+
version = "unspecified"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
implementation(project(":xconn"))
14+
implementation("io.xconn:wampproto:0.1.1")
15+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")
16+
}
17+
18+
tasks.test {
19+
useJUnitPlatform()
20+
}
21+
kotlin {
22+
jvmToolchain(17)
23+
}

example/src/main/kotlin/Main.kt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.xconn
2+
3+
import io.xconn.wampproto.auth.CRAAuthenticator
4+
import io.xconn.wampproto.auth.CryptoSignAuthenticator
5+
import io.xconn.wampproto.auth.TicketAuthenticator
6+
import io.xconn.wampproto.serializers.CBORSerializer
7+
import io.xconn.wampproto.serializers.JSONSerializer
8+
import io.xconn.wampproto.serializers.Serializer
9+
import io.xconn.xconn.Client
10+
import io.xconn.xconn.Result
11+
import io.xconn.xconn.Session
12+
import kotlinx.coroutines.runBlocking
13+
14+
const val URL = "ws://localhost:8080/ws"
15+
const val REALM = "realm1"
16+
17+
fun main() =
18+
runBlocking {
19+
val callee = connectAnonymous()
20+
// register a procedure
21+
val reg =
22+
callee.register("test", { invocation ->
23+
Result(args = invocation.args, kwargs = invocation.kwargs)
24+
}).await()
25+
26+
val caller = connectTicket("ticket-user", "ticket-pass", JSONSerializer())
27+
// call a procedure
28+
val result = caller.call("test", args = listOf("abc", 123)).await()
29+
println(result)
30+
31+
// unregister a procedure
32+
callee.unregister(reg).await()
33+
34+
val subscriber = connectCRA("wamp-cra-user", "cra-secret", CBORSerializer())
35+
// subscribe to a topic
36+
val subscription =
37+
subscriber.subscribe("test", { event ->
38+
println(event)
39+
}).await()
40+
41+
val publisher =
42+
connectCryptosign(
43+
"cryptosign-user",
44+
"150085398329d255ad69e82bf47ced397bcec5b8fbeecd28a80edbbd85b49081",
45+
CBORSerializer(),
46+
)
47+
// publish to a topic
48+
publisher.publish("test", args = listOf("abc", 123), kwargs = mapOf("key" to "value"))?.await()
49+
50+
// unsubscribe to topic
51+
subscriber.unsubscribe(subscription).await()
52+
}
53+
54+
suspend fun connectAnonymous(): Session {
55+
val client = Client()
56+
57+
return client.connect(URL, REALM)
58+
}
59+
60+
suspend fun connectTicket(authID: String, ticket: String, serializer: Serializer): Session {
61+
val client = Client(TicketAuthenticator(authID, emptyMap(), ticket), serializer)
62+
63+
return client.connect(URL, REALM)
64+
}
65+
66+
suspend fun connectCRA(authID: String, secret: String, serializer: Serializer): Session {
67+
val client = Client(CRAAuthenticator(authID, emptyMap(), secret), serializer)
68+
69+
return client.connect(URL, REALM)
70+
}
71+
72+
suspend fun connectCryptosign(authID: String, privateKey: String, serializer: Serializer): Session {
73+
val client = Client(CryptoSignAuthenticator(authID, privateKey, mutableMapOf()), serializer)
74+
75+
return client.connect(URL, REALM)
76+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ plugins {
22
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
33
}
44
rootProject.name = "xconn"
5+
include(":xconn", ":example")
File renamed without changes.

0 commit comments

Comments
 (0)