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;
}
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);
}
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);
}
}
Aggregations