【发布时间】:2018-08-29 03:59:08
【问题描述】:
我在服务器端和凉亭客户端使用 Spring 4.3.6.RELEASE 版本“stomp-websocket”:“2.0”和“sockjs-client”:“1.1.4”
这是服务器端的所有相关代码 -
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer
{
@Autowired
private SimpMessagingTemplate brokerMessagingTemplate;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry)
{
// for local testing, setting allowed origins to * - Should be removed later
registry.addEndpoint("/websoc").setAllowedOrigins("*").withSockJS().setSupressCors(true);
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry)
{
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic", "/queue");
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration)
{
registration.taskExecutor()
.corePoolSize(10)
.maxPoolSize(20);
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration)
{
registration.setSendTimeLimit( 15 * 1000)
.setSendBufferSizeLimit( 512 * 1024)
.setMessageSizeLimit( 128 * 1024);
}
@PostConstruct
public void fetchNotifications()
{
NotificationsController.setMesgingTmpt(brokerMessagingTemplate);
}
}
@Controller
public class NotificationsController
{
private static SimpMessagingTemplate brokerMessagingTemplate;
public static void setMesgingTmpt(SimpMessagingTemplate tmp)
{
brokerMessagingTemplate = tmp;
}
// @SendTo("/topic/userNotifications")
public void testWSMessage(JUserNotification un)
{
LOGGER.debug("testWSMessage called");
un.setAccountId("1");
un.setUserId("1");
try
{
LOGGER.debug("sending notif: {}", JSONUtils.stringify(un));
brokerMessagingTemplate.convertAndSend("/topic/userNotifications", un);
} catch (final Exception e)
{
LOGGER.debug("exception while sending msg to client: {}", e);
}
// return un;
}
}
@Component
public class CustomApplicationListener implements ApplicationListener<ApplicationEvent>
{
@Override
public void onApplicationEvent(ApplicationEvent event)
{
if (event instanceof SessionSubscribeEvent)
{
LOGGER.debug("subscribe event received. Some params - ");
final SessionSubscribeEvent se = (SessionSubscribeEvent) (event);
final StompHeaderAccessor headers = StompHeaderAccessor.wrap(se.getMessage());
LOGGER.debug("sessionId: {}", headers.getSessionId());
LOGGER.debug("sessionAttributes: {}", headers.getSessionAttributes());
LOGGER.debug("ack: {}", headers.getAck());
LOGGER.debug("command: {}", headers.getCommand());
LOGGER.debug("destination: {}", headers.getDestination());
LOGGER.debug("subscriptionId: {}", headers.getSubscriptionId());
LOGGER.debug("user: {}", headers.getUser());
// test code
final NotificationsController notifs = new NotificationsController();
final JUserNotification usrNotif = new JUserNotification();
notifs.testWSMessage(usrNotif);
}
}
}
在客户端,这是代码-
this.set("webSockURL", this.get("serverInfo").getWebsocketEndPoint());
this.set("sock", new SockJS(this.get("webSockURL")));
this.set("stompClient", Client.over(this.get("sock")));
var stompcli = this.get("stompClient");
var subscribeCallBack = function(message) {
console.log("inside subscribeCallBack");
this._processNotifications(message)
};
var connectCallBack = function() {
console.log("inside connectCallBack");
stompcli.subscribe("topic/userNotifications", subscribeCallBack);
};
stompcli.connect({}, {}, connectCallBack);
Websocket 连接已建立,我收到了心跳消息,但没有收到我正在发送的通知。我什至尝试过使用@SendTo 注释,但它不起作用。
当我使用 webjars stomp 和 sockjs 尝试相同的服务器代码并在本地运行它时,我会收到消息。这让我认为是客户端库有问题,但我不确定。
服务器在 nginx 后面的 docker 容器上作为反向代理运行。我已将 ELB 配置为允许 websockets 并启用 proxy_protocol。
由于连接本身建立成功,我不怀疑它的基础设施部分。 任何想法将不胜感激。
【问题讨论】:
-
stompcli.connect({}, connectCallBack); -
我使用的客户端库需要 2 个参数,我都试过了,只有 stompcli.connect({}, {}, connectCallBack);作品
-
哦,我明白了。我认为您正在使用这个jmesnil.net/stomp-websocket/doc,所以我的评论具有误导性,对不起。
-
还要检查主题名称,这里没有前导斜杠 - “topic/userNotifications”。也许应该是“/topic/userNotifications”?我不熟悉你的图书馆,它是否在订阅时添加了前导斜杠?
-
哇!那成功了。谢谢。该文档具有误导性 - 它在某些方法中指定了前导 / 而在其他方法中没有。如果您可以将此添加为答案,我将很乐意接受。
标签: bower stomp spring-websocket sockjs