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