In case anyone is looking for an updated version of this code that will 
work with more current akka-http and Java:

https://gist.github.com/bentito/560eb95c64fa131efb34ad62c7bf60f8

-Brett Tofel

On Tuesday, March 8, 2016 at 9:59:26 AM UTC-5, Johan Andrén wrote:
>
> Here is an adaptation of the Scala sample, but in Java:
>
> import akka.NotUsed;
> import akka.actor.*;
> import akka.http.javadsl.model.ws.Message;
> import akka.http.javadsl.model.ws.TextMessage;
> import akka.http.javadsl.server.HttpApp;
> import akka.http.javadsl.server.Route;
> import akka.japi.pf.ReceiveBuilder;
> import akka.stream.OverflowStrategy;
> import akka.stream.javadsl.Flow;
> import akka.stream.javadsl.Sink;
> import akka.stream.javadsl.Source;
>
> import java.util.Optional;
>
> public class WebSocketServer {
>   private static final class Router extends HttpApp {
>
>     private final ActorSystem system;
>
>     public Router(ActorSystem system) {
>       this.system = system;
>     }
>
>     public Route createRoute() {
>       return route(
>         path("test").route(
>           get(handleWebSocketMessages(createWebSocketFlow()))
>         )
>       );
>     }
>
>     private Flow<Message, Message, NotUsed> createWebSocketFlow() {
>       ActorRef actor = system.actorOf(Props.create(AnActor.class));
>
>       Source<Message, NotUsed> source = Source.<Outgoing>actorRef(5, 
> OverflowStrategy.fail())
>         .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
>         .<NotUsed>mapMaterializedValue(destinationRef -> {
>           actor.tell(new OutgoingDestination(destinationRef), 
> ActorRef.noSender());
>           return NotUsed.getInstance();
>         });
>
>       Sink<Message, NotUsed> sink = Flow.<Message>create()
>         .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>         .to(Sink.actorRef(actor, PoisonPill.getInstance()));
>
>
>       return Flow.fromSinkAndSource(sink, source);
>     }
>
>   }
>
>
>
>
>     public static void main(String[] args) {
>         ActorSystem actorSystem = ActorSystem.create();
>
>         Router router = new Router(actorSystem);
>         router.bindRoute("127.0.0.1", 8082, actorSystem);
>     }
>
>   static class Incoming {
>     public final String message;
>     public Incoming(String message) {
>       this.message = message;
>     }
>   }
>
>   static class Outgoing {
>     public final String message;
>     public Outgoing(String message) {
>       this.message = message;
>     }
>   }
>
>   static class OutgoingDestination {
>     public final ActorRef destination;
>     OutgoingDestination(ActorRef destination) {
>       this.destination = destination;
>     }
>   }
>
>   static class AnActor extends AbstractActor {
>
>     private Optional<ActorRef> outgoing = Optional.empty();
>
>     public AnActor() {
>       receive(ReceiveBuilder.match(
>         OutgoingDestination.class, (msg) -> outgoing = 
> Optional.of(msg.destination)
>       ).match(
>         Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
> Outgoing("got it"), self()))
>       ).build());
>     }
>   }
> }
>
>
> Hope this helps.
>
> --
> Johan Andrén
> Akka Team, Lightbend Inc.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to