-
-
Notifications
You must be signed in to change notification settings - Fork 32
Rename interface to OkHttp3SocketClientFactory.java. #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
ParseLiveQuery/src/main/java/com/parse/OkHttp3SocketClientFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.parse; | ||
|
||
import android.util.Log; | ||
|
||
import java.net.URI; | ||
import java.util.Locale; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.WebSocket; | ||
import okhttp3.WebSocketListener; | ||
import okio.ByteString; | ||
|
||
/* package */ public class OkHttp3SocketClientFactory implements WebSocketClientFactory { | ||
|
||
OkHttpClient mClient; | ||
|
||
public OkHttp3SocketClientFactory(OkHttpClient client) { | ||
mClient = client; | ||
} | ||
|
||
public OkHttp3SocketClientFactory() { | ||
mClient = new OkHttpClient(); | ||
} | ||
|
||
@Override | ||
public WebSocketClient createInstance(WebSocketClient.WebSocketClientCallback webSocketClientCallback, URI hostUrl) { | ||
return new OkHttp3WebSocketClient(mClient, webSocketClientCallback, hostUrl); | ||
} | ||
|
||
class OkHttp3WebSocketClient implements WebSocketClient { | ||
|
||
private static final String LOG_TAG = "OkHttpWebSocketClient"; | ||
|
||
private final WebSocketClientCallback webSocketClientCallback; | ||
private WebSocket webSocket; | ||
private State state = State.NONE; | ||
private final OkHttpClient client; | ||
private final String url; | ||
private final int STATUS_CODE = 200; | ||
private final String CLOSING_MSG = "User invoked close"; | ||
|
||
private final WebSocketListener handler = new WebSocketListener() { | ||
@Override | ||
public void onOpen(WebSocket webSocket, Response response) { | ||
setState(State.CONNECTED); | ||
webSocketClientCallback.onOpen(); | ||
} | ||
|
||
@Override | ||
public void onMessage(WebSocket webSocket, String text) { | ||
webSocketClientCallback.onMessage(text); | ||
} | ||
|
||
@Override | ||
public void onMessage(WebSocket webSocket, ByteString bytes) { | ||
Log.w(LOG_TAG, String.format(Locale.US, | ||
"Socket got into inconsistent state and received %s instead.", | ||
bytes.toString())); | ||
} | ||
|
||
@Override | ||
public void onClosed(WebSocket webSocket, int code, String reason) { | ||
setState(State.DISCONNECTED); | ||
webSocketClientCallback.onClose(); | ||
} | ||
|
||
@Override | ||
public void onFailure(okhttp3.WebSocket webSocket, Throwable t, Response response) { | ||
webSocketClientCallback.onError(t); | ||
} | ||
}; | ||
|
||
private OkHttp3WebSocketClient(OkHttpClient okHttpClient, | ||
WebSocketClientCallback webSocketClientCallback, URI hostUrl) { | ||
client = okHttpClient; | ||
this.webSocketClientCallback = webSocketClientCallback; | ||
url = hostUrl.toString(); | ||
} | ||
|
||
@Override | ||
public synchronized void open() { | ||
if (State.NONE == state) { | ||
// OkHttp3 connects as soon as the socket is created so do it here. | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.build(); | ||
|
||
webSocket = client.newWebSocket(request, handler); | ||
setState(State.CONNECTING); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
setState(State.DISCONNECTING); | ||
webSocket.close(STATUS_CODE, CLOSING_MSG); | ||
} | ||
|
||
@Override | ||
public void send(String message) { | ||
if (state == State.CONNECTED) { | ||
webSocket.send(message); | ||
} | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return state; | ||
} | ||
|
||
private synchronized void setState(State newState) { | ||
this.state = newState; | ||
this.webSocketClientCallback.stateChanged(); | ||
} | ||
} | ||
|
||
} |
110 changes: 0 additions & 110 deletions
110
ParseLiveQuery/src/main/java/com/parse/OkHttp3WebSocketClient.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
ParseLiveQuery/src/main/java/com/parse/TubeSockWebSocketClient.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a few more interface methods (so you don't need to specify the Uri thanks to changes to @hermanliang