我一直在使用Bidirectional Streaming的概念Grpc使用Async stub。
以下是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOnline = (Button) findViewById(R.id.btnOnline);
btnOffline = (Button) findViewById(R.id.btnOffline);
btnAcceptRide = (Button) findViewById(R.id.btnAcceptRide);
btnCancelRide = (Button) findViewById(R.id.btnCancelRide);
txtCode = (EditText) findViewById(R.id.txtCode);
txtReply = (TextView) findViewById(R.id.txtReply);
ClientConnState = 0;
btnOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new GrpcTask().execute();
}
});
private class GrpcTask extends AsyncTask<Void, Void, String> {
private String mHost;
private String mMessage;
private int mPort;
private ManagedChannel mChannel;
@Override
protected void onPreExecute() {
mHost = "localhost";
mPort = 8080;
mChannel = ManagedChannelBuilder.forAddress("192.168.0.102", 50049)
.usePlaintext(true)
.build();
blockingStub = bidirectionalserviceGrpc.newBlockingStub(mChannel);
asyncStub = bidirectionalserviceGrpc.newStub(mChannel);
}
@Override
protected String doInBackground(Void... nothing) {
try {
final CountDownLatch countDownLatch = new CountDownLatch(1);
requestStreamObserver = asyncStub.requestRide(new StreamObserver<Bidirectional.RideReply>() {
@Override
public void onNext(Bidirectional.RideReply value) {
if (countDownLatch.getCount() > 0) {
countDownLatch.countDown();
}
}
现在一切正常..我可以使用建立的流来ping/pong服务器/客户端。但是当我关闭服务器并从客户端发出流请求时,它会等待无穷大。我除了它抛出OnError()事件,但它没有。
有人可以帮我解决这个问题吗?
相关分类