9/20/2023 0 Comments Scala http client![]() The spray-httpx Request Building and Response Transformation traits. When you import ._ you not only get easy access to sendReceive but also all elements of The response coming back is thenĭecompressed and its entity unmarshalled. This defines a more complex pipeline that takes an HttpRequest, adds headers and compresses its entity beforeĭispatching it to the target server (the sendReceive element of the pipeline). import MyJsonProtocol._ implicit val system = ActorSystem () import system.dispatcher // execution context for futures val pipeline : HttpRequest => Future = ( addHeader ( "X-My-Special-Header", "fancy-value" ) ~> addCredentials ( BasicHttpCredentials ( "bob", "secret" )) ~> encode ( Gzip ) ~> sendReceive ~> decode ( Deflate ) ~> unmarshal ) val response : Future = pipeline ( Post ( "", Order ( 42 ))) Need to be serialized to HTTP requests or deserialized from HTTP responses.Ĭheck out this snippet for an example of what spray-client pipelining can do for you in that regard: The core of the library has no dependencies, and it's very easy to get started both when using SBT or Ammonite - just take a look at the readme. If you've haven't heard about it, here's an introductory post from last week. Many times you actually want to send and/or receive custom objects that sttp is a Scala HTTP client library with the goal of providing a simple, no-surprises, immutable API. Interpretation of responses completely to you. ![]() HostConnectorInfo ( connector, _ ) Future is nice start but leaves the creation of requests and Materializer materializer = SystemMaterializer.get(system).Import akka.io.IO import import import spray.http._ import ._ implicit val system = ActorSystem () import system.dispatcher // execution context for futures val pipeline : Future = for ( Http. Val connected = upgradeResponse.map Java copy source ActorSystem system = ActorSystem.create() Http().singleWebSocketRequest(WebSocketRequest("ws://"), flow) and closed is a Future representing the stream completion from above ![]() completes or fails when the connection succeeds or fails Val flow: Flow] =įlow.fromSinkAndSourceMat(printSink, helloSource)(Keep.left) and it is completed when the stream completes the Future is the materialized value of Sink.foreach Source.single(TextMessage("hello world!")) print each incoming strict text message Simple example sending a message and printing any incoming message: Scala copy source /* The future will succeed when the WebSocket connection has been established or the server returned a regular HTTP response, or fail if the connection fails with an exception. It will trigger the request right away and returns a tuple containing the Future CompletionStage and the materialized value from the flow passed to the method. SingleWebSocketRequest takes a WebSocketRequest WebSocketRequest and a flow it will connect to the source and sink of the WebSocket connection. In these cases the data is provided as a Source Source for binary and Source Source for text messages. ![]() The strict text is available from TextMessage.Strict TextMessage.getStrictText and strict binary data from BinaryMessage.Strict BinaryMessage.getStrictData.įor streamed messages BinaryMessage.Streamed BinaryMessage.getStreamedData and TextMessage.Streamed TextMessage.getStreamedText will be used. In typical applications messages will be Strict as WebSockets are usually deployed to communicate using small messages not stream data, the protocol does however allow this (by not marking the first fragment as final, as described in RFC 6455 section 5.2). Messages sent and received over a WebSocket can be either TextMessage TextMessage s or BinaryMessage BinaryMessage s and each of those has two subtypes Strict (all data in one chunk) or Streamed. Make sure to read and understand the section about Half-Closed WebSockets as the behavior when using WebSockets for one-way communication may not be what you would expect. If the connection fails, for example with a 404 NotFound error, this regular HTTP result can be found in WebSocketUpgradeResponse.response The methods of the WebSocket client API handle the upgrade to WebSocket on connection success and materializes the connected WebSocket stream. Therefore a WebSocket connection is modelled as either something you connect a Flow Flow to or a Flow Flow that you connect a Source Source and a Sink Sink to.Ī WebSocket request starts with a regular HTTP request which contains an Upgrade header (and possibly other regular HTTP request properties), so in addition to the flow of messages there also is an initial response from the server, this is modelled with WebSocketUpgradeResponse WebSocketUpgradeResponse. Client-side WebSocket support is available through Http().singleWebSocketRequest Http.get(system).singleWebSocketRequest, Http().webSocketClientFlow Http.get(system).webSocketClientFlow and Http().webSocketClientLayer Http.get(system).webSocketClientLayer.Ī WebSocket consists of two streams of messages, incoming messages (a Sink Sink) and outgoing messages (a Source Source) where either may be signalled first or even be the only direction in which messages flow during the lifetime of the connection. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |