postman Spring WebSocket控制器不返回任何内容

kupeojn6  于 5个月前  发布在  Postman
关注(0)|答案(1)|浏览(115)

我在Spring WebSocket上遇到了一个问题,我可以通过ws://localhost:port/ws成功连接到WebSocket,但我不能通过STOMP命令发送或接收任何东西(我的意思是我可以发送,但我不知道是否发生了什么)
WebSocketConfiguration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic"); 
        config.setApplicationDestinationPrefixes("/app"); 
    }
    
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws");
        registry.addEndpoint("/ws").withSockJS(); 
    }
}

字符串
主计长:

@Controller
public class SignalingController {
    @MessageMapping("/offer/sendOffer")
    @SendTo("/topic/offer")
    public String sendOffer(String offer){
        // Process the offer and send a response
        return "Received offer: " + offer;
    }

    @MessageMapping("/receive/receiveOffer")
    @SendTo("/topic/answer")
    public String sendAnswer(String answer) {
        // Process the answer and send a response
        return "Received answer: " + answer;
    }
}


我尝试使用Postman测试端点,并发送以下请求:

SEND
destination:/app/offer/sendOffer
content-type:text/plain

Test message!
^@


我看到它正在发送这个请求,但没有任何东西回来(ACK或其他东西):Postman Websocket sending

klh5stk1

klh5stk11#

TL;DR:Sping Boot 代码很好,但是测试它的方法是错误的。
好吧,经过很多次的尝试,我终于找到了一个方法。
首先,我试图从Android应用程序连接和发送请求,但在此之前,我试图用Postman WebSocket测试端点(我放弃了)。
其次,我尝试在Android上使用OkHTTP 3进行测试,但没有工作(无论出于什么原因,STOMP消息没有被Spring后端解析)。
最后,我遇到了运行完美的org.java_websocket,我刚刚从WebSocketClient.class发送STOMP请求作为文本,Spring终于开始将消息路由到控制器。

相关问题