Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mradkhambek/moon
Moon
https://github.com/mradkhambek/moon
Last synced: 12 days ago
JSON representation
Moon
- Host: GitHub
- URL: https://github.com/mradkhambek/moon
- Owner: MrAdkhambek
- License: apache-2.0
- Created: 2022-02-16T06:38:37.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-02T18:54:09.000Z (over 1 year ago)
- Last Synced: 2023-07-29T10:22:59.361Z (over 1 year ago)
- Language: Kotlin
- Size: 2.01 MB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
## Moon
![Maven Central](https://img.shields.io/maven-central/v/com.adkhambek.moon/moon?logo=Kotlin)
![GitHub](https://img.shields.io/github/license/MrAdkhambek/Moon)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/MrAdkhambek/Moon/ci.yml)A Retrofit inspired [Socket.io](https://socket.io) client for Kotlin (Android, JVM).
For WebSocket [Scarlet](https://github.com/Tinder/Scarlet)> **⚠️ This library works only on Kotlin and Kotlin coroutines**
Dependencies
-------------```groovy
repositories {
mavenCentral()
}dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutines_version}") // REQUIREDimplementation("com.adkhambek.moon:moon:${latest_version}")
// or KTX version for more extensions
implementation("com.adkhambek.moon:moon-ktx:${latest_version}")
}
```Convertors
-------------```groovy
dependencies {
implementation("com.adkhambek.moon:convertor-gson:${latest_version}")
implementation("com.google.code.gson:gson:${gson_version}")
// REQUIRED if you use convertor-gsonimplementation("com.adkhambek.moon:convertor-kotlin-serialization:${latest_version}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${json_version}")
// REQUIRED if you use convertor-kotlin-serialization
}
```Usage
-------------```kotlin
import kotlinx.serialization.Serializable
@Serializable
data class Message(
val message: String
)interface SocketAPI {
@Event(value = "{event}")
fun helloEvent(
@Event(value = "event") event: String
): Flow/**
* This is an example to show backend side
*
* ```js
* socket.emit('ping', {
* 'message': 'pong'
* })
* ```
*
* @return Response (Message) from server
* @author by Mr. Adkhambek
* @see Github
*/
@Event(value = "ping")
fun helloEvent(): Flow/**
* This is an example to show backend side
*
* ```js
* socket.on('test', (arg) => {
* // TODO
* })
* ```
*
* @param message is Request
* @author by Mr. Adkhambek
* @see Github
*/
@Event(value = "test")
suspend fun testEvent(message: Message)/**
* This is an example to show backend side
*
* ```js
* socket.on('testAck', (arg, ack) => {
* // TODO
* ack([
* {
* 'message': 'pong 1'
* },
* {
* 'message': 'pong 2'
* }
* ])
* })
* ```
*
* @param message is Request
* @return Response (Message) from server
* @author by Mr. Adkhambek
* @see Github
*/
@Event(value = "testAck")
suspend fun testAckEvent(message: Message): List
}
``````kotlin
val logger: Logger = TODO()
val socket: io.socket.client.Socket = TODO()
val convertor: EventConvertor.Factory = TODO()// Create moon object with Factory pattern
val moon = Moon.Factory().create(socket, logger, convertor)
moon.connect() // or socket.connect()// Create API Interface object
val socketAPI = moon.create(SocketAPI::class.java)
// or shorter version
val socketAPI: SocketAPI = moon.create()
``````kotlin
class ViewModel(
private val socketAPI: SocketAPI
) {val events: Flow = socketAPI.eventWithBody()
fun singleEvent(body: SomeClass) {
viewModelScope.launch {
val eventResponse = socketAPI.singleEvent(body)
}
}
}
``````kotlin
class ViewModel(
private val moon: Moon,
) {
/**
* For listen socket state
* @see Moon.Status
*/
val state: StateFlow get() = moon.state
}
```## Extensions
```kotlin
public fun Moon.Companion.factory(): Moon.Factorypublic fun Moon.Factory.create(
uri: String,
logger: Logger,
vararg converterFactories: EventConvertor.Factory,
): Moonpublic fun Moon.Factory.create(
uri: String,
opts: Options,
logger: Logger,
vararg converterFactories: EventConvertor.Factory,
): Moonpublic fun Moon.Factory.create(
uri: URI,
logger: Logger,
vararg converterFactories: EventConvertor.Factory,
): Moonpublic fun Moon.Factory.create(
uri: URI,
opts: Options,
logger: Logger,
vararg converterFactories: EventConvertor.Factory,
): Moon
```R8 / ProGuard
-------------If you are using R8 the shrinking and obfuscation rules are included automatically.
ProGuard users must manually add the options from
[moon.pro][proguard file].## TODO
- [x] Feature multiple convertor adapter
- [ ] Custom event
- [ ] Convertor adapter for acknowledgement
- [ ] Convertor adapter for acknowledgement
- [ ] Feature convert adapter for ByteArray
- [ ] Feature FILELicense
=======Copyright 2022 Adkhambek
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 athttp://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.[1]: https://search.maven.org/remote_content?g=com.adkhambek.moon&a=moon&v=LATEST
[snap]: https://s01.oss.sonatype.org/content/repositories/snapshots/
[proguard file]: https://github.com/MrAdkhambek/Moon/blob/main/moon/src/main/resources/META-INF/proguard/moon.pro