implementation 'io.github.zhou-jun-jie:RxWebSocket:1.0.2'
return new OkHttpClient.Builder()
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS) // 连接超时: 自己定义常量
.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS) // 读取超时: 自己定义常量
.writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS) // 写超时: 自己定义常量
.pingInterval(PING_INTERVAL, TimeUnit.SECONDS) // ping的连接时间: 自己定义常量
.build();
rxWebSocket = new RxWebSocketConfig()
.isPrintLog(true)
.client(getOkHttpClient())
.reconnectInterval(RECONNECT_INTERVAL, TimeUnit.SECONDS)
.build();
public void registerWebSocketMsg(String url, long timeout, TimeUnit timeUnit, WebSocketSubscriber<?> webSocketSubscriber) {
rxWebSocket.get(url, timeout, timeUnit)
.subscribe(webSocketSubscriber);
}
WebSocketManager.getInstance().registerWebSocketMsg(url, new WebSocketSubscriber<String>() {
@Override
protected void onMessage(String str) {
}
});