@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent idleStateEvent = (IdleStateEvent) evt; if (idleStateEvent.state() == IdleState.ALL_IDLE) { // close the connection if it is idle for too long if (clientSession != null && clientSession.isOpen()) { clientSession.close(CloseReason.NORMAL); } } } else { super.userEventTriggered(ctx, evt); } }
@Override public String toString() { return "WebSocket Session: " + getId(); }
@Override public Set<? extends RxWebSocketSession> getOpenSessions() { return webSocketSessionRepository.getChannelGroup().stream().flatMap((Function<Channel, Stream<RxWebSocketSession>>) ch -> { NettyRxWebSocketSession s = ch.attr(NettyRxWebSocketSession.WEB_SOCKET_SESSION_KEY).get(); if (s != null && s.isOpen()) { return Stream.of(s); } return Stream.empty(); }).collect(Collectors.toSet()); }
@Override public void close() { close(CloseReason.NORMAL); }
@Override public <T> CompletableFuture<T> sendAsync(T message, MediaType mediaType) { if (isOpen()) { if (message != null) { CompletableFuture<T> future = new CompletableFuture<>(); WebSocketFrame frame = messageEncoder.encodeMessage(message, mediaType); channel.writeAndFlush(frame).addListener(f -> { if (f.isSuccess()) { future.complete(message); } else { future.completeExceptionally(new WebSocketSessionException("Send Failure: " + f.cause().getMessage(), f.cause())); } }); return future; } else { return CompletableFuture.completedFuture(null); } } else { throw new WebSocketSessionException("Session closed"); } }
@Override public void close(CloseReason closeReason) { super.close(closeReason); webSocketSessionRepository.removeChannel(ctx.channel()); }
if (session.isOpen()) { session.close(CloseReason.INTERNAL_ERROR); if (session.isOpen()) { session.close(CloseReason.INTERNAL_ERROR); LOG.error("Error Opening WebSocket [" + webSocketBean + "]: " + error.getMessage(), error); if (session.isOpen()) { session.close(CloseReason.INTERNAL_ERROR); LOG.error("Error Opening WebSocket [" + webSocketBean + "]: " + e.getMessage(), e); if (session.isOpen()) { session.close(CloseReason.INTERNAL_ERROR);
@Override public void sendSync(Object message, MediaType mediaType) { if (isOpen()) { if (message != null) { try { WebSocketFrame frame = messageEncoder.encodeMessage(message, mediaType); channel.writeAndFlush(frame).sync().get(); } catch (InterruptedException e) { throw new WebSocketSessionException("Send interrupt: " + e.getMessage(), e); } catch (ExecutionException e) { throw new WebSocketSessionException("Send Failure: " + e.getMessage(), e); } } } else { throw new WebSocketSessionException("Session closed"); } }
emitter.onError(new WebSocketClientException("WebSocket @OnMessage method " + targetBean.getClass().getSimpleName() + "." + messageHandler.getExecutableMethod() + " should define exactly 1 message parameter, but found 2 possible candidates: " + unboundArguments)); } finally { if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR); if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR);
@Override public <T> void broadcastSync(T message, MediaType mediaType, Predicate<WebSocketSession> filter) { WebSocketFrame frame = webSocketMessageEncoder.encodeMessage(message, mediaType); try { webSocketSessionRepository.getChannelGroup().writeAndFlush(frame, ch -> { Attribute<NettyRxWebSocketSession> attr = ch.attr(NettyRxWebSocketSession.WEB_SOCKET_SESSION_KEY); NettyRxWebSocketSession s = attr.get(); return s != null && s.isOpen() && filter.test(s); }).sync(); } catch (InterruptedException e) { throw new WebSocketSessionException("Broadcast Interrupted"); } }
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent idleStateEvent = (IdleStateEvent) evt; if (idleStateEvent.state() == IdleState.ALL_IDLE) { // close the connection if it is idle for too long if (clientSession != null && clientSession.isOpen()) { clientSession.close(CloseReason.NORMAL); } } } else { super.userEventTriggered(ctx, evt); } }
if (getSession().isOpen()) { if (LOG.isDebugEnabled()) { LOG.debug("Closing WebSocket session {} with reason {}", getSession(), cr);
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent idleStateEvent = (IdleStateEvent) evt; if (idleStateEvent.state() == IdleState.ALL_IDLE) { // close the connection if it is idle for too long if (clientSession != null && clientSession.isOpen()) { clientSession.close(CloseReason.NORMAL); } } } else { super.userEventTriggered(ctx, evt); } }
@Override public <T> Flowable<T> send(T message, MediaType mediaType) { if (message == null) { return Flowable.empty(); } return Flowable.create(emitter -> { if (!isOpen()) { emitter.onError(new WebSocketSessionException("Session closed")); } else { WebSocketFrame frame = messageEncoder.encodeMessage(message, mediaType); ChannelFuture channelFuture = channel.writeAndFlush(frame); channelFuture.addListener(future -> { if (future.isSuccess()) { emitter.onNext(message); emitter.onComplete(); } else { emitter.onError(new WebSocketSessionException("Send Failure: " + future.cause().getMessage(), future.cause())); } }); } }, BackpressureStrategy.ERROR); }
emitter.onError(new WebSocketClientException("WebSocket @OnMessage method " + targetBean.getClass().getSimpleName() + "." + messageHandler.getExecutableMethod() + " should define exactly 1 message parameter, but found 2 possible candidates: " + unboundArguments)); } finally { if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR); if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR);
@Override public <T> Flowable<T> broadcast(T message, MediaType mediaType, Predicate<WebSocketSession> filter) { return Flowable.create(emitter -> { try { WebSocketFrame frame = webSocketMessageEncoder.encodeMessage(message, mediaType); webSocketSessionRepository.getChannelGroup().writeAndFlush(frame, ch -> { Attribute<NettyRxWebSocketSession> attr = ch.attr(NettyRxWebSocketSession.WEB_SOCKET_SESSION_KEY); NettyRxWebSocketSession s = attr.get(); return s != null && s.isOpen() && filter.test(s); }).addListener(future -> { if (future.isSuccess()) { emitter.onNext(message); emitter.onComplete(); } else { Throwable cause = future.cause(); emitter.onError(new WebSocketSessionException("Broadcast Failure: " + cause.getMessage(), cause)); } }); } catch (Throwable e) { emitter.onError(new WebSocketSessionException("Broadcast Failure: " + e.getMessage(), e)); } }, BackpressureStrategy.BUFFER); } }
emitter.onError(new WebSocketClientException("WebSocket @OnMessage method " + targetBean.getClass().getSimpleName() + "." + messageHandler.getExecutableMethod() + " should define exactly 1 message parameter, but found 2 possible candidates: " + unboundArguments)); } finally { if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR); if (getSession().isOpen()) { getSession().close(CloseReason.INTERNAL_ERROR);