Tyrus websocket 客户端 - 尽管连接成功但未调用 onMessage
Tryus websocket client - onMessage does not get called although connection is succesful
我已使用 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();
}
}
这是一个已知问题 - 如果您将 lambda 重写为匿名 class 或使用 Session#addMessageHandler(Class, MessageHandler)(您可以在此处使用 lambda),它将起作用。
我已使用 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();
}
}
这是一个已知问题 - 如果您将 lambda 重写为匿名 class 或使用 Session#addMessageHandler(Class, MessageHandler)(您可以在此处使用 lambda),它将起作用。