Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actualize sample for 0.15.4 #223

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions samples/chat/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)))
}
Loading