Skip to content

Commit

Permalink
actualize sample for 0.15.4 (#223)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
olme04 committed Apr 14, 2022
1 parent e3efddb commit a09a2b5
Show file tree
Hide file tree
Showing 48 changed files with 1,038 additions and 151 deletions.
24 changes: 14 additions & 10 deletions samples/chat/README.md
@@ -1,13 +1,17 @@
# chat

* api - shared chat API for both client and server
* client - client API implementation as requests to RSocket with Protobuf serialization. Works on JVM(TCP/WS), JS(WS),
Native(TCP). Tasks for running sample clients:
* JVM: `run`
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
* NodeJs: `jsNodeRun`
* Browser: `jsBrowserRun`
* server - server API implementation with storage in ordinary concurrent map and exposing it through RSocket with
Protobuf serialization. Can be started on JVM(TCP+WS) and Native(TCP). Tasks for running sample servers:
* JVM: `run`
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
* client - client API implementation via requesting to RSocket with Protobuf serialization.
Works on JVM(TCP/WS), Native(TCP/WS), NodeJS(WS/TCP), Browser(WS).
Tasks for running clients:
* JVM: `run`
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
* NodeJs: `nodejsNodeRun` / `nodejsNodeDevelopmentRun` / `nodejsNodeProductionRun`
* Browser: `browserBrowserRun` / `browserBrowserDevelopmentRun` / `browserBrowserProductionRun`
* server - server API implementation with storage in concurrent map
and exposing it through RSocket with Protobuf serialization.
Can be started on JVM(TCP/WS), Native(TCP/WS), NodeJS(TCP).
Tasks for running servers:
* JVM: `run`
* Native: `runDebugExecutableNative` / `runReleaseExecutableNative`
* NodeJs: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`
18 changes: 17 additions & 1 deletion samples/chat/api/build.gradle.kts
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.jetbrains.kotlin.konan.target.*

plugins {
Expand All @@ -10,7 +26,7 @@ val kotlinxSerializationVersion: String by rootProject

kotlin {
jvm()
js(IR) {
js {
browser()
nodejs()
}
Expand Down
16 changes: 16 additions & 0 deletions samples/chat/api/src/commonMain/kotlin/ChatApi.kt
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.api

import kotlinx.serialization.*
Expand Down
16 changes: 16 additions & 0 deletions samples/chat/api/src/commonMain/kotlin/MessageApi.kt
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.api

import kotlinx.coroutines.flow.*
Expand Down
16 changes: 16 additions & 0 deletions samples/chat/api/src/commonMain/kotlin/Serialization.kt
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.api

import io.ktor.utils.io.core.*
Expand Down
49 changes: 49 additions & 0 deletions samples/chat/api/src/commonMain/kotlin/Servers.kt
@@ -0,0 +1,49 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.api

enum class TransportType { TCP, WS }

data class ServerAddress(val port: Int, val type: TransportType)

object Servers {
object JVM {
val TCP = ServerAddress(port = 8001, type = TransportType.TCP)
val WS = ServerAddress(port = 8002, type = TransportType.WS)
}

object JS {
val TCP = ServerAddress(port = 7001, type = TransportType.TCP)
}

object Native {
val TCP = ServerAddress(port = 9001, type = TransportType.TCP)
val WS = ServerAddress(port = 9002, type = TransportType.WS)
}

val WS = setOf(
JVM.WS,
Native.WS
)
val TCP = setOf(
JVM.TCP,
JS.TCP,
Native.TCP
)

val ALL = WS + TCP
}
16 changes: 16 additions & 0 deletions samples/chat/api/src/commonMain/kotlin/UserApi.kt
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.api

import kotlinx.serialization.*
Expand Down
40 changes: 35 additions & 5 deletions samples/chat/client/build.gradle.kts
@@ -1,3 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.jetbrains.kotlin.konan.target.*

plugins {
Expand All @@ -17,10 +33,12 @@ kotlin {
jvm {
withJava()
}
js(IR) {
js("browser") {
browser {
binaries.executable()
}
}
js("nodejs") {
nodejs {
binaries.executable()
}
Expand All @@ -33,25 +51,37 @@ kotlin {
}?.binaries {
executable {
entryPoint = "io.rsocket.kotlin.samples.chat.client.main"
freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis" //TODO
}
}

sourceSets {
commonMain {
dependencies {
implementation(project(":api"))
implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion")
implementation("io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:$rsocketVersion")
}
}
val jvmMain by getting {
dependencies {
implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
}
val jsMain by getting {
findByName("nativeMain")?.apply {
dependencies {
implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
}
val browserMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
}
}
val nodejsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-js:$ktorVersion")
implementation("io.rsocket.kotlin:rsocket-transport-nodejs-tcp:$rsocketVersion")
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions samples/chat/client/src/browserMain/kotlin/App.kt
@@ -0,0 +1,33 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.client

import io.rsocket.kotlin.samples.chat.api.*
import kotlinx.coroutines.*

suspend fun main() {
coroutineScope {
//only WS is supported on browser JS
// native WS server is incompatible with js WS client
(Servers.WS - Servers.Native.WS).forEach {
val client = ApiClient(it, "Yuri")
launch {
client.use(it, "RSocket is awesome! (from browser)")
}
}
}
}
31 changes: 31 additions & 0 deletions samples/chat/client/src/browserMain/kotlin/clientTransport.kt
@@ -0,0 +1,31 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.client

import io.ktor.client.engine.js.*
import io.rsocket.kotlin.samples.chat.api.*
import io.rsocket.kotlin.transport.*
import io.rsocket.kotlin.transport.ktor.websocket.client.*

internal actual fun clientTransport(
type: TransportType,
host: String,
port: Int
): ClientTransport = when (type) {
TransportType.TCP -> error("TCP is not supported")
TransportType.WS -> WebSocketClientTransport(Js, host, port)
}
@@ -1,5 +1,5 @@
<!--
~ Copyright 2015-2020 the original author or authors.
~ Copyright 2015-2022 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chat</title>
<meta charset="UTF-8">
<title>Chat</title>
</head>
<body>

</body>
<script src="chat-client.js"></script>
<script src="client.js"></script>
</html>
31 changes: 31 additions & 0 deletions samples/chat/client/src/commonMain/kotlin/ApiClient.kt
@@ -1,6 +1,24 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.client

import io.rsocket.kotlin.*
import io.rsocket.kotlin.core.*
import io.rsocket.kotlin.payload.*
import io.rsocket.kotlin.samples.chat.api.*
import kotlinx.serialization.*

Expand All @@ -11,3 +29,16 @@ class ApiClient(rSocket: RSocket) {
val chats = ChatApiClient(rSocket, proto)
val messages = MessageApiClient(rSocket, proto)
}

suspend fun ApiClient(
address: ServerAddress,
name: String
): ApiClient {
println("Connecting client to: $address")
val connector = RSocketConnector {
connectionConfig {
setupPayload { buildPayload { data(name) } }
}
}
return ApiClient(connector.connect(ClientTransport(address)))
}

0 comments on commit a09a2b5

Please sign in to comment.