我使用tyrus
成功连接到本地websocket服务器,但onMessage
方法未被调用。我将Fiddler设置为中间的代理,我看到服务器响应两条消息,但是,它们不会在我的代码中打印出来。我或多或少地改编了样本代码:
打印出onOpen
消息
public static void createAndConnect(String channel) {
CountDownLatch messageLatch;
try {
messageLatch = new CountDownLatch(1);
final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
ClientManager client = ClientManager.createClient();
client.connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
System.out.println("On Open and is Open " + session.isOpen());
session.addMessageHandler((Whole<String>) message -> {
System.out.println("Received message: " + message);
messageLatch.countDown();
});
}
}, cec, new URI("ws://192.168.1.248/socket.io/1/websocket/" + channel));
messageLatch.await(5, TimeUnit.SECONDS); //I also tried increasing timeout to 30sec, doesn't help
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
这是一个已知的问题 - 如果你将lambda重写为匿名类或使用Session#addMessageHandler(Class, MessageHandler)(你可以在这里使用lambdas),它会有效。