Skip to content

Instantly share code, notes, and snippets.

@adamw
Created August 26, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/377a89848d696dc834c8d6452dfd758b to your computer and use it in GitHub Desktop.
Save adamw/377a89848d696dc834c8d6452dfd758b to your computer and use it in GitHub Desktop.
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