Comments (5)
这个是依托于RxJava写的,内部的WebSocket是共享的,所以当 当前的Observable取消订阅的时候,就是WebSocket关闭的时候,所以回调的WebSocketSubscriber 中的onClose方法,当当前url对应的WebSocket没有Observable订阅者的时候,内部的WebSocket就关闭了。所以WebSocketSubscriber 中的onClose方法相当于当前是当前Observable取消订阅的回调。
from rxwebsocket.
其实你说的在取消订阅时回调 onClose 这种情况我也想过,但是在我自己写的一个测试例子里,调用 stopService 之后(在这里是已经取消订阅了)并没有执行 onClose 这个方法。
下面测试的例子,Activity 中只有两个按钮,一个是调用 serviceStart,一个是调用 serviceStop 方法
public class NotificationService extends Service {
public static void serviceStart(Context context) {
Intent intent = new Intent(context, NotificationService.class);
context.startService(intent);
}
public static void serviceStop(Context context) {
Intent intent = new Intent(context, NotificationService.class);
context.stopService(intent);
}
private static final String TAG = "NotificationService";
// 网上的服务器
private static final String URL_WEB_SOCKET = "ws://121.40.165.18:8800";
private WebSocketSubscriber mWebSocketSubscriber;
@Override
public void onCreate() {
super.onCreate();
Config config = new Config.Builder().setShowLog(true).setReconnectInterval(5, TimeUnit.SECONDS).build();
RxWebSocket.setConfig(config);
mWebSocketSubscriber = new WebSocketSubscriber() {
@Override
protected void onOpen(WebSocket webSocket) {
Log.d(TAG, "RxWebSocket opened");
}
@Override
protected void onMessage(String text) {
Log.d(TAG, "RxWebSocket message: " + text);
}
@Override
protected void onReconnect() {
Log.d(TAG, "RxWebSocket reconnect");
}
@Override
protected void onClose() {
Log.d(TAG, "RxWebSocket close");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "RxWebSocket error", e);
}
};
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
RxWebSocket.get(URL_WEB_SOCKET).subscribe(mWebSocketSubscriber);
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
mWebSocketSubscriber.dispose();
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
from rxwebsocket.
嗯,谢谢你,问题我找到了,源码里在注销的时候,我没有调用onComplete,所以在注销的时候,没有回调onClose,RxWebSocket内部的日志,已经显示关闭WebSocket了。日志如下:
D/RxWebSocket: ws://121.40.165.18:8800 --> onOpen
D/RxWebSocket: OnDispose
D/RxWebSocket: ws://121.40.165.18:8800 --> cancel
from rxwebsocket.
如果你要在WebSocket关闭的时候做事情,你暂时可以使用doOnDispose操作符这样做,可以实现同样效果(这是kotlin代码,你用Java写就行了):
RxWebSocket.get("ws://121.40.165.18:8800")
.doOnDispose { "相当于onClose回调" }
.subscribe(mWebSocketSubscriber)
from rxwebsocket.
谢谢
from rxwebsocket.
Related Issues (20)
- 服务端的源码能否分享一下? HOT 1
- 不行啊,虽然是可以新开dispose,但是我看了本质还是同一个连接啊 HOT 4
- 断网重连方法回调的问题 HOT 1
- SocketException 后一直无法重连 HOT 2
- disposable.dispose()方法不回调onclose吗 HOT 3
- onClosing方法收到一个1011错误码后不重连 HOT 3
- 急!!!!长时间熄屏或者其他情况无消息 HOT 2
- timeout 连接 开销有点大 HOT 1
- 心跳检测 HOT 2
- 你好!发现发送大文件会失败,发送小点的图片可以,怎样可以监听发送文件的进度? HOT 1
- 服务器发布,应用会崩溃 HOT 1
- 我想一个url 多个连接咋办 HOT 3
- onReconnect()重连成功后,不会停止重连?
- setCancellable 会立即执行 HOT 7
- WebSocket连接过程中,切换wifi,重连之后再dispose无法断开WebSocket HOT 1
- 接锅回调存在多个 HOT 1
- fragment 调用注销后没有成功注销 HOT 5
- 没有失败回调 HOT 4
- RxWebsocket引起java.io.EOFException
- 开启混淆之后RxWebSocket无法使用
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxwebsocket.