Skip to content

Commit a09a2b5

Browse files
author
olme04
authored
actualize sample for 0.15.4 (#223)
* add support for running server on nodejs TCP * commonize server starting and client creating * create common Servers object with all possible variations of started servers
1 parent e3efddb commit a09a2b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1038
-151
lines changed

samples/chat/README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# chat
22

33
* api - shared chat API for both client and server
4-
* client - client API implementation as requests to RSocket with Protobuf serialization. Works on JVM(TCP/WS), JS(WS),
5-
Native(TCP). Tasks for running sample clients:
6-
* JVM: `run`
7-
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
8-
* NodeJs: `jsNodeRun`
9-
* Browser: `jsBrowserRun`
10-
* server - server API implementation with storage in ordinary concurrent map and exposing it through RSocket with
11-
Protobuf serialization. Can be started on JVM(TCP+WS) and Native(TCP). Tasks for running sample servers:
12-
* JVM: `run`
13-
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
4+
* client - client API implementation via requesting to RSocket with Protobuf serialization.
5+
Works on JVM(TCP/WS), Native(TCP/WS), NodeJS(WS/TCP), Browser(WS).
6+
Tasks for running clients:
7+
* JVM: `run`
8+
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
9+
* NodeJs: `nodejsNodeRun` / `nodejsNodeDevelopmentRun` / `nodejsNodeProductionRun`
10+
* Browser: `browserBrowserRun` / `browserBrowserDevelopmentRun` / `browserBrowserProductionRun`
11+
* server - server API implementation with storage in concurrent map
12+
and exposing it through RSocket with Protobuf serialization.
13+
Can be started on JVM(TCP/WS), Native(TCP/WS), NodeJS(TCP).
14+
Tasks for running servers:
15+
* JVM: `run`
16+
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
17+
* NodeJs: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`

samples/chat/api/build.gradle.kts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
import org.jetbrains.kotlin.konan.target.*
218

319
plugins {
@@ -10,7 +26,7 @@ val kotlinxSerializationVersion: String by rootProject
1026

1127
kotlin {
1228
jvm()
13-
js(IR) {
29+
js {
1430
browser()
1531
nodejs()
1632
}

samples/chat/api/src/commonMain/kotlin/ChatApi.kt

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.rsocket.kotlin.samples.chat.api
218

319
import kotlinx.serialization.*

samples/chat/api/src/commonMain/kotlin/MessageApi.kt

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.rsocket.kotlin.samples.chat.api
218

319
import kotlinx.coroutines.flow.*

samples/chat/api/src/commonMain/kotlin/Serialization.kt

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.rsocket.kotlin.samples.chat.api
218

319
import io.ktor.utils.io.core.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.samples.chat.api
18+
19+
enum class TransportType { TCP, WS }
20+
21+
data class ServerAddress(val port: Int, val type: TransportType)
22+
23+
object Servers {
24+
object JVM {
25+
val TCP = ServerAddress(port = 8001, type = TransportType.TCP)
26+
val WS = ServerAddress(port = 8002, type = TransportType.WS)
27+
}
28+
29+
object JS {
30+
val TCP = ServerAddress(port = 7001, type = TransportType.TCP)
31+
}
32+
33+
object Native {
34+
val TCP = ServerAddress(port = 9001, type = TransportType.TCP)
35+
val WS = ServerAddress(port = 9002, type = TransportType.WS)
36+
}
37+
38+
val WS = setOf(
39+
JVM.WS,
40+
Native.WS
41+
)
42+
val TCP = setOf(
43+
JVM.TCP,
44+
JS.TCP,
45+
Native.TCP
46+
)
47+
48+
val ALL = WS + TCP
49+
}

samples/chat/api/src/commonMain/kotlin/UserApi.kt

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.rsocket.kotlin.samples.chat.api
218

319
import kotlinx.serialization.*

samples/chat/client/build.gradle.kts

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
import org.jetbrains.kotlin.konan.target.*
218

319
plugins {
@@ -17,10 +33,12 @@ kotlin {
1733
jvm {
1834
withJava()
1935
}
20-
js(IR) {
36+
js("browser") {
2137
browser {
2238
binaries.executable()
2339
}
40+
}
41+
js("nodejs") {
2442
nodejs {
2543
binaries.executable()
2644
}
@@ -33,25 +51,37 @@ kotlin {
3351
}?.binaries {
3452
executable {
3553
entryPoint = "io.rsocket.kotlin.samples.chat.client.main"
36-
freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis" //TODO
3754
}
3855
}
3956

4057
sourceSets {
4158
commonMain {
4259
dependencies {
4360
implementation(project(":api"))
44-
implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion")
61+
implementation("io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:$rsocketVersion")
4562
}
4663
}
4764
val jvmMain by getting {
4865
dependencies {
66+
implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
4967
implementation("io.ktor:ktor-client-cio:$ktorVersion")
5068
}
5169
}
52-
val jsMain by getting {
70+
findByName("nativeMain")?.apply {
71+
dependencies {
72+
implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
73+
implementation("io.ktor:ktor-client-cio:$ktorVersion")
74+
}
75+
}
76+
val browserMain by getting {
77+
dependencies {
78+
implementation("io.ktor:ktor-client-js:$ktorVersion")
79+
}
80+
}
81+
val nodejsMain by getting {
5382
dependencies {
54-
implementation("io.ktor:ktor-client-core:$ktorVersion")
83+
implementation("io.ktor:ktor-client-js:$ktorVersion")
84+
implementation("io.rsocket.kotlin:rsocket-transport-nodejs-tcp:$rsocketVersion")
5585
}
5686
}
5787
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.samples.chat.client
18+
19+
import io.rsocket.kotlin.samples.chat.api.*
20+
import kotlinx.coroutines.*
21+
22+
suspend fun main() {
23+
coroutineScope {
24+
//only WS is supported on browser JS
25+
// native WS server is incompatible with js WS client
26+
(Servers.WS - Servers.Native.WS).forEach {
27+
val client = ApiClient(it, "Yuri")
28+
launch {
29+
client.use(it, "RSocket is awesome! (from browser)")
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.samples.chat.client
18+
19+
import io.ktor.client.engine.js.*
20+
import io.rsocket.kotlin.samples.chat.api.*
21+
import io.rsocket.kotlin.transport.*
22+
import io.rsocket.kotlin.transport.ktor.websocket.client.*
23+
24+
internal actual fun clientTransport(
25+
type: TransportType,
26+
host: String,
27+
port: Int
28+
): ClientTransport = when (type) {
29+
TransportType.TCP -> error("TCP is not supported")
30+
TransportType.WS -> WebSocketClientTransport(Js, host, port)
31+
}

samples/chat/client/src/jsMain/resources/index.html samples/chat/client/src/browserMain/resources/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
~ Copyright 2015-2020 the original author or authors.
2+
~ Copyright 2015-2022 the original author or authors.
33
~
44
~ Licensed under the Apache License, Version 2.0 (the "License");
55
~ you may not use this file except in compliance with the License.
@@ -17,11 +17,11 @@
1717
<!DOCTYPE html>
1818
<html lang="en">
1919
<head>
20-
<meta charset="UTF-8">
21-
<title>Chat</title>
20+
<meta charset="UTF-8">
21+
<title>Chat</title>
2222
</head>
2323
<body>
2424

2525
</body>
26-
<script src="chat-client.js"></script>
26+
<script src="client.js"></script>
2727
</html>

samples/chat/client/src/commonMain/kotlin/ApiClient.kt

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.rsocket.kotlin.samples.chat.client
218

319
import io.rsocket.kotlin.*
20+
import io.rsocket.kotlin.core.*
21+
import io.rsocket.kotlin.payload.*
422
import io.rsocket.kotlin.samples.chat.api.*
523
import kotlinx.serialization.*
624

@@ -11,3 +29,16 @@ class ApiClient(rSocket: RSocket) {
1129
val chats = ChatApiClient(rSocket, proto)
1230
val messages = MessageApiClient(rSocket, proto)
1331
}
32+
33+
suspend fun ApiClient(
34+
address: ServerAddress,
35+
name: String
36+
): ApiClient {
37+
println("Connecting client to: $address")
38+
val connector = RSocketConnector {
39+
connectionConfig {
40+
setupPayload { buildPayload { data(name) } }
41+
}
42+
}
43+
return ApiClient(connector.connect(ClientTransport(address)))
44+
}

0 commit comments

Comments
 (0)