Androidのソケットを介してメッセージを送受信するためにOkHttpを使用しています。
ソケット接続の作成-
SocketListener listener = new SocketListener();
mOkHttpClient = new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder()
.url(socketURL)
.build();
mOkHttpClient.newWebSocket(request, listener);
そして、これはすべてのWebソケットイベントをリッスンするために使用されるSocketListenerクラスです-
public final class SocketListener extends WebSocketListener {
@Override
public void onOpen(WebSocket webSocket, Response response) {
// doing something
isSocketConnected = true;
}
@Override
public void onMessage(WebSocket webSocket, String text) {
// doing something
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
// doing something
isSocketConnected = true;
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
isSocketConnected = false;
}
}
ソケット接続が切断されることがあります。ご覧のとおり、SocketListenerクラスでisSocketConnected変数を使用して、「ソケットが接続されているか切断されているか」を追跡しています。
ソケットが接続されているか切断されているかを確認するためのより良い方法はありますか?
これは、Rxを使用して解決するTheTinderScarletメソッドです。
internal class OkHttpWebSocketEventObserver : WebSocketListener() {
private val processor = PublishProcessor.create<WebSocket.Event>().toSerialized()
fun observe(): Flowable<WebSocket.Event> = processor
fun terminate() = processor.onComplete()
override fun onOpen(webSocket: okhttp3.WebSocket, response: Response) =
processor.onNext(WebSocket.Event.OnConnectionOpened(webSocket))
override fun onMessage(webSocket: okhttp3.WebSocket, bytes: ByteString) =
processor.onNext(WebSocket.Event.OnMessageReceived(Message.Bytes(bytes.toByteArray())))
override fun onMessage(webSocket: okhttp3.WebSocket, text: String) =
processor.onNext(WebSocket.Event.OnMessageReceived(Message.Text(text)))
override fun onClosing(webSocket: okhttp3.WebSocket, code: Int, reason: String) =
processor.onNext(WebSocket.Event.OnConnectionClosing(ShutdownReason(code, reason)))
override fun onClosed(webSocket: okhttp3.WebSocket, code: Int, reason: String) =
processor.onNext(WebSocket.Event.OnConnectionClosed(ShutdownReason(code, reason)))
override fun onFailure(webSocket: okhttp3.WebSocket, t: Throwable, response: Response?) =
processor.onNext(WebSocket.Event.OnConnectionFailed(t))
}
この答えを試してください
private final class EchoWebSocketListener extends WebSocketListener {
@Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
isSocketConnected = true;
}
@Override
public void onMessage(WebSocket webSocket, String text) {
//you code
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
isSocketConnected = false;
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, okhttp3.Response response) {
isSocketConnected = false;
createSocket(); // reopen socket
}
}
そしてこの接続コード
if (isSocketConnected == false) {
SocketListener listener = new SocketListener();
mOkHttpClient = new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder()
.url(socketURL)
.build();
mOkHttpClient.newWebSocket(request, listener);
}
この助けを願っています