When client make the first connect, I save the responseStreamObserver and link it with the client's user ID,so the server can use the StreamObserver when needs to push message to client.
But now I can not find a way to identify the client when connection is lost and "CANNCELED" is catched in onError on server side.
public StreamObserver<Message> chat(StreamObserver<Message> responseObserver){
final UserData data = new UserData();
final ServerCallStreamObserver<Message> rsob = (ServerCallStreamObserver<Message>) responseObserver;
rsob.setOnCancelHandler(new Runnable() {
@Override
public void run() {
System.out.println("OnCancelHandler:" + data.name);//
}
});
return new StreamObserver<Message>() {
@Override
public void onNext(Message value) {
data.name = value.getFrom();
}
UserData is a POJO.
chat() will be call when client begin RPC, so just create UserData there and use it in onError and other callbacks.