akka-http

Close akka-http websocket connection from server

匆匆过客 提交于 2019-11-30 20:15:37
In my scenario, a client sends "goodbye" websocket message and I need to close previously established connection at the server side. From akka-http docs : Closing connections is possible by cancelling the incoming connection Flow from your server logic (e.g. by connecting its downstream to a Sink.cancelled and its upstream to a Source.empty). It is also possible to shut down the server's socket by cancelling the IncomingConnection source connections. But it's not clear to me how to do that taking into account that Sink and Source are set once when negotiating a new connection: (get & path("ws"

Websocket Proxy using Play 2.6 and akka streams

筅森魡賤 提交于 2019-11-30 15:09:58
I'm trying to create a simple Proxy for Websocket connections using Play and akka streams. The traffic flow is like this: (Client) request -> -> request (Server) Proxy (Client) response <- <- response (Server) I came up with the following code after following some examples: def socket = WebSocket.accept[String, String] { request => val uuid = UUID.randomUUID().toString // wsOut - actor that deals with incoming websocket frame from the Client // wsIn - publisher of the frame for the Server val (wsOut: ActorRef, wsIn: Publisher[String]) = { val source: Source[String, ActorRef] = Source.actorRef

Akka HTTP 2.0 to use SSL (HTTPS)

ε祈祈猫儿з 提交于 2019-11-30 10:02:37
I am trying to add a self signed certificate to my configuration in order to make my web service to serve content over SSL but I had no luck. Here is my current configuration ssl { jksKeystore = "localhost.p12" password = "changeit" } Any ideas on what I'm misisng to be able to spin up an HTTPS server ? My project is using akka 2.0 and scala There are some tests in the akka code base that test the https functionality. They use the predefined http contexts defined in ExampleHttpContexts . I have created a small repo that uses the keys from the akka repository (I hope they won't mind) and

How to stop gracefully the actor system for an akka-http server that must be deployed.

十年热恋 提交于 2019-11-30 09:02:18
问题 I just created my first rest server with akka-http. The problem is that I do not know how to deploy the server in such a way that I could gracefully shutdown the actor system. For example I found something here: https://stackoverflow.com/a/17399574/5388513 where you could use Akka's microkernel, but it is deprecated. I tried using sbt-native-package, but I do not know how to gracefully shutdown the actor system. Thank you! 回答1: You can add shutdown hook: // import scala.concurrent.duration._

akka-http: complete request with flow

杀马特。学长 韩版系。学妹 提交于 2019-11-30 07:12:49
Assume I have set up an arbitrarily complex Flow[HttpRequest, HttpResponse, Unit] . I can already use said flow to handle incoming requests with Http().bindAndHandle(flow, "0.0.0.0", 8080) Now I would like to add logging, leveraging some existing directive, like logRequestResult("my-service"){...} Is there a way to combine this directive with my flow? I guess I am looking for another directive, something along the lines of def completeWithFlow(flow: Flow): Route Is this possible at all? N.B.: logRequestResult is an example, my question applies to any Directive one might find useful. Turns out

Close akka-http websocket connection from server

只谈情不闲聊 提交于 2019-11-30 03:38:46
问题 In my scenario, a client sends "goodbye" websocket message and I need to close previously established connection at the server side. From akka-http docs: Closing connections is possible by cancelling the incoming connection Flow from your server logic (e.g. by connecting its downstream to a Sink.cancelled and its upstream to a Source.empty). It is also possible to shut down the server's socket by cancelling the IncomingConnection source connections. But it's not clear to me how to do that

akka HttpResponse read body as String scala

旧城冷巷雨未停 提交于 2019-11-30 03:18:28
So I have a function with this signature (akka.http.model.HttpResponse): def apply(query: Seq[(String, String)], accept: String): HttpResponse I simply get a value in a test like: val resp = TagAPI(Seq.empty[(String, String)], api.acceptHeader) I want to check its body in a test something like: resp.entity.asString == "tags" My question is how I can get the response body as string? import akka.http.scaladsl.unmarshalling.Unmarshal implicit val system = ActorSystem("System") implicit val materializer = ActorFlowMaterializer() val responseAsString: Future[String] = Unmarshal(entity).to[String]

Websocket Proxy using Play 2.6 and akka streams

帅比萌擦擦* 提交于 2019-11-29 22:38:44
问题 I'm trying to create a simple Proxy for Websocket connections using Play and akka streams. The traffic flow is like this: (Client) request -> -> request (Server) Proxy (Client) response <- <- response (Server) I came up with the following code after following some examples: def socket = WebSocket.accept[String, String] { request => val uuid = UUID.randomUUID().toString // wsOut - actor that deals with incoming websocket frame from the Client // wsIn - publisher of the frame for the Server val

How does one log Akka HTTP client requests

给你一囗甜甜゛ 提交于 2019-11-29 21:27:47
I need to log akka http client requests as well as their responses. While there seems to be a hint of API for logging these requests, there is no clear documentation on how it should be done. My approach has been to create a logged request which transparently wraps Http().singleRequest(req) as follows: def loggedRequest(req: HttpRequest) (implicit system: ActorSystem, ctx: ExecutionContext, m: Materializer): Future[HttpResponse] = { Http().singleRequest(req).map { resp ⇒ Unmarshal(resp.entity).to[String].foreach{s ⇒ system.log.info(req.toString) system.log.info(resp.toString + "\n" + s) } resp

akka-http send continuous chunked http response (stream)

て烟熏妆下的殇ゞ 提交于 2019-11-29 15:13:46
问题 I have this crude test example with akka-http client and server. Server.scala: import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model._ import scala.concurrent.Future class Server extends Runnable { def run() = { implicit val system = ActorSystem("server") implicit val materializer = ActorMaterializer() val serverSource = Http().bind