Infinum Android Talks #17 - A quest for WebSockets by Zeljko Plesac
Transcript of Infinum Android Talks #17 - A quest for WebSockets by Zeljko Plesac
SIMPLE TASK
Create a screen inside Android application, which will use
WebSocket for data retrieval.
Define WebSocket interface, implement it with custom
WebSocket implementation and provide this implementation
with Dagger 2 inside ApplicationComponent.
DECOUPLED ARCHITECTURE
public interface WebSocketsService { void connect(ConnectedCallback connectedCallback);
void setMessagesListener(MessagesCallback messagesCallback);
void disconnect();
void resetConnectionCallback();
boolean isConnected();
void sendMessage(@NonNull String message); }
@Module public class WebSocketModule {
@Provides @Singleton public WebSocketsService provideWebSocketService() { return new AndroidAsyncWebSocketService(); } }
@Singleton @Component(modules = WebSocketModule.class) public interface ApplicationComponent {
/** * Injects application dependencies. * * @param socketManApp instance of application */ void inject(SocketManApp socketManApp); }
Low level network protocol library.
Socket, HTTP client/server, WebSocket, and Socket.IO library
support.
Easy configuration.
3 195 stars.
ANDROIDASYNC
@Override public void connect(final ConnectedCallback connectedCallback) { if (isConnected()) { connectedCallback.onAlreadyConnected(); return; }
AsyncHttpClient.getDefaultInstance().websocket(BuildConfig.SOCKET_ENDPOINT, BuildConfig.SOCKET_PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() { @Override public void onCompleted(final Exception ex, WebSocket socket) { if (ex != null) { callbackExecutor.execute(new Runnable() { @Override public void run() { connectedCallback.onWebSocketException(ex, false);
} });
} else { webSocket = socket; webSocket.setStringCallback(AndroidAsyncWebSocketService.this); webSocket.setClosedCallback(AndroidAsyncWebSocketService.this);
callbackExecutor.execute(new Runnable() { @Override public void run() { if (connectedCallback != null) { connectedCallback.onConnected(); } } }); } } }); }
@Override public void disconnect() { if (webSocket != null) { webSocket.close(); webSocket = null; } }
@Override public void onCompleted(Exception ex) { if (ex != null && connectedCallback != null) { callbackExecutor.execute(new Runnable() { @Override public void run() { connectedCallback.onDisconnected(); webSocket = null; } }); } }
@Override public void onStringAvailable(final String s) { if (messagesCallback != null) { callbackExecutor.execute(new Runnable() { @Override public void run() { messagesCallback.onMessageReceived(s); } }); } }
@Override public void sendMessage(@NonNull String message) { if (webSocket != null && webSocket.isOpen()) { webSocket.send(message); } else if (messagesCallback != null) { messagesCallback.onSocketClosed(); } }
Big library - 512 KB, 3 425 methods
All callbacks are called on background thread
Multiple parallel connections to same endpoint
PROBLEMS
Buggy libraries.
Outdated forums, posts, Stackoverflow threads
Horrible Socket client/server applications.
NOT A SIMPLE TASK
Socket client library, which showcases Web Socket
implementation and features.
Open sourced.
SOCKET MAN
Thank you!
Visit www.infinum.co or find us on social networks:
infinum.co infinumco infinumco infinum
@ZELJKOPLESAC [email protected]