Search in sources :

Example 1 with NettyWebSocketSession

use of io.micronaut.http.netty.websocket.NettyWebSocketSession in project micronaut-core by micronaut-projects.

the class NettyServerWebSocketHandler method createWebSocketSession.

@Override
protected NettyWebSocketSession createWebSocketSession(ChannelHandlerContext ctx) {
    String id = originatingRequest.getHeaders().get(HttpHeaderNames.SEC_WEBSOCKET_KEY);
    final Channel channel = ctx.channel();
    NettyWebSocketSession session = new NettyWebSocketSession(id, channel, originatingRequest, mediaTypeCodecRegistry, webSocketVersion.toHttpHeaderValue(), ctx.pipeline().get(SslHandler.class) != null) {

        private final ConvertibleValues<Object> uriVars = ConvertibleValues.of(uriVariables);

        @Override
        public Optional<String> getSubprotocol() {
            return Optional.ofNullable(subProtocol);
        }

        @Override
        public Set<? extends WebSocketSession> getOpenSessions() {
            return webSocketSessionRepository.getChannelGroup().stream().flatMap((Function<Channel, Stream<WebSocketSession>>) ch -> {
                NettyWebSocketSession s = ch.attr(NettyWebSocketSession.WEB_SOCKET_SESSION_KEY).get();
                if (s != null && s.isOpen()) {
                    return Stream.of(s);
                }
                return Stream.empty();
            }).collect(Collectors.toSet());
        }

        @Override
        public void close(CloseReason closeReason) {
            super.close(closeReason);
            webSocketSessionRepository.removeChannel(ctx.channel());
        }

        @Override
        public Optional<Principal> getUserPrincipal() {
            return originatingRequest.getAttribute(HttpAttributes.PRINCIPAL, Principal.class);
        }

        @Override
        public ConvertibleValues<Object> getUriVariables() {
            return uriVars;
        }
    };
    webSocketSessionRepository.addChannel(channel);
    return session;
}
Also used : Function(java.util.function.Function) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) CloseReason(io.micronaut.websocket.CloseReason) Channel(io.netty.channel.Channel) Principal(java.security.Principal) NettyWebSocketSession(io.micronaut.http.netty.websocket.NettyWebSocketSession) NettyWebSocketSession(io.micronaut.http.netty.websocket.NettyWebSocketSession) WebSocketSession(io.micronaut.websocket.WebSocketSession)

Aggregations

ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)1 NettyWebSocketSession (io.micronaut.http.netty.websocket.NettyWebSocketSession)1 CloseReason (io.micronaut.websocket.CloseReason)1 WebSocketSession (io.micronaut.websocket.WebSocketSession)1 Channel (io.netty.channel.Channel)1 Principal (java.security.Principal)1 Function (java.util.function.Function)1