Created
August 26, 2020 12:48
-
-
Save adamw/377a89848d696dc834c8d6452dfd758b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package sttp.client.examples | |
import monix.eval.Task | |
import sttp.client._ | |
import sttp.client.asynchttpclient.monix.AsyncHttpClientMonixBackend | |
import sttp.ws.WebSocket | |
object WebSocketMonix extends App { | |
import monix.execution.Scheduler.Implicits.global | |
def useWebSocket(ws: WebSocket[Task]): Task[Unit] = { | |
def send(i: Int) = ws.sendText(s"Hello $i!") | |
val receive = ws.receiveText().flatMap(t => Task(println(s"RECEIVED: $t"))) | |
send(1) *> send(2) *> receive *> receive | |
} | |
AsyncHttpClientMonixBackend | |
.resource() | |
.use { backend => | |
basicRequest | |
.response(asWebSocket(useWebSocket)) | |
.get(uri"wss://echo.websocket.org") | |
.send(backend) | |
.void | |
} | |
.runSyncUnsafe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment