Skip to content

Commit

Permalink
Build Handler on new thread
Browse files Browse the repository at this point in the history
Backgrounds all websocket communication

Fixes #142
  • Loading branch information
Robert Collins committed Jul 22, 2014
1 parent b1787a7 commit b0a1d92
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import java.io.IOException;

Expand All @@ -19,12 +20,41 @@ class AsyncWebSocketProvider implements WebSocketManager.ConnectionProvider {
protected final String mAppId;
protected final String mSessionId;
protected final AsyncHttpClient mAsyncClient;
protected final Handler mHandler = new Handler();
protected Handler mHandler;

AsyncWebSocketProvider(String appId, String sessionId, AsyncHttpClient asyncClient) {
mAppId = appId;
mAsyncClient = asyncClient;
mSessionId = sessionId;

Runnable handlerInit = new Runnable() {

@Override
public void run() {
synchronized(this) {
Looper.prepare();

mHandler = new Handler();

notify();

}
Looper.loop();
}

};

Thread handlerThread = new Thread(handlerInit, "websocket-handler");

try {
synchronized(handlerInit) {
handlerThread.start();
handlerInit.wait();
}
} catch (InterruptedException e) {
// interrupted while initializing handler
}

}

@Override
Expand Down

0 comments on commit b0a1d92

Please sign in to comment.