akka.http.scaladsl.model.ws.TextMessage Scala Examples
The following examples show how to use akka.http.scaladsl.model.ws.TextMessage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: WebSocketMessageHandler.scala From asura with MIT License | 5 votes |
package asura.core.actor.flow import akka.NotUsed import akka.actor.{ActorRef, PoisonPill} import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.OverflowStrategy import akka.stream.scaladsl.{Flow, Sink, Source} import asura.common.actor.{ActorEvent, SenderMessage} import asura.common.exceptions.InvalidStatusException import asura.core.CoreConfig import asura.core.util.JacksonSupport import scala.concurrent.duration._ object WebSocketMessageHandler { val DEFAULT_BUFFER_SIZE = CoreConfig.DEFAULT_WS_ACTOR_BUFFER_SIZE val KEEP_ALIVE_INTERVAL = 2 def newHandleFlow[T <: AnyRef](workActor: ActorRef, msgClass: Class[T]): Flow[Message, Message, NotUsed] = { val incomingMessages: Sink[Message, NotUsed] = Flow[Message].map { case TextMessage.Strict(text) => JacksonSupport.parse(text, msgClass) case _ => throw InvalidStatusException("Unsupported message type") }.to(Sink.actorRef[T](workActor, PoisonPill)) val outgoingMessages: Source[Message, NotUsed] = Source.actorRef[ActorEvent](DEFAULT_BUFFER_SIZE, OverflowStrategy.dropHead) .mapMaterializedValue { outActor => workActor ! SenderMessage(outActor) NotUsed } .map(result => TextMessage(JacksonSupport.stringify(result))) .keepAlive(KEEP_ALIVE_INTERVAL.seconds, () => TextMessage.Strict("")) Flow.fromSinkAndSource(incomingMessages, outgoingMessages) } def newHandleStringFlow[T <: AnyRef](workActor: ActorRef, msgClass: Class[T]): Flow[Message, Message, NotUsed] = { val incomingMessages: Sink[Message, NotUsed] = Flow[Message].map { case TextMessage.Strict(text) => JacksonSupport.parse(text, msgClass) case _ => throw InvalidStatusException("Unsupported message type") }.to(Sink.actorRef[T](workActor, PoisonPill)) val outgoingMessages: Source[Message, NotUsed] = Source.actorRef[String](DEFAULT_BUFFER_SIZE, OverflowStrategy.dropHead) .mapMaterializedValue { outActor => workActor ! SenderMessage(outActor) NotUsed } .map(result => TextMessage(result)) .keepAlive(KEEP_ALIVE_INTERVAL.seconds, () => TextMessage.Strict("")) Flow.fromSinkAndSource(incomingMessages, outgoingMessages) } def stringToActorEventFlow[T <: AnyRef](workActor: ActorRef, msgClass: Class[T]): Flow[String, String, NotUsed] = { val incomingMessages: Sink[String, NotUsed] = Flow[String].map { case text: String => JacksonSupport.parse(text, msgClass) }.to(Sink.actorRef[T](workActor, PoisonPill)) val outgoingMessages: Source[String, NotUsed] = Source.actorRef[ActorEvent](DEFAULT_BUFFER_SIZE, OverflowStrategy.dropHead) .mapMaterializedValue { outActor => workActor ! SenderMessage(outActor) NotUsed } .map(result => JacksonSupport.stringify(result)) .keepAlive(KEEP_ALIVE_INTERVAL.seconds, () => "") Flow.fromSinkAndSource(incomingMessages, outgoingMessages) } def stringToActorEventFlow[T <: AnyRef](workActor: ActorRef): Flow[String, String, NotUsed] = { val incomingMessages: Sink[String, NotUsed] = Flow[String].to(Sink.actorRef[String](workActor, PoisonPill)) val outgoingMessages: Source[String, NotUsed] = Source.actorRef[ActorEvent](DEFAULT_BUFFER_SIZE, OverflowStrategy.dropHead) .mapMaterializedValue { outActor => workActor ! SenderMessage(outActor) NotUsed } .map(result => JacksonSupport.stringify(result)) .keepAlive(KEEP_ALIVE_INTERVAL.seconds, () => "") Flow.fromSinkAndSource(incomingMessages, outgoingMessages) } }
Example 2
Source File: HttpOutcomingSenderTest.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend import akka.actor.{ActorSystem, Props} import akka.http.scaladsl.model.ws.TextMessage import akka.testkit.{TestActorRef, TestActors, TestKit, TestProbe} import com.sumologic.sumobot.core.HttpReceptionist import com.sumologic.sumobot.core.model.{IncomingMessage, OutgoingMessage} import com.sumologic.sumobot.test.SumoBotSpec import com.sumologic.sumobot.test.annotated.SumoBotTestKit import org.scalatest.BeforeAndAfterAll class HttpOutcomingSenderTest extends SumoBotTestKit(ActorSystem("HttpOutcomingSenderTest")) with BeforeAndAfterAll { private val probe = new TestProbe(system) system.eventStream.subscribe(probe.ref, classOf[TextMessage.Strict]) private val httpOutcomingSender = TestActorRef(new HttpOutcomingSender(probe.ref)) "HttpOutcomingSender" should { "send TextMessage" when { "received OutgoingMessage" in { val outgoingMessage = OutgoingMessage(HttpReceptionist.DefaultSumoBotChannel, "hello!") system.eventStream.publish(outgoingMessage) val result = probe.expectMsgClass(classOf[TextMessage.Strict]) result.getStrictText should be ("hello!") } } "stop publisher" when { "it is stopped" in { val dummyActor = TestActorRef(TestActors.blackholeProps) val testProbe = TestProbe() testProbe.watch(dummyActor) val stoppedSender = TestActorRef(new HttpOutcomingSender(dummyActor)) system.stop(stoppedSender) testProbe.expectTerminated(dummyActor) } } } override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } }
Example 3
Source File: HttpIncomingReceiverTest.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend import java.time.Instant import akka.actor.{ActorSystem, Props} import akka.http.scaladsl.model.ws.TextMessage import akka.stream.scaladsl.Source import akka.testkit.{TestActorRef, TestActors, TestKit, TestProbe} import com.sumologic.sumobot.core.HttpReceptionist import com.sumologic.sumobot.core.model.IncomingMessage import com.sumologic.sumobot.test.SumoBotSpec import com.sumologic.sumobot.test.annotated.SumoBotTestKit import org.scalatest.BeforeAndAfterAll class HttpIncomingReceiverTest extends SumoBotTestKit(ActorSystem("HttpIncomingReceiverTest")) with BeforeAndAfterAll { private val probe = new TestProbe(system) system.eventStream.subscribe(probe.ref, classOf[IncomingMessage]) private val dummyActor = TestActorRef(TestActors.blackholeProps) private val httpIncomingReceiver = TestActorRef(new HttpIncomingReceiver(dummyActor)) "HttpIncomingReceiver" should { "publish IncomingMessage" when { "received streamed TextMessage" in { val msgSource = Source(List("hello")) val streamedMsg = TextMessage.Streamed(msgSource) httpIncomingReceiver ! streamedMsg val result = probe.expectMsgClass(classOf[IncomingMessage]) result.canonicalText should be ("hello") result.addressedToUs should be (true) result.channel should be (HttpReceptionist.DefaultSumoBotChannel) result.attachments should be (Seq.empty) result.sentBy.plainTextReference should be (HttpReceptionist.DefaultClientUser.id) } "received strict TextMessage" in { val strictMsg = TextMessage.Strict("hi!") httpIncomingReceiver ! strictMsg val result = probe.expectMsgClass(classOf[IncomingMessage]) result.canonicalText should be ("hi!") result.addressedToUs should be (true) result.channel should be (HttpReceptionist.DefaultSumoBotChannel) result.attachments should be (Seq.empty) result.sentBy.plainTextReference should be (HttpReceptionist.DefaultClientUser.id) } "properly format date" when { "sending IncomingMessage" in { val strictMsg = TextMessage.Strict("test") httpIncomingReceiver ! strictMsg val result = probe.expectMsgClass(classOf[IncomingMessage]) val currentDate = Instant.now().getEpochSecond.toDouble val messageDate = result.idTimestamp.toDouble messageDate should be (currentDate +- 5.0) } } } "stop itself and outcoming actor" when { "stream ended" in { val outcomingActor = TestActorRef(TestActors.blackholeProps) val testProbeOutcoming = TestProbe() testProbeOutcoming.watch(outcomingActor) val shutdownReceiver = TestActorRef(new HttpIncomingReceiver(outcomingActor)) val testProbeShutdown = TestProbe() testProbeShutdown.watch(shutdownReceiver) shutdownReceiver ! HttpIncomingReceiver.StreamEnded testProbeOutcoming.expectTerminated(outcomingActor) testProbeShutdown.expectTerminated(shutdownReceiver) } } } override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } }
Example 4
Source File: HttpOutcomingSender.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend import akka.actor.{Actor, ActorLogging, ActorRef} import akka.http.scaladsl.model.ws.TextMessage import com.sumologic.sumobot.core.model.OutgoingMessage class HttpOutcomingSender(publisherRef: ActorRef) extends Actor with ActorLogging { override def preStart(): Unit = { Seq(classOf[OutgoingMessage]).foreach(context.system.eventStream.subscribe(self, _)) } override def receive: Receive = { case OutgoingMessage(_, text, _) => publisherRef ! TextMessage(text) } override def postStop(): Unit = { context.stop(publisherRef) context.system.eventStream.unsubscribe(self) } }
Example 5
Source File: HttpIncomingReceiver.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend import java.time.Instant import java.util.concurrent.TimeUnit import akka.actor.{Actor, ActorLogging, ActorRef} import akka.pattern.pipe import scala.concurrent.ExecutionContext.Implicits.global import akka.http.scaladsl.model.ws.TextMessage import akka.stream.ActorMaterializer import com.sumologic.sumobot.core.HttpReceptionist import com.sumologic.sumobot.core.model.{IncomingMessage, UserSender} import scala.concurrent.duration.Duration object HttpIncomingReceiver { case class StreamEnded() private val StrictTimeout = Duration.create(5, TimeUnit.SECONDS) } class HttpIncomingReceiver(outcomingRef: ActorRef) extends Actor with ActorLogging { private implicit val materializer = ActorMaterializer() override def receive: Receive = { case streamedMsg: TextMessage.Streamed => streamedMsg.toStrict(HttpIncomingReceiver.StrictTimeout).pipeTo(self)(sender()) case strictMsg: TextMessage.Strict => val contents = strictMsg.getStrictText val incomingMessage = IncomingMessage(contents, true, HttpReceptionist.DefaultSumoBotChannel, formatDateNow(), None, Seq.empty, UserSender(HttpReceptionist.DefaultClientUser)) context.system.eventStream.publish(incomingMessage) case HttpIncomingReceiver.StreamEnded => context.stop(outcomingRef) context.stop(self) } private def formatDateNow(): String = { s"${Instant.now().getEpochSecond}.000000" } }
Example 6
Source File: Server.scala From chat-with-akka-http-websockets with Apache License 2.0 | 5 votes |
package chat import akka.NotUsed import akka.actor._ import akka.http.scaladsl._ import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.http.scaladsl.server.Directives._ import akka.stream._ import akka.stream.scaladsl._ import scala.concurrent.duration._ import scala.concurrent.Await import scala.io.StdIn object Server { def main(args: Array[String]): Unit = { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() val chatRoom = system.actorOf(Props(new ChatRoom), "chat") def newUser(): Flow[Message, Message, NotUsed] = { // new connection - new user actor val userActor = system.actorOf(Props(new User(chatRoom))) val incomingMessages: Sink[Message, NotUsed] = Flow[Message].map { // transform websocket message to domain message case TextMessage.Strict(text) => User.IncomingMessage(text) }.to(Sink.actorRef[User.IncomingMessage](userActor, PoisonPill)) val outgoingMessages: Source[Message, NotUsed] = Source.actorRef[User.OutgoingMessage](10, OverflowStrategy.fail) .mapMaterializedValue { outActor => // give the user actor a way to send messages out userActor ! User.Connected(outActor) NotUsed }.map( // transform domain message to web socket message (outMsg: User.OutgoingMessage) => TextMessage(outMsg.text)) // then combine both to a flow Flow.fromSinkAndSource(incomingMessages, outgoingMessages) } val route = path("chat") { get { handleWebSocketMessages(newUser()) } } val binding = Await.result(Http().bindAndHandle(route, "127.0.0.1", 8080), 3.seconds) // the rest of the sample code will go here println("Started server at 127.0.0.1:8080, press enter to kill server") StdIn.readLine() system.terminate() } }
Example 7
Source File: WebSocketRoute.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.http_api.ws import java.util.UUID import akka.actor.PoisonPill import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.ws.{ Message, TextMessage } import akka.http.scaladsl.server.Route import akka.stream._ import akka.stream.scaladsl.{ Flow, Sink, Source } import akka.util.Timeout import io.vamp.common.akka.IoC._ import io.vamp.common.http.{ HttpApiDirectives, HttpApiHandlers, TerminateFlowStage } import io.vamp.common.{ Config, Namespace } import io.vamp.http_api.ws.WebSocketActor.{ SessionClosed, SessionEvent, SessionOpened, SessionRequest } import io.vamp.http_api.{ AbstractRoute, LogDirective } import scala.concurrent.Future trait WebSocketRoute extends AbstractRoute with WebSocketMarshaller with HttpApiHandlers { this: HttpApiDirectives with LogDirective ⇒ implicit def materializer: Materializer private lazy val limit = Config.int("vamp.http-api.websocket.stream-limit") protected def websocketApiHandler(implicit namespace: Namespace, timeout: Timeout): Route def websocketRoutes(implicit namespace: Namespace, timeout: Timeout) = { pathEndOrSingleSlash { get { extractRequest { request ⇒ handleWebSocketMessages { websocket(request) } } } } } protected def filterWebSocketOutput(message: AnyRef)(implicit namespace: Namespace, timeout: Timeout): Future[Boolean] = Future.successful(true) private def apiHandler(implicit namespace: Namespace, timeout: Timeout) = Route.asyncHandler(log { websocketApiHandler }) private def websocket(origin: HttpRequest)(implicit namespace: Namespace, timeout: Timeout): Flow[AnyRef, Message, Any] = { val id = UUID.randomUUID() val in = Flow[AnyRef].collect { case TextMessage.Strict(message) ⇒ Future.successful(message) case TextMessage.Streamed(stream) ⇒ stream.limit(limit()).completionTimeout(timeout.duration).runFold("")(_ + _) }.mapAsync(parallelism = 3)(identity) .mapConcat(unmarshall) .map(SessionRequest(apiHandler, id, origin, _)) .to(Sink.actorRef[SessionEvent](actorFor[WebSocketActor], SessionClosed(id))) val out = Source.actorRef[AnyRef](16, OverflowStrategy.dropHead) .mapMaterializedValue(actorFor[WebSocketActor] ! SessionOpened(id, _)) .via(new TerminateFlowStage[AnyRef](_ == PoisonPill)) .mapAsync(parallelism = 3)(message ⇒ filterWebSocketOutput(message).map(f ⇒ f → message)) .collect { case (true, m) ⇒ m } .map(message ⇒ TextMessage.Strict(marshall(message))) Flow.fromSinkAndSource(in, out) } }
Example 8
Source File: SttpBackendStubAkkaTests.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import akka.actor.ActorSystem import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.OverflowStrategy import akka.stream.scaladsl.{Flow, Keep, Sink, Source} import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.ScalaFutures import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import sttp.client._ import sttp.model.Headers import scala.concurrent.{Await, Future} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ class SttpBackendStubAkkaTests extends AnyFlatSpec with Matchers with ScalaFutures with BeforeAndAfterAll { implicit val system: ActorSystem = ActorSystem() override protected def afterAll(): Unit = { Await.result(system.terminate().map(_ => ()), 5.seconds) } "backend stub" should "cycle through responses using a single sent request" in { // given implicit val backend = AkkaHttpBackend.stub .whenRequestMatches(_ => true) .thenRespondCyclic("a", "b", "c") // when def r = basicRequest.get(uri"http://example.org/a/b/c").send().futureValue // then r.body shouldBe Right("a") r.body shouldBe Right("b") r.body shouldBe Right("c") r.body shouldBe Right("a") } it should "use given flow as web socket handler" in { // This test is an example how can we test client flow. // We check behavior of client when connected to echo server. // Client responsibility was to send two messages to the server and collect received messages. val useHandler: Flow[Message, Message, Future[Seq[Message]]] => Future[Seq[Message]] = clientFlow => { val ((outQueue, clientReceivedMessages), inQueue) = Source .queue(1, OverflowStrategy.fail) .viaMat(clientFlow)(Keep.both) .toMat(Sink.queue())(Keep.both) .run() def echoMsg(): Future[Unit] = inQueue.pull().flatMap { case None => echoMsg() case Some(msg) => outQueue.offer(TextMessage(s"echo: " + msg.asTextMessage.getStrictText)).map(_ => ()) } (for { _ <- outQueue.offer(TextMessage("Hi!")) _ <- echoMsg() _ <- echoMsg() _ = outQueue.complete() _ <- outQueue.watchCompletion() } yield ()).flatMap(_ => clientReceivedMessages) } val clientFlow: Flow[Message, Message, Future[Seq[Message]]] = { Flow.fromSinkAndSourceMat( Sink.seq[Message], Source((1 to 2).map(i => TextMessage(s"test$i"))) )(Keep.left) } implicit val b = AkkaHttpBackend.stub .whenRequestMatches(_ => true) .thenHandleOpenWebSocket(Headers(List.empty), useHandler) val receivedMessages = basicRequest .get(uri"wss://echo.websocket.org") .openWebsocket(clientFlow) .flatMap(_.result) .futureValue .toList receivedMessages shouldBe List("Hi!", "echo: test1", "echo: test2").map(TextMessage(_)) } }
Example 9
Source File: AkkaHttpWebsocketTest.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import java.util.concurrent.ConcurrentLinkedQueue import akka.Done import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.Materializer import akka.stream.scaladsl._ import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.{Eventually, IntegrationPatience} import sttp.client._ import scala.collection.JavaConverters._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Success import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import sttp.client.testing.HttpTest.wsEndpoint class AkkaHttpWebsocketTest extends AsyncFlatSpec with Matchers with BeforeAndAfterAll with Eventually with IntegrationPatience { implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global implicit val backend: SttpBackend[Future, Nothing, Flow[Message, Message, *]] = AkkaHttpBackend() it should "send and receive ten messages" in { val received = new ConcurrentLinkedQueue[String]() val sink: Sink[Message, Future[Done]] = collectionSink(received) val n = 10 val source: Source[Message, Promise[Option[Message]]] = Source((1 to n).map(i => TextMessage(s"test$i"))).concatMat(Source.maybe[Message])(Keep.right) val flow: Flow[Message, Message, (Future[Done], Promise[Option[Message]])] = Flow.fromSinkAndSourceMat(sink, source)(Keep.both) basicRequest.get(uri"$wsEndpoint/ws/echo").openWebsocket(flow).flatMap { r => eventually { received.asScala.toList shouldBe (1 to n).map(i => s"echo: test$i").toList } r.result._2.complete(Success(None)) // the source should now complete r.result._1.map(_ => succeed) // the future should be completed once the stream completes (and the ws closes) } } it should "receive two messages" in { val received = new ConcurrentLinkedQueue[String]() val sink: Sink[Message, Future[Done]] = collectionSink(received) val source: Source[Message, Promise[Option[Message]]] = Source.maybe[Message] val flow: Flow[Message, Message, Promise[Option[Message]]] = Flow.fromSinkAndSourceMat(sink, source)(Keep.right) basicRequest.get(uri"$wsEndpoint/ws/send_and_wait").openWebsocket(flow).flatMap { r => eventually { received.asScala.toList shouldBe List("test10", "test20") } r.result.success(None) // closing succeed } } it should "error if the endpoint is not a websocket" in { basicRequest.get(uri"$wsEndpoint/echo").openWebsocket(Flow.apply[Message]).failed.map { t => t shouldBe a[NotAWebsocketException] } } def collectionSink(queue: ConcurrentLinkedQueue[String]): Sink[Message, Future[Done]] = Sink .setup[Message, Future[Done]] { (_materializer, _) => Flow[Message] // mapping with parallelism 1 so that messages don't get reordered .mapAsync(1) { case m: TextMessage => implicit val materializer: Materializer = _materializer m.toStrict(1.second).map(Some(_)) case _ => Future.successful(None) } .collect { case Some(TextMessage.Strict(text)) => text } .toMat(Sink.foreach(queue.add))(Keep.right) } .mapMaterializedValue(_.flatMap(identity)) override protected def afterAll(): Unit = { backend.close() super.afterAll() } }
Example 10
Source File: WsConnection.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.api.ws.connection import java.util.concurrent.ConcurrentLinkedQueue import akka.Done import akka.actor.{ActorRef, ActorSystem, Status} import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage, WebSocketRequest} import akka.stream.scaladsl.{Flow, Sink, Source} import akka.stream.{CompletionStrategy, Materializer, OverflowStrategy} import com.wavesplatform.dex.api.ws.protocol.{WsClientMessage, WsMessage, WsPingOrPong, WsServerMessage} import com.wavesplatform.dex.domain.utils.ScorexLogging import play.api.libs.json.Json import scala.collection.JavaConverters._ import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} class WsConnection(uri: String, keepAlive: Boolean = true)(implicit system: ActorSystem, materializer: Materializer) extends ScorexLogging { log.info(s"""Connecting to Matcher WS API: | URI = $uri | Keep alive = $keepAlive""".stripMargin) import materializer.executionContext private val wsHandlerRef = system.actorOf(TestWsHandlerActor props keepAlive) protected def stringifyClientMessage(cm: WsClientMessage): TextMessage.Strict = WsMessage.toStrictTextMessage(cm)(WsClientMessage.wsClientMessageWrites) // From test to server private val source: Source[TextMessage.Strict, ActorRef] = { val completionMatcher: PartialFunction[Any, CompletionStrategy] = { case akka.actor.Status.Success(_) => CompletionStrategy.draining } val failureMatcher: PartialFunction[Any, Throwable] = { case Status.Failure(cause) => cause } Source .actorRef[WsClientMessage](completionMatcher, failureMatcher, 10, OverflowStrategy.fail) .map(stringifyClientMessage) .mapMaterializedValue { source => wsHandlerRef.tell(TestWsHandlerActor.AssignSourceRef, source) source } } private val messagesBuffer: ConcurrentLinkedQueue[WsServerMessage] = new ConcurrentLinkedQueue[WsServerMessage]() // From server to test private val sink: Sink[Message, Future[Done]] = Sink.foreach { case tm: TextMessage => for { strictText <- tm.toStrict(1.second).map(_.getStrictText) clientMessage <- { log.trace(s"Got $strictText") Try { Json.parse(strictText).as[WsServerMessage] } match { case Failure(exception) => Future.failed(exception) case Success(x) => { messagesBuffer.add(x) if (keepAlive) x match { case value: WsPingOrPong => wsHandlerRef ! value case _ => } Future.successful(x) } } } } yield clientMessage case bm: BinaryMessage => bm.dataStream.runWith(Sink.ignore) Future.failed { new IllegalArgumentException("Binary messages are not supported") } } private val flow: Flow[Message, TextMessage.Strict, Future[Done]] = Flow.fromSinkAndSourceCoupled(sink, source).watchTermination() { case (_, f) => f.onComplete { case Success(_) => log.info(s"WebSocket connection to $uri successfully closed") case Failure(e) => log.error(s"WebSocket connection to $uri closed with an error", e) }(materializer.executionContext) f } val (connectionResponse, closed) = Http().singleWebSocketRequest(WebSocketRequest(uri), flow) val connectionOpenedTs: Long = System.currentTimeMillis val connectionClosedTs: Future[Long] = closed.map(_ => System.currentTimeMillis) val connectionLifetime: Future[FiniteDuration] = connectionClosedTs.map(cc => FiniteDuration(cc - connectionOpenedTs, MILLISECONDS)) def messages: List[WsServerMessage] = messagesBuffer.iterator().asScala.toList def clearMessages(): Unit = messagesBuffer.clear() def send(message: WsClientMessage): Unit = wsHandlerRef ! TestWsHandlerActor.SendToServer(message) def close(): Unit = if (!isClosed) wsHandlerRef ! TestWsHandlerActor.CloseConnection def isClosed: Boolean = closed.isCompleted }
Example 11
Source File: Webservice.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat import java.util.Date import akka.actor.ActorSystem import akka.http.scaladsl.model.ws.{ Message, TextMessage } import scala.concurrent.duration._ import akka.http.scaladsl.server.Directives import akka.stream.scaladsl.Flow import upickle.default._ import shared.Protocol import shared.Protocol._ import scala.util.Failure class Webservice(implicit system: ActorSystem) extends Directives { val theChat = Chat.create() import system.dispatcher system.scheduler.scheduleAtFixedRate(15.second, 15.second) { () => theChat.injectMessage(ChatMessage(sender = "clock", s"Bling! The time is ${new Date().toString}.")) } def route = get { pathSingleSlash { getFromResource("web/index.html") } ~ // Scala-JS puts them in the root of the resource directory per default, // so that's where we pick them up path("frontend-launcher.js")(getFromResource("frontend-launcher.js")) ~ path("frontend-fastopt.js")(getFromResource("frontend-fastopt.js")) ~ path("chat") { parameter("name") { name => handleWebSocketMessages(websocketChatFlow(sender = name)) } } } ~ getFromResourceDirectory("web") def websocketChatFlow(sender: String): Flow[Message, Message, Any] = Flow[Message] .collect { case TextMessage.Strict(msg) => msg // unpack incoming WS text messages... // This will lose (ignore) messages not received in one chunk (which is // unlikely because chat messages are small) but absolutely possible // FIXME: We need to handle TextMessage.Streamed as well. } .via(theChat.chatFlow(sender)) // ... and route them through the chatFlow ... .map { case msg: Protocol.Message => TextMessage.Strict(write(msg)) // ... pack outgoing messages into WS JSON messages ... } .via(reportErrorsFlow) // ... then log any processing errors on stdin def reportErrorsFlow[T]: Flow[T, T, Any] = Flow[T] .watchTermination()((_, f) => f.onComplete { case Failure(cause) => println(s"WS stream failed with $cause") case _ => // ignore regular completion }) }
Example 12
Source File: ChatService.scala From heimdallr with Apache License 2.0 | 5 votes |
package chat import java.net._ import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits._ import akka.actor._ import akka.NotUsed import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.model.RemoteAddress import chat.admin.AdminService import EventConstants._ //import chat.utils.HttpClient class ChatService(env: String, admin: AdminService) (implicit system: ActorSystem, mat: ActorMaterializer, dispatcher: ExecutionContext) extends WebService { private var chatSuper: ActorRef = null private var aggregator: ActorRef = null private var httpClient: HttpClient= null private val servicePort = 8000 private val serviceRoute= //<- adjustable depended on client url pathPrefix(IntNumber) { chatRoomID => { chatSuper ! CreateChatRoom(chatRoomID) extractClientIP { ip => handleWebSocketMessages(newUser(chatRoomID, ip)) } } } def regNode(port: Int) = { val localhost = InetAddress.getLocalHost val localIpAddress = localhost.getHostAddress environment.hostName = localIpAddress environment.port = port log.info(s"Server IP Address of System => ${localIpAddress}") } def incomingMessages(userActor: ActorRef): Sink[Message, NotUsed] = { Flow[Message].map { // transform websocket message to domain message case TextMessage.Strict(text) => UserActor.IncomingMessage(text) // PoisonPill asynchronously stops disconnected user actor //TODO : to deal with join, leave, text message types }.to(Sink.actorRef[UserActor.IncomingMessage](userActor, PoisonPill)) } def outgoingMessages(userActor: ActorRef): Source[Message, NotUsed] = { Source.actorRef[UserActor.OutgoingMessage](20000, OverflowStrategy.fail) .mapMaterializedValue { outActor => // give the user actor a way to send messages out userActor ! UserActor.Connected(outActor) NotUsed }.map( // transform domain message to web socket message (outMsg: UserActor.OutgoingMessage) => TextMessage(outMsg.text)) } def newUser(chatRoomID: Int, ip: RemoteAddress): Flow[Message, Message, NotUsed] = { // new connection - new user actor val userActor = system.actorOf(Props(new UserActor(chatRoomID, chatSuper, ChatRooms.httpClient, ip))) // Set Sink & Source val incomingMsg = incomingMessages(userActor) val outgoingMsg = outgoingMessages(userActor) Flow.fromSinkAndSource(incomingMsg, outgoingMsg) } def start() { log.info( "ChatService staring ..." ) environment.setEnvType(env) environment.version = system.settings.config.getString("akka.heimdallr-version" ) chatSuper = system.actorOf(Props(classOf[ChatSupervisor], env), "cs") admin.setChatSupervisorActorRef(chatSuper) regNode(servicePort) serviceBind(this.getClass.getSimpleName, serviceRoute, servicePort) log.info(environment.HeimdallrLogo(system)) } def stop(): Unit = { serviceUnbind(this.getClass.getSimpleName) } }
Example 13
Source File: StreamingUpload.scala From ws_to_kafka with MIT License | 5 votes |
package com.pkinsky import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.http.scaladsl.model.ws.{TextMessage, Message} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.PathMatchers.PathEnd import akka.stream._ import akka.stream.scaladsl._ import com.softwaremill.react.kafka.ReactiveKafka import play.api.libs.json.Json import scala.concurrent.{Future, ExecutionContext} import scala.concurrent.duration._ import scala.util.Success import scala.language.postfixOps object StreamingUpload extends App with AppContext { val kafkaPublisherGraph: RunnableGraph[SourceQueue[Event]] = Source.queue[Event](1024, OverflowStrategy.backpressure).to(kafka.publish[Event](eventTopic)) val sourceQueue: SourceQueue[Event] = kafkaPublisherGraph.run val queueWriter: Sink[Event, Unit] = Flow[Event].mapAsync(1){ elem => sourceQueue.offer(elem) .andThen{ case Success(false) => println(s"failed to publish $elem to topic $eventTopic") case Success(true) => println(s"published $elem to topic $eventTopic") } }.to(Sink.ignore) val parseMessages: Flow[Message, Event, Unit] = Flow[Message] .collect{ case TextMessage.Strict(t) => val js = Json.parse(t) Json.fromJson[Event](js).get } val wsHandlerFlow: Flow[Message, Message, Unit] = Flow.fromSinkAndSource( sink = parseMessages.to(queueWriter), source = Source.maybe ) val routes: Flow[HttpRequest, HttpResponse, Unit] = get { path(PathEnd) { getFromResource("test.html") } ~ path("ws") { println("ws connection accepted") handleWebsocketMessages(wsHandlerFlow) } } Http().bindAndHandle(routes, "localhost", port).andThen{ case util.Failure(t) => println(s"error binding to localhost: $t")} println(s"listening on port $port, press ENTER to stop") awaitTermination() } object KafkaListener extends App with AppContext { val graph = kafka.consume[Event](eventTopic, "kafka_listener").toMat(Sink.foreach(println))(Keep.right) graph.run.onComplete(println) println(s"listening to Kafka topic $eventTopic, press ENTER to stop") awaitTermination() }
Example 14
Source File: LoadTest.scala From ws_to_kafka with MIT License | 5 votes |
package com.pkinsky import java.util.concurrent.atomic.AtomicInteger import akka.http.scaladsl.model.ws.{InvalidUpgradeResponse, WebsocketUpgradeResponse, WebsocketRequest, TextMessage} import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.stream.ThrottleMode import akka.stream.scaladsl.{Keep, Sink, RunnableGraph, Source} import play.api.libs.json.Json import scala.concurrent.{Future, Await} import scala.concurrent.duration._ import scala.language.postfixOps object LoadTest extends App with AppContext { val clients = 256 val eventsPerClient = 256 val eventsSent = new AtomicInteger(0) def testData(clientId: String): Source[Event, Unit] = Source.unfoldInf(1) { n => val event = Event(s"msg number $n", clientId, System.currentTimeMillis()) (n + 1, event) }.take(eventsPerClient).throttle(1, 100 millis, 1, ThrottleMode.Shaping) def wsClient(clientId: String): RunnableGraph[Future[WebsocketUpgradeResponse]] = testData(clientId).map(e => TextMessage.Strict(Json.toJson(e).toString)) .map { x => eventsSent.incrementAndGet(); x } .viaMat(Http().websocketClientFlow(WebsocketRequest(Uri(s"ws://localhost:$port/ws"))))(Keep.right).to(Sink.ignore) //set up websocket connections (1 to clients).foreach { id => wsClient(s"client $id").run() } //watch kafka for messages sent via websocket val kafkaConsumerGraph: RunnableGraph[Future[Seq[Event]]] = kafka.consume[Event](eventTopic, "group_new") .take(clients * eventsPerClient).takeWithin(2 minutes) .toMat(Sink.seq)(Keep.right) val res = Await.result(kafkaConsumerGraph.run, 5 minutes) println(s"sent ${eventsSent.get()} events total") println(s"res size: ${res.length}") }
Example 15
Source File: Demo.scala From chordial with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.tristanpenman.chordial.demo import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.TextMessage import akka.stream.scaladsl._ import akka.stream.{ActorAttributes, ActorMaterializer, OverflowStrategy, Supervision} import akka.util.Timeout import com.tristanpenman.chordial.core.Event import com.tristanpenman.chordial.core.Event._ import scala.concurrent.Await import scala.concurrent.duration._ object Demo extends App { implicit val system = ActorSystem("chordial-demo") implicit val mat = ActorMaterializer() implicit val ec = system.dispatcher implicit val timeout: Timeout = 3.seconds // Generate IDs ranging from 0 to 63 (inclusive) so that when visualising the network, // each node represents a ~5.625 degree arc on the ring private val keyspaceBits = 6 // Create an actor that is responsible for creating and terminating nodes, while ensuring // that nodes are assigned unique IDs in the Chord key-space private val governor = system.actorOf(Governor.props(keyspaceBits), "Governor") // Create an actor that will log events published by nodes private val eventWriter = system.actorOf(EventWriter.props, "EventWriter") // Subscribe the EventWriter actor to events published by nodes system.eventStream.subscribe(eventWriter, classOf[Event]) val (listener, eventsSource) = Source .actorRef[Event](Int.MaxValue, OverflowStrategy.fail) .map { case FingerReset(nodeId: Long, index: Int) => s"""{ "type": "FingerReset", "nodeId": $nodeId, "index": $index }""" case FingerUpdated(nodeId: Long, index: Int, fingerId: Long) => s"""{ "type": "FingerUpdated", "nodeId": $nodeId, "index": $index, "fingerId": $fingerId }""" case NodeCreated(nodeId, successorId) => s"""{ "type": "NodeCreated", "nodeId": $nodeId, "successorId": $successorId }""" case NodeShuttingDown(nodeId) => s"""{ "type": "NodeDeleted", "nodeId": $nodeId }""" case PredecessorReset(nodeId) => s"""{ "type": "PredecessorReset", "nodeId": $nodeId }""" case PredecessorUpdated(nodeId, predecessorId) => s"""{ "type": "PredecessorUpdated", "nodeId": $nodeId, "predecessorId": $predecessorId }""" case SuccessorListUpdated(nodeId, primarySuccessorId, _) => s"""{ "type": "SuccessorUpdated", "nodeId": $nodeId, "successorId": $primarySuccessorId }""" } .map(TextMessage(_)) .withAttributes(ActorAttributes.supervisionStrategy(Supervision.resumingDecider)) .toMat(BroadcastHub.sink[TextMessage](bufferSize = 16))(Keep.both) .run() system.eventStream.subscribe(listener, classOf[Event]) Http().bindAndHandle(WebSocketWorker(governor, eventsSource), "0.0.0.0", 4567) Await.result(system.whenTerminated, Duration.Inf) }
Example 16
Source File: WebSocketWorker.scala From chordial with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.tristanpenman.chordial.demo import akka.actor._ import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.scaladsl.{Flow, Source} import ch.megard.akka.http.cors.scaladsl.CorsDirectives._ import scala.concurrent.ExecutionContext final class WebSocketWorker(governor: ActorRef, eventsSource: Source[TextMessage, _])(implicit val ec: ExecutionContext) extends WebService { def route = // scalafmt: { indentOperator = spray } cors() { routes(governor) ~ pathPrefix("eventstream")(getFromResourceDirectory("webapp")) ~ handleWebSocketMessages(Flow[Message].take(0).prepend(eventsSource)) } } object WebSocketWorker { def apply(nodeRef: ActorRef, eventsSource: Source[TextMessage, _])( implicit ec: ExecutionContext): Route = new WebSocketWorker(nodeRef, eventsSource).route }
Example 17
Source File: Flows.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data.server import akka.actor.{ActorRef, Props} import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.FlowShape import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Source} import de.nierbeck.floating.data.domain.Vehicle import GraphDSL.Implicits._ import de.nierbeck.floating.data.server._ import de.nierbeck.floating.data.server.actors.websocket._ object Flows { def graphFlowWithStats(router: ActorRef): Flow[Message, Message, _] = { Flow.fromGraph(GraphDSL.create() { implicit builder => // create an actor source val source = Source.actorPublisher[String](VehiclePublisher.props(router)) // Graph elements we'll use val merge = builder.add(Merge[String](2)) val filter = builder.add(Flow[String].filter(_ => false)) // get BBox from request and send it to route, return nothing ... val mapMsgToString = builder.add(Flow[Message].map[String] { case TextMessage.Strict(msg) => { println(s"received message: $msg") if (msg.contains("close")) { router ! msg } else if (msg.contains("spark")) { router ! SPARK } else if (msg.contains("flink")) { router ! FLINK } else { val bbox = toBoundingBox(msg) println(s"transformedt to bbox: $bbox") router ! bbox } "" } }) //outgoing message ... val mapStringToMsg = builder.add(Flow[String].map[Message](x => TextMessage.Strict(x))) //add source to flow val vehiclesSource = builder.add(source) // connect the graph mapMsgToString ~> filter ~> merge // this part of the merge will never provide msgs vehiclesSource ~> merge ~> mapStringToMsg // expose ports FlowShape(mapMsgToString.in, mapStringToMsg.out) }) } }
Example 18
Source File: WsConnection.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.load.ws import akka.Done import akka.actor.{ActorRef, ActorSystem, Status} import akka.http.scaladsl.Http import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage, WebSocketRequest} import akka.stream.scaladsl.{Flow, Sink, Source} import akka.stream.{CompletionStrategy, Materializer, OverflowStrategy} import com.wavesplatform.dex.api.ws.connection.TestWsHandlerActor import com.wavesplatform.dex.api.ws.protocol.{WsClientMessage, WsMessage, WsServerMessage} import com.wavesplatform.dex.domain.utils.ScorexLogging import play.api.libs.json.Json import scala.concurrent.Future import scala.concurrent.duration.DurationInt import scala.util.{Failure, Success, Try} class WsConnection(uri: String, receive: WsServerMessage => Option[WsClientMessage])(implicit system: ActorSystem) extends ScorexLogging { import system.dispatcher private implicit val materializer = Materializer(system) private val wsHandlerRef = system.actorOf(TestWsHandlerActor.props(keepAlive = true)) log.info(s"Connecting to Matcher WS API: $uri") protected def stringifyClientMessage(cm: WsClientMessage): TextMessage.Strict = WsMessage.toStrictTextMessage(cm)(WsClientMessage.wsClientMessageWrites) // To server private val source: Source[TextMessage.Strict, ActorRef] = { val completionMatcher: PartialFunction[Any, CompletionStrategy] = { case akka.actor.Status.Success(_) => CompletionStrategy.draining } val failureMatcher: PartialFunction[Any, Throwable] = { case Status.Failure(cause) => cause } Source .actorRef[WsClientMessage](completionMatcher, failureMatcher, 10, OverflowStrategy.fail) .map(stringifyClientMessage) .mapMaterializedValue { source => wsHandlerRef.tell(TestWsHandlerActor.AssignSourceRef, source) source } } // To client private val sink: Sink[Message, Future[Done]] = Sink.foreach { case tm: TextMessage => // TODO move to tests for { strictText <- tm.toStrict(1.second).map(_.getStrictText) clientMessage <- { log.trace(s"Got $strictText") Try { Json.parse(strictText).as[WsServerMessage] } match { case Failure(exception) => Future.failed(exception) case Success(x) => Future.successful { receive(x).foreach(wsHandlerRef ! _) } } } } yield clientMessage case bm: BinaryMessage => bm.dataStream.runWith(Sink.ignore) Future.failed { new IllegalArgumentException("Binary messages are not supported") } } private val flow: Flow[Message, TextMessage.Strict, Future[Done]] = Flow.fromSinkAndSourceCoupled(sink, source).watchTermination() { case (_, f) => f.onComplete { case Success(_) => log.info(s"WebSocket connection to $uri successfully closed") case Failure(e) => log.error(s"WebSocket connection to $uri closed with an error", e) }(materializer.executionContext) f } val (connectionResponse, closed) = Http().singleWebSocketRequest(WebSocketRequest(uri), flow) def send(message: WsClientMessage): Unit = wsHandlerRef ! TestWsHandlerActor.SendToServer(message) def isClosed: Boolean = closed.isCompleted def close(): Future[Done] = { if (!isClosed) wsHandlerRef ! TestWsHandlerActor.CloseConnection closed } }