use of io.micronaut.websocket.WebSocketSession in project micronaut-core by micronaut-projects.
the class BinaryChatServerWebSocket method onOpen.
@OnOpen
public void onOpen(String topic, String username, WebSocketSession session) {
assert ServerRequestContext.currentRequest().isPresent();
Set<? extends WebSocketSession> openSessions = session.getOpenSessions();
System.out.println("Server session opened for username = " + username);
System.out.println("Server openSessions = " + openSessions);
for (WebSocketSession openSession : openSessions) {
if (isValid(topic, session, openSession)) {
String msg = "[" + username + "] Joined!";
System.out.println("Server sending msg = " + msg);
openSession.sendSync(msg.getBytes());
}
}
}
use of io.micronaut.websocket.WebSocketSession in project micronaut-core by micronaut-projects.
the class BinaryChatServerWebSocket method onMessage.
@OnMessage
public void onMessage(String topic, String username, byte[] message, WebSocketSession session) {
assert ServerRequestContext.currentRequest().isPresent();
Set<? extends WebSocketSession> openSessions = session.getOpenSessions();
System.out.println("Server received message = " + new String(message));
System.out.println("Server openSessions = " + openSessions);
for (WebSocketSession openSession : openSessions) {
if (isValid(topic, session, openSession)) {
String msg = "[" + username + "] " + new String(message);
System.out.println("Server sending msg = " + msg);
openSession.sendSync(msg.getBytes());
}
}
}
use of io.micronaut.websocket.WebSocketSession 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.WebSocketSession in project micronaut-core by micronaut-projects.
the class BinaryChatServerWebSocket method onClose.
@OnClose
public void onClose(String topic, String username, WebSocketSession session) {
assert ServerRequestContext.currentRequest().isPresent();
Set<? extends WebSocketSession> openSessions = session.getOpenSessions();
System.out.println("Server session closing for username = " + username);
for (WebSocketSession openSession : openSessions) {
if (isValid(topic, session, openSession)) {
String msg = "[" + username + "] Disconnected!";
System.out.println("Server sending msg = " + msg);
openSession.sendSync(msg.getBytes());
}
}
}
use of io.micronaut.websocket.WebSocketSession in project micronaut-core by micronaut-projects.
the class NettyServerWebSocketBroadcaster method broadcast.
@Override
public <T> Publisher<T> broadcast(T message, MediaType mediaType, Predicate<WebSocketSession> filter) {
return Flux.create(emitter -> {
try {
WebSocketFrame frame = webSocketMessageEncoder.encodeMessage(message, mediaType);
webSocketSessionRepository.getChannelGroup().writeAndFlush(frame, ch -> {
Attribute<NettyWebSocketSession> attr = ch.attr(NettyWebSocketSession.WEB_SOCKET_SESSION_KEY);
NettyWebSocketSession s = attr.get();
return s != null && s.isOpen() && filter.test(s);
}).addListener(future -> {
if (!future.isSuccess()) {
Throwable cause = extractBroadcastFailure(future.cause());
if (cause != null) {
emitter.error(new WebSocketSessionException("Broadcast Failure: " + cause.getMessage(), cause));
return;
}
}
emitter.next(message);
emitter.complete();
});
} catch (Throwable e) {
emitter.error(new WebSocketSessionException("Broadcast Failure: " + e.getMessage(), e));
}
}, FluxSink.OverflowStrategy.BUFFER);
}
Aggregations