Last active
September 16, 2020 16:58
-
-
Save adamw/a9a59d0690e6f1982f1fcc69de63ba09 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.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