JAVA API Websocket-发送消息后立即获取响应

问题描述

我在服务器端使用JSR 356 Websocket(用于websocket的JAVA API),在Android应用端使用Okhttp websocket。 我可以在websockets上从服务器发送和接收消息。我需要编写一个函数,该函数会将数据发送到Android应用程序并等待Android应用程序的响应,然后作为函数的结果返回它。

从服务器端发送:

currentSession.getBasicRemote().sendText ("command will be here");

在服务器端接收是在websocket注释的方法

   @OnMessage
public String message(String message,Session session,@PathParam(value = "user_id") String userID) { 
   System.out.println ("WebSocket incoming msg for session with ID: "+session.getId() 
   + "\nReceived user ID: " + userID
   + "\nReceived msg operation: " + message
   );
  return "Thank you for msg client";

我的功能是:

public String transReceive(String command) {

   RemoteEndpoint.Basic other = currentSession.getBasicRemote();
   try {
        other.sendText ("command will be here");
    } catch (IOException e) {
        e.printstacktrace();
    }
//How wait for receive and get response here?
return response;
}

如何使用Java API for WebSocket实现此目的? 我在Link处发现了可能需要用Java脚本编写的内容。但是我不太了解。我知道我需要对客户端使用一些函数回调吗?这与JAVA相关吗?如果是的话,谁能给我一个解释?

我也在考虑使用简单的getter和setter方法-在使用@OnMessage注释的方法中设置响应,然后在transReceive函数获取响应。但是,如何等待直到设置了响应?

谢谢你们的贡献!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)