Java WebSocket:未触发 onMessage

Java WebSocket: onMessage is not triggered

我用这个code在java写过websocket客户端。这是一个安全套接字。与目的地的连接建立正常,但未调用 onMessage。

当我在 java 脚本中编写相同的内容时,它正在运行并且能够得到响应。

请检查我的代码并告知我在这里遗漏了什么。

我的代码:

@ClientEndpoint
public class MyClientEndpoint {
    @SuppressWarnings("unchecked")

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
            JSONObject obj = new JSONObject();
            obj.put("subscribe", URLEncoder.encode("xxxxxxx","UTF-8"));
            String msg = obj.toJSONString();
            System.out.println("Sending message to endpoint: " + msg);
            session.getBasicRemote().sendText(msg);
        } catch (Exception ex) {
            Logger.getLogger(MyClientEndpoint.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @OnMessage
    public void onMessage(String message) {
    System.out.println("Received message in client: " + message);
        Client.messageLatch.countDown();
    }



public class Client {

    final static CountDownLatch messageLatch = new CountDownLatch(1);

    public static void main(String[] args) {
        try {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            String uri = "wss://api.....";
            System.out.println("Connecting to:: " + uri);
            container.connectToServer(MyClientEndpoint.class, URI.create(uri));
            messageLatch.await(10, TimeUnit.SECONDS);            
        } catch (DeploymentException | InterruptedException | IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

我可以通过在

中传递凭据来连接到安全的 websocket
ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator() {
             public void beforeRequest(Map<String, List<String>> headers) {
          String credentials = "username:password";
          headers.put("Authorization", Arrays.asList("Basic " + new BASE64Encoder().encode(credentials.getBytes())));
                 System.out.println("Header set successfully");
             }
         };

         ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create()
                 .configurator(configurator)
                 .build();

我在下面记录了步骤 link。

https://bigdatatechbytes.blogspot.in/2017/10/java-client-for-secure-web-socket.html