protected void handleMessage(T message) { String channel = getChannel(message); handleChannelMessage(channel, message); }
protected String getChannel(T message) { String channel; try { channel = getChannelNameFromMessage(message); } catch (IOException e) { LOG.error("Cannot parse channel from message: {}", message); return ""; } return channel; }
public void resubscribeChannels() { for (String channelId : channels.keySet()) { try { Subscription subscription = channels.get(channelId); sendMessage(getSubscribeMessage(subscription.channelName, subscription.args)); } catch (IOException e) { LOG.error("Failed to reconnect channel: {}", channelId); } } }
protected void handleError(T message, Throwable t) { String channel = getChannel(message); handleChannelError(channel, t); }
public Observable<T> subscribeChannel(String channelName, Object... args) { final String channelId = getSubscriptionUniqueId(channelName, args); LOG.info("Subscribing to channel {}", channelId); return Observable.<T>create(e -> { if (webSocketChannel == null || !webSocketChannel.isOpen()) { e.onError(new NotConnectedException()); } if (!channels.containsKey(channelId)) { Subscription newSubscription = new Subscription(e, channelName, args); channels.put(channelId, newSubscription); try { sendMessage(getSubscribeMessage(channelName, args)); } catch (IOException throwable) { e.onError(throwable); } } }).doOnDispose(() -> { if (channels.containsKey(channelId)) { sendMessage(getUnsubscribeMessage(channelId)); channels.remove(channelId); } }).share(); }
final WebSocketClientHandler handler = getWebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker( uri, WebSocketVersion.V13, null, true, getCustomHeaders(), maxFramePayloadLength), this::messageHandler); completable.onComplete(); } else { handleError(completable, f.cause()); handleError(completable, future.cause()); handleError(completable, throwable); connectedSuccessfully = true; LOG.warn("Resubscribing channels"); resubscribeChannels();