我在Spring框架中对STOMP有问题。我的代码与端点连接,并接收消息,但它仅调用getPeyloadType,但不调用handleFrame。我也不例外你能帮助我吗 ?下面是我的代码
我尝试将getPayloadType和StringMessageConvert的返回类型更改为ByteArray。
public WebSocketCl (String Uri, Logger log) {
this.url = new String (Uri);
webSocketClient = new StandardWebSocketClient();
stompClient = new WebSocketStompClient(webSocketClient);
stompClient.setMessageConverter(new StringMessageConverter());
sessionHandler = new MySessionHandler();
stompClient.connect(url, sessionHandler);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
和处理程序类:
public class MySessionHandler extends StompSessionHandlerAdapter {
private StompSession sessionCon;
private StompFrameHandler handler;
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
System.out.println("Connected");
this.sessionCon = session;
this.sessionCon.subscribe("/listen", handler = new StompFrameHandler() {
@Override
public Type getPayloadType(StompHeaders headers) {
System.out.println("type");
return String.class;
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
String json = new String((byte[]) payload);
System.out.println(json);
System.out.println("received");
}
});
this.sessionCon.send("/hal/sensor/3", "{}");
}
}