【发布时间】:2018-06-18 21:41:31
【问题描述】:
我正在尝试将 sockjs + stomp 连接到我的 spring boot websockets。这是我的配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {
private final String MESSAGE_BROKER_PREFIX = "/topic";
private final String WEBSOCKET_PREFIX = "/sockjs-node";
private final String REQUEST_PREFIX = "/";
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint(WEBSOCKET_PREFIX)
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker(MESSAGE_BROKER_PREFIX);
config.setApplicationDestinationPrefixes(REQUEST_PREFIX);
}
}
还有我的端点定义:
@Controller
public class Foo {
@SubscribeMapping("/{pipelineId}/{topic}")
private void subscribe(
HttpSession session,
@PathVariable String pipelineId,
@PathVariable String topic
) {
System.out.println(session.getId());
}
@EventListener
public void onApplicationEvent(SessionConnectEvent event) {
System.out.println(event.getSource());
}
@EventListener
public void onApplicationEvent(SessionDisconnectEvent event) {
System.out.println(event.getSessionId());
}
}
从 javascript 方面来说:
var ws = new SockJS('/sockjs-node');
var client = Stomp.over(ws);
var subscription = client.subscribe("/topic/foo/bar", () => {
console.log("asdas");
});
但是连接没有发生,并且没有任何方法被调用。在 javascript 控制台中我可以看到:
>>> SUBSCRIBE
id:sub-0
destination:/topic/lala
stomp.js:199 Uncaught TypeError: Cannot read property 'send' of undefined
at Client._transmit (webpack:///./node_modules/@stomp/stompjs/lib/stomp.js?:199:26)
at Client.subscribe (webpack:///./node_modules/@stomp/stompjs/lib/stomp.js?:468:12)
at Object.eval (webpack:///./src/index.js?:128:27)
我可以使用wscat --connect ws://localhost:8080/sockjs-node/902/phebsu4o/websocket 进行连接,但有趣的是,只有断开连接处理程序被调用,而连接处理程序没有。我在这里错过了什么?
【问题讨论】:
标签: spring-boot stomp sockjs