【问题标题】:No enum value sending message using websockets没有枚举值使用 websockets 发送消息
【发布时间】:2022-11-20 17:59:36
【问题描述】:

我正在尝试使用 websockets 创建一个简单的聊天应用程序。我遵循了本教程:https://www.baeldung.com/websockets-spring

这是我的配置:

@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("/chat");
         registry.addEndpoint("/chat").withSockJS();
    }
}
@Controller
public class WebSocketController {
    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public OutputMessage send(Message message) throws Exception {
        return new OutputMessage(message.getFrom(), message.getText(), Instant.now());
    }
}
@Builder
@Getter
public class Message {
    private final String from;
    private final String text;
}

@Builder
@Getter
public class OutputMessage {
    private final String from;
    private final String text;
    private final Instant date;
}

现在我正在尝试使用 Message 请求模型发送消息。

但最后我得到:

2022-01-02 11:17:30.438 ERROR 26412 --- [nio-8080-exec-7] o.s.w.s.m.StompSubProtocolHandler        : Failed to parse TextMessage payload=[{
    "fr..], byteCount=44, last=true] in session 49df8471-5fe9-d785-c4c9-5058a712bb9b. Sending STOMP ERROR to client.

java.lang.IllegalArgumentException: No enum constant org.springframework.messaging.simp.stomp.StompCommand.{
    at java.base/java.lang.Enum.valueOf(Enum.java:240) ~[na:na]
    at org.springframework.messaging.simp.stomp.StompCommand.valueOf(StompCommand.java:28) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.StompDecoder.decodeMessage(StompDecoder.java:148) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.StompDecoder.decode(StompDecoder.java:115) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.BufferingStompDecoder.decode(BufferingStompDecoder.java:114) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:252) ~[spring-websocket-5.3.14.jar:5.3.14]

我应该发送一些额外的标头来解析消息吗?我没主意了。

【问题讨论】:

    标签: java spring-boot websocket spring-websocket


    【解决方案1】:

    我按照相同的教程来理解 WebSockets,但遇到了完全相同的问题。我发现请求正文中的换行符导致了问题。您的请求正文必须是:

    {"from": "test", "text": "test"}
    

    进行此更改后,它对我有用。

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多