29
A quest for WebSockets ŽELJKO PLESAC

Infinum Android Talks #17 - A quest for WebSockets by Zeljko Plesac

  • Upload
    infinum

  • View
    134

  • Download
    1

Embed Size (px)

Citation preview

A quest for WebSockets

ŽELJKO PLESAC

Every story has it’s beginning.

SIMPLE TASK

Create a screen inside Android application, which will use

WebSocket for data retrieval.

DIFFERENT SOLUTIONS

Java WebSocket

OkHttp WebSocket

AutoBahn

IVAN KUŠTAndroid Team Leader, Infinum

Let’s define custom interface for communication between web socket and our application.

- IVAN KUŠT

ALL OF THEM HAVE MISSING FEATURES.

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); }

WE DECIDED TO USE AUTOBAHN.

664 stars on Github.

Easy configuration.

32 issues.

AUTOBAHN

WEB SOCKET

THERE IS A THING CALLED WEB SOCKET SECURE.

AUTOBAHN DOESN’T SUPPORT IT.

ANDROID ASYNC TO THE RESCUE.

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]