Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active September 16, 2020 16:58
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/a9a59d0690e6f1982f1fcc69de63ba09 to your computer and use it in GitHub Desktop.
Save adamw/a9a59d0690e6f1982f1fcc69de63ba09 to your computer and use it in GitHub Desktop.
package sttp.client3.examples
import sttp.client3._
import sttp.client3.akkahttp.AkkaHttpBackend
import sttp.ws.WebSocket
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object WebSocketAkka extends App {
def useWebSocket(ws: WebSocket[Future]): Future[Unit] = {
def send(i: Int) = ws.sendText(s"Hello $i!")
def receive() = ws.receiveText().map(t => println(s"RECEIVED: $t"))
for {
_ <- send(1)
_ <- send(2)
_ <- receive()
_ <- receive()
} yield ()
}
val backend = AkkaHttpBackend()
basicRequest
.response(asWebSocket(useWebSocket))
.get(uri"wss://echo.websocket.org")
.send(backend)
.onComplete(_ => backend.close())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment