-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ktor for websocket, add live price implementation
- Loading branch information
Showing
9 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/root14/hoopoe/data/WebSocketHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.root14.hoopoe.data | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.plugins.websocket.WebSockets | ||
import io.ktor.client.plugins.websocket.webSocket | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.websocket.Frame | ||
import io.ktor.websocket.readText | ||
|
||
/** | ||
*@sample WebSocketHelper().subscribeWebSocket(object : WebSocketHelper.IWebSocketListener { | ||
* override fun observeSocket(data: String) { | ||
* println(data) | ||
* } | ||
* }) | ||
*/ | ||
class WebSocketHelper { | ||
private val client = HttpClient { | ||
install(WebSockets) | ||
} | ||
|
||
interface IWebSocketListener { | ||
fun observeSocket(data: String) | ||
} | ||
|
||
//default listen bitcoin | ||
suspend fun subscribeWebSocket( | ||
listener: IWebSocketListener, | ||
host: String = "ws.coincap.io", | ||
asset: String = "bitcoin", | ||
path: String = "/prices?assets=${asset}" | ||
): IWebSocketListener { | ||
client.webSocket( | ||
method = HttpMethod.Get, host = host, path = path | ||
) { | ||
while (true) { | ||
val othersMessage = incoming.receive() as? Frame.Text ?: continue | ||
//println(othersMessage.readText()) | ||
listener.observeSocket(othersMessage.readText()) | ||
} | ||
} | ||
return listener | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/root14/hoopoe/view/binding/livePrice.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.root14.hoopoe.view.binding | ||
|
||
import android.widget.TextView | ||
import androidx.databinding.BindingAdapter | ||
import com.root14.hoopoe.utils.convertPriceFormat | ||
|
||
@BindingAdapter("setLivePrice") | ||
fun setLivePrice(textView: TextView, data: String) { | ||
if (data.isNotEmpty()) { | ||
textView.text = data.convertPriceFormat() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters