【发布时间】:2019-07-06 00:52:44
【问题描述】:
我正在尝试将 Spring Boot Stomp Server 与多个 sockjs 客户端离线连接,但我收到警告
Websocket 在连接建立之前关闭
紧随其后
GET http://192.168.1.45:8080/socket/327/si5osugt/jsonp?c=jp.a3xdefl net::ERR_ABORTED 404(未找到)
我在后端使用带有spring-boot-starter-websocket 包的Spring Boot 2.1.2 版,在前端使用带有sockjs-client 1.3.0 版的Angular 6。前端和后端都在 8080 端口上运行
我在关闭互联网时遇到了一些错误。如果互联网关闭,iframe 会尝试访问https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.js。
我通过在后端配置 stomp 服务器来设置客户端库,方法是将 .setClientLibraryUrl 添加到离线可访问的绝对路径中。
registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS).setClientLibraryUrl("http://192.168.1.45/dist/sockjs.min.js");
在http://192.168.1.45/dist/sockjs.min.js 上获得 200 OK
春季启动:
WebSocketConfiguration(扩展 AbstractWebSocketMessageBrokerConfigurer)
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS().setClientLibraryUrl("http://192.168.1.45/dist/sockjs.min.js");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
MessageBrokerRegistry messageBrokerRegistry = registry.setApplicationDestinationPrefixes("/app");
messageBrokerRegistry.enableSimpleBroker( "/test", "/test2"
);
}
WebSocket控制器
private final SimpMessagingTemplate template;
@Autowired
WebSocketController(SimpMessagingTemplate template){
this.template=template;
}
@MessageMapping("/send/message")
public void onReceivedMessage( String destination , String message){
this.template.convertAndSend(destination , message);
}
public void convertAndSend(String url, Object o){
this.template.convertAndSend(url, o);
}
角度 6:
测试组件
ngAfterViewInit() {
let ws = new SockJS('http://192.168.1.45:8080/socket');
this.stompClient = Stomp.over(ws);
let that = this;
that.stompClient.subscribe("/test", (message) => {
if (message.body) {
console.log(message.body);
}
});
that.stompClient.subscribe("/test2", (message) => {
if (message.body) {
console.log(message.body);
}
});
}
我认为只需将 sockjs 客户端库添加到离线可达路径即可,但我收到警告
Websocket 在连接建立之前关闭
紧随其后
“GET http://192.168.1.45:8080/socket/327/si5osugt/jsonp?c=jp.a3xdefl net::ERR_ABORTED 404(未找到)”
图书馆在互联网连接下工作得非常好,但我需要它在在线和离线两种情况下工作。
【问题讨论】:
标签: spring-boot spring-websocket stomp sockjs