Search in sources :

Example 1 with CloseReason

use of io.micronaut.websocket.CloseReason 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)

Example 2 with CloseReason

use of io.micronaut.websocket.CloseReason in project micronaut-core by micronaut-projects.

the class AbstractNettyWebSocketHandler method handleCloseFrame.

private void handleCloseFrame(ChannelHandlerContext ctx, CloseWebSocketFrame cwsf) {
    CloseReason cr = new CloseReason(cwsf.statusCode(), cwsf.reasonText());
    handleCloseReason(ctx, cr, true);
}
Also used : CloseReason(io.micronaut.websocket.CloseReason)

Example 3 with CloseReason

use of io.micronaut.websocket.CloseReason in project micronaut-core by micronaut-projects.

the class AbstractNettyWebSocketHandler method handleUnexpected.

private void handleUnexpected(ChannelHandlerContext ctx, Throwable cause) {
    if (cause instanceof IOException) {
        String msg = cause.getMessage();
        if (msg != null && msg.contains("Connection reset")) {
            // ignore client connection drops
            return;
        }
    }
    if (LOG.isErrorEnabled()) {
        LOG.error("Unexpected Exception in WebSocket [" + webSocketBean.getTarget() + "]: " + cause.getMessage(), cause);
    }
    Channel channel = ctx.channel();
    if (channel.isOpen()) {
        final CloseReason internalError = CloseReason.INTERNAL_ERROR;
        writeCloseFrameAndTerminate(ctx, internalError);
    }
}
Also used : CloseReason(io.micronaut.websocket.CloseReason) Channel(io.netty.channel.Channel) IOException(java.io.IOException)

Aggregations

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