I'm using WebSocket Rails-Android library by ararog.
I subscribe to a few channels in a loop based on data I receive from the server-side. I'm using the same WebSocketRailsDataCallback for all channels and the only varying thing is the channel name. My problem is that the callback's onDataAvailable(Object data)
data contains only the received JSON, which doesn't contain the channel name, and I failed to find a way to know on which channel the response was received.
I tried to create custom interface and class that extends WebSocketRailsDataCallback and WebSocketRailsChannel respectively with onDataAvailable(Object data, String channelName)
signature and overriden dispatch
method, but the class' variables are private, which means that I cannot have a constructor like the following and thus can't cast the WebSocketRailsChannel to the custom class:
public CustomWebSocketRailsChannel(WebSocketRailsChannel channel)
{
super(/*channel arguments*/);
}
I can get the fields using getDeclaredFields()
in methods, but it doesn't help me with casting (which I need because dispatcher.subscribe
method returns the base class).
The next solution I thought about was creating full custom classes for all the relevant classes (dispatcher, channel, dataCallback, etc.) instead of inheriting it, but it's a rough solution. Another one is contacting and library author. Any better and simpler idea will be appreciated.
Problem solved; I went too deep without noticing that I can do it by overriding the DataCallback only. I created the following class:
public abstract class CustomWebSocketRailsDataCallback implements WebSocketRailsDataCallback {
public CustomWebSocketRailsDataCallback(String channel)
{
mChannel = channel;
}
public String mChannel = null;
}
Created an instance of this class, saved the channel name in mChannel and accessed it in onDataAvailable.