我正在使用OkHttp通过Android上的套接字发送和接收消息。
创建套接字连接 -
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 变量来跟踪“如果套接字已连接或已断开连接”。
有没有更好的方法来检查插座是连接还是断开连接?
答案 0 :(得分:3)
这是使用Rx解决的Tinder Scarlet方法
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))
}
答案 1 :(得分:0)
尝试这个答案
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);
}
我希望有帮助