//if you want to use your okhttpClient
OkHttpClient yourClient = new OkHttpClient();
RxWebSocketUtil.getInstance().setClient(yourClient);
// show log,default false
RxWebSocketUtil.getInstance().setShowLog(true);
WSS support
//wss support
RxWebSocketUtil.getInstance().setSSLSocketFactory(yourSSlSocketFactory,yourX509TrustManager);
RxWebSocketUtil.getInstance().getWebSocket("wss://...");
//or
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(yourSSlSocketFactory, yourX509TrustManager)
//other config...
.build();
RxWebSocketUtil.getInstance().setClient(client);
open WebSocket
mDisposable = RxWebSocketUtil.getInstance().getWebSocketInfo(url)
//bind on life
.takeUntil(bindOndestroy())
.subscribe(new Consumer<WebSocketInfo>() {
@Override
public void accept(WebSocketInfo webSocketInfo) throws Exception {
mWebSocket = webSocketInfo.getWebSocket();
if (webSocketInfo.isOnOpen()) {
Log.d("MainActivity", " on WebSocket open");
} else {
String string = webSocketInfo.getString();
if (string != null) {
Log.d("MainActivity", string);
textview.setText(Html.fromHtml(string));
}
ByteString byteString = webSocketInfo.getByteString();
if (byteString != null) {
Log.d("MainActivity", "webSocketInfo.getByteString():" + byteString);
}
}
}
});
mWebSocket.send("hello word");
// use WebSocketSubscriber
RxWebSocketUtil.getInstance().getWebSocketInfo(url)
.subscribe(new WebSocketSubscriber() {
@Override
public void onOpen(@NonNull WebSocket webSocket) {
}
@Override
public void onMessage(@NonNull String text) {
}
@Override
public void onMessage(@NonNull ByteString bytes) {
}
@Override
public void onSubscribe(@NonNull Disposable d) {
}
});
// use WebSocketConsumer
RxWebSocketUtil.getInstance().getWebSocketInfo("ws://10.7.5.88:8089")
.compose(RxLifecycle.with(this).<WebSocketInfo>bindOnDestroy())
.subscribe(new WebSocketConsumer() {
@Override
public void onOpen(WebSocket webSocket) {
}
@Override
public void onMessage(String text) {
}
@Override
public void onMessage(ByteString bytes) {
}
});
//get StringMsg
RxWebSocketUtil.getInstance().getWebSocketString(url)
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
}
});
// get ByteString
RxWebSocketUtil.getInstance().getWebSocketByteString(url)
.subscribe(new Consumer<ByteString>() {
@Override
public void accept(ByteString byteString) throws Exception {
}
});
//get WebSocket
RxWebSocketUtil.getInstance().getWebSocket(url)
.subscribe(new Consumer<WebSocket>() {
@Override
public void accept(WebSocket webSocket) throws Exception {
}
});
//with timeout
RxWebSocketUtil.getInstance().getWebSocketInfo(url, 10, TimeUnit.SECONDS)
.subscribe(new Consumer<WebSocketInfo>() {
@Override
public void accept(WebSocketInfo webSocketInfo) throws Exception {
}
});
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.