/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }
/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }
/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }