8000 GitHub - HackWebRTC/kmp-socketio: KMP (pure Kotlin) implementation of SocketIO client.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

KMP (pure Kotlin) implementation of SocketIO client.

License

Notifications You must be signed in to change notification settings

HackWebRTC/kmp-socketio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

20 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

kmp-socketio

KMP (pure Kotlin) implementation of SocketIO client.

Maven Central Version Main branch status Coverage

Supported platforms

Platform ๐Ÿ› Builds๐Ÿ›  + ๐Ÿ”ฌTests๐Ÿ”ฌ
JVM 17 ๐Ÿš€
JS (Chrome) ๐Ÿš€
Android ๐Ÿš€
iOS ๐Ÿš€
macOS ๐Ÿš€
Windows X64 ๐Ÿš€
Linux X64 ๐Ÿš€

Dependency

You only need to add gradle dependency:

// add common source set dependency
kotlin {
  sourceSets {
    val commonMain by getting {
      dependencies {
        implementation("com.piasy:kmp-socketio:$version")
      }
    }
  }
}

Usage

IO.socket("http://localhost:3000", IO.Options()) { socket ->
    socket.on(Socket.EVENT_CONNECT) { args ->
        println("on connect ${args.joinToString()}")

        val bin = UnsafeByteStringOperations.wrapUnsafe(byteArrayOf(0x1, 0x3, 0x1, 0x4))
        socket.emit("echo", 1, "2", bin, GMTDate())
    }.on("echoBack") { args ->
        println("on echoBack ${args.joinToString()}")
    }

    socket.open()
}

Most of the APIs are the same as socket.io-client-java, here are some differences:

  • Create socket is asynchronous, to make it's easier to guarantee thread safety.
  • Binary messages can't be nested, because emit only accepts String/Boolean/Number/JsonElement/ByteString, other types will be converted to String using toString(), so there is no way to put ByteString in JsonElement.

Logging with kmp-xlog

Development

To check coverage details, run ./gradlew :kmp-socketio:jvmTest --info && ./gradlew koverHtmlReport, then check kmp-socketio/build/reports/kover/html/index.html.

Example

Before running examples, run node kmp-socketio/src/jvmTest/resources/socket-server.js to start the socket-io echo server, and update the local IP address in example/shared/src/commonMain/kotlin/com/piasy/kmp/socketio/example/Greeting.kt.

Android

Open the project (the repo root dir) in Android studio, and run the example.androidApp target.

iOS

brew install cocoapods xcodegen
# if you have installed them earlier, you need to remove them at first,
# or run brew link --overwrite xcodegen cocoapods

cd example/iosApp
xcodegen
pod install
# open iosApp.xcworkspace in Xcode, and run it.

JS

Use Chrome CORS Unblock extension to workaround with CORS error.

./gradlew :example:shared:jsBrowserRun

Windows

.\gradlew runKmp_socketioDebugExecutableMingwX64

Linux

./gradlew runKmp_socketioDebugExecutableLinuxX64

macOS

./gradlew runKmp_socketioDebugExecutableMacosX64

Publish

Maven central portal credentials and signing configs are set in ~/.gradle/gradle.properties.

# on Linux: need manual release on website
./gradlew clean publishLinuxX64PublicationToMavenCentralRepository --no-configuration-cache
# on Windows: need manual release on website
.\gradlew clean publishMingwX64PublicationToMavenCentralRepository --no-configuration-cache
# on macOS: need manual release on website
./gradlew clean \
    publishKotlinMultiplatformPublicationToMavenCentralRepository \
    publishJvmPublicationToMavenCentralRepository \
    publishIosArm64PublicationToMavenCentralRepository \
    publishIosSimulatorArm64PublicationToMavenCentralRepository \
    publishIosX64PublicationToMavenCentralRepository \
    publishMacosArm64PublicationToMavenCentralRepository \
    publishMacosX64PublicationToMavenCentralRepository \
    publishJsPublicationToMavenCentralRepository \
    --no-configuration-cache

Login to https://central.sonatype.com/publishing/deployments, and release them manually.

Credit

0