diff --git a/samples/chat/README.md b/samples/chat/README.md index f83991ef5..6d9b3fc39 100644 --- a/samples/chat/README.md +++ b/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` diff --git a/samples/chat/api/build.gradle.kts b/samples/chat/api/build.gradle.kts index 22eb57613..e0df9283c 100644 --- a/samples/chat/api/build.gradle.kts +++ b/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 { @@ -10,7 +26,7 @@ val kotlinxSerializationVersion: String by rootProject kotlin { jvm() - js(IR) { + js { browser() nodejs() } diff --git a/samples/chat/api/src/commonMain/kotlin/ChatApi.kt b/samples/chat/api/src/commonMain/kotlin/ChatApi.kt index 2ed70347a..f7d700b14 100644 --- a/samples/chat/api/src/commonMain/kotlin/ChatApi.kt +++ b/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.* diff --git a/samples/chat/api/src/commonMain/kotlin/MessageApi.kt b/samples/chat/api/src/commonMain/kotlin/MessageApi.kt index c69709709..c30797b2f 100644 --- a/samples/chat/api/src/commonMain/kotlin/MessageApi.kt +++ b/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.* diff --git a/samples/chat/api/src/commonMain/kotlin/Serialization.kt b/samples/chat/api/src/commonMain/kotlin/Serialization.kt index 8515da20d..f590e9302 100644 --- a/samples/chat/api/src/commonMain/kotlin/Serialization.kt +++ b/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.* diff --git a/samples/chat/api/src/commonMain/kotlin/Servers.kt b/samples/chat/api/src/commonMain/kotlin/Servers.kt new file mode 100644 index 000000000..efb6f09b2 --- /dev/null +++ b/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 +} diff --git a/samples/chat/api/src/commonMain/kotlin/UserApi.kt b/samples/chat/api/src/commonMain/kotlin/UserApi.kt index 05ba6c64f..3d92a1a33 100644 --- a/samples/chat/api/src/commonMain/kotlin/UserApi.kt +++ b/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.* diff --git a/samples/chat/client/build.gradle.kts b/samples/chat/client/build.gradle.kts index b02aaa553..0aef81492 100644 --- a/samples/chat/client/build.gradle.kts +++ b/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 { @@ -17,10 +33,12 @@ kotlin { jvm { withJava() } - js(IR) { + js("browser") { browser { binaries.executable() } + } + js("nodejs") { nodejs { binaries.executable() } @@ -33,7 +51,6 @@ kotlin { }?.binaries { executable { entryPoint = "io.rsocket.kotlin.samples.chat.client.main" - freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis" //TODO } } @@ -41,17 +58,30 @@ kotlin { 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") } } } diff --git a/samples/chat/client/src/browserMain/kotlin/App.kt b/samples/chat/client/src/browserMain/kotlin/App.kt new file mode 100644 index 000000000..cd54f5fed --- /dev/null +++ b/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)") + } + } + } +} diff --git a/samples/chat/client/src/browserMain/kotlin/clientTransport.kt b/samples/chat/client/src/browserMain/kotlin/clientTransport.kt new file mode 100644 index 000000000..e18a78b7c --- /dev/null +++ b/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) +} diff --git a/samples/chat/client/src/jsMain/resources/index.html b/samples/chat/client/src/browserMain/resources/index.html similarity index 82% rename from samples/chat/client/src/jsMain/resources/index.html rename to samples/chat/client/src/browserMain/resources/index.html index 6b9fe7dff..f98970589 100644 --- a/samples/chat/client/src/jsMain/resources/index.html +++ b/samples/chat/client/src/browserMain/resources/index.html @@ -1,5 +1,5 @@