【发布时间】:2021-12-31 13:43:19
【问题描述】:
我正在尝试使用 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;
}
在我得到的应用程序开始时:
2021-12-31 14:22:39.442 INFO 1952 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
我以为我已经配置好了所有东西,所以我决定尝试使用邮递员进行连接,但没有成功:/ 我执行了以下端点:
ws://localhost/app/chat
ws://localhost:8080/app/chat
ws://localhost/chat
ws://localhost:8080/chat
它们都不起作用:/
我正在使用 Postman Websockets beta 来执行请求。
我是否遗漏了配置中的某些内容,还是我使用 postaman 错误?
【问题讨论】:
-
ws://localhost:8080/聊天作品
标签: java spring-boot spring-websocket