Skip to content

Commit 576241c

Browse files
authored
add wasm module (#9)
* add wasm target * add wasm target * update xlog to 1.3.1 * revert logging changes * replace html title from `kmp-xlog` to `kmp-socketio` * remove moduleName from wasmJs configuration
1 parent 73d9b55 commit 576241c

9 files changed

Lines changed: 86 additions & 3 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ Use Chrome CORS Unblock extension to workaround with CORS error.
103103
./gradlew :example:shared:jsBrowserRun
104104
```
105105

106+
### WASM JS
107+
108+
```bash
109+
./gradlew :example:shared:wasmJsBrowserRun
110+
```
111+
106112
### Windows
107113

108114
```bash

example/shared/build.gradle.kts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
2+
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
3+
14
plugins {
25
alias(libs.plugins.kmp)
36

@@ -37,6 +40,25 @@ kotlin {
3740
}
3841
binaries.executable()
3942
}
43+
@OptIn(ExperimentalWasmDsl::class)
44+
wasmJs {
45+
moduleName = "example"
46+
browser {
47+
val rootDirPath = project.rootDir.path
48+
val projectDirPath = project.projectDir.path
49+
commonWebpackConfig {
50+
outputFileName = "composeApp.js"
51+
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
52+
static = (static ?: mutableListOf()).apply {
53+
// Serve sources to debug inside browser
54+
add(rootDirPath)
55+
add(projectDirPath)
56+
}
57+
}
58+
}
59+
}
60+
binaries.executable()
61+
}
4062

4163
listOf(/*linuxX64(), */mingwX64()).forEach {
4264
it.binaries {
@@ -54,6 +76,11 @@ kotlin {
5476
implementation(project(":kmp-socketio"))
5577
}
5678
}
79+
wasmJsMain {
80+
dependencies {
81+
implementation(libs.kotlinx.browser)
82+
}
83+
}
5784
}
5885
}
5986

example/shared/src/commonMain/kotlin/com/piasy/kmp/socketio/example/Greeting.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
1010
class Greeting {
1111
@OptIn(UnsafeByteStringApi::class)
1212
fun greet() {
13-
IO.socket("http://172.16.11.25:3000", IO.Options()) { socket ->
13+
IO.socket(
14+
"https://echo.websocket.org/",
15+
IO.Options().apply {
16+
transports = listOf("websocket")
17+
}
18+
) { socket ->
1419
socket.on(Socket.EVENT_CONNECT) { args ->
1520
println("on connect ${args.joinToString()}")
1621

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.piasy.kmp.socketio.example
2+
3+
import kotlinx.browser.document
4+
5+
fun main() {
6+
document.getElementById("text")?.innerHTML = "hello world"
7+
Greeting().greet()
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>kmp-socketio</title>
7+
<script type="application/javascript" src="composeApp.js"></script>
8+
</head>
9+
<body>
10+
</body>
11+
</html>

gradle/libs.versions.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ mockk = "1.13.16"
1010
ktor = "3.0.3"
1111
coroutine = "1.10.1"
1212
compose = "1.7.6"
13+
kotlinxBrowser = "0.3"
14+
kmpXlog = "1.3.1"
1315

1416
[libraries]
1517
junit = "junit:junit:4.13.2"
@@ -20,17 +22,19 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t
2022
json = "org.json:json:20250107"
2123
socketioParser = "org.hildan.socketio:socketio-kotlin:2.0.0"
2224
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutine" }
25+
kotlinx-browser = { module = "org.jetbrains.kotlinx:kotlinx-browser", version.ref = "kotlinxBrowser" }
2326
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
2427
ktor-client-websockets = { module = "io.ktor:ktor-client-websockets", version.ref = "ktor" }
2528
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
2629
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
2730
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
2831
ktor-client-winhttp = { module = "io.ktor:ktor-client-winhttp", version.ref = "ktor" }
32+
ktor-client-wasm = { module = "io.ktor:ktor-client-js-wasm-js", version.ref = "ktor" }
2933
ktor-client-curl = { module = "io.ktor:ktor-client-curl", version.ref = "ktor" }
3034
ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }
3135
ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktor" }
3236
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
33-
kmpXlog = "com.piasy:kmp-xlog:1.3.0"
37+
kmpXlog = { module = "com.piasy:kmp-xlog", version.ref = "kmpXlog" }
3438

3539
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
3640
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }

kmp-socketio/build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.vanniktech.maven.publish.SonatypeHost
2+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
23

34
plugins {
45
alias(libs.plugins.kmp)
@@ -25,6 +26,12 @@ kotlin {
2526
binaries.executable()
2627
}
2728
mingwX64 {}
29+
@OptIn(ExperimentalWasmDsl::class)
30+
wasmJs {
31+
browser {
32+
}
33+
binaries.executable()
34+
}
2835

2936
// Ktor's curl engine doesn't support websockets now,
3037
// although CIO engine supports websockets, but it doesn't support TLS.
@@ -85,6 +92,11 @@ kotlin {
8592
api(libs.ktor.client.winhttp)
8693
}
8794
}
95+
wasmJsMain {
96+
dependencies {
97+
api(libs.ktor.client.wasm)
98+
}
99+
}
88100
}
89101
}
90102

kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/WebSocket.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
1515
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
1616
import org.hildan.socketio.EngineIO
1717
import org.hildan.socketio.EngineIOPacket
18+
import org.hildan.socketio.InvalidEngineIOPacketException
1819
import org.hildan.socketio.InvalidSocketIOPacketException
1920
import org.hildan.socketio.SocketIOPacket
2021

@@ -108,7 +109,7 @@ open class WebSocket(
108109
} else {
109110
EngineIO.decodeSocketIO(data)
110111
}
111-
} catch (e: InvalidSocketIOPacketException) {
112+
} catch (e: InvalidEngineIOPacketException) {
112113
val log = "onWsText decode error: ${e.message}"
113114
logE(log)
114115
onError(log)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.piasy.kmp.socketio.engineio.transports
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.HttpClientConfig
5+
import io.ktor.client.engine.js.Js
6+
7+
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient = HttpClient(Js) {
8+
config(this)
9+
}

0 commit comments

Comments
 (0)