栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Spring WebSocket向自定义用户发送自定义消息?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Spring WebSocket向自定义用户发送自定义消息?

首先,我的问题是,当您成功将Websocket与stomp集成时,为什么要尝试将HTTP请求发送到rest控制器?如果我正确地理解了您的用例,那么我可以想到的atm应该有三种解决方案。

解决方案1(套接字会话↔产品ID)

您可以通过打开的websocket连接将请求直接从客户端发送到服务器。然后,Spring可以确定哪个Websocket会话进行了调用,然后可以实现您的业务逻辑。您需要激活另一个称为“
/
queue”的代理,并为预定用于广播的订阅指定用户目标的前缀。在客户端,您还必须更改订阅路径。最后,您必须创建一个用@Controller注释的类,该类包含您的消息映射以从连接的客户端接收消息。

服务器配置

@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {    @Override    public void registerStompEndpoints(StompEndpointRegistry registry) {        registry.addEndpoint("/websocket").withSockJS();    }    @Override    public void configureMessageBroker(MessageBrokerRegistry registry) {        registry.enableSimpleBroker("/queue", "/product");  // <- added "/queue"        registry.setApplicationDestinationPrefixes("/app");        registry.setUserDestinationPrefix("/user");    }}

服务器控制器

@Controllerpublic class WebSocketContoller{    @Autowired    private SimpMessagingTemplate simpMessagingTemplate;    @MessageMapping("/product/register")    public void register(@Payload Long productId, @Header("simpSessionId") String sessionId) {        // register current websocket session with product id and         // then with convertAndSendToUser send changes to current user.        // Example of how to send a message to the user using the sessionId        String response = "This could also be one of your product objects of type Product";        SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);        headerAccessor.setSessionId(sessionId);        headerAccessor.setLeaveMutable(true);        messagingTemplate.convertAndSendToUser(sessionId,"/queue/product/changes", response, headerAccessor.getMessageHeaders());    }}

客户订阅变更

stompClient.subscribe('/user/queue/product/changes', function (scoredata) {    // We received product changes});

解决方案2(主要产品↔)

但是,如果您确实要考虑使用rest控制器来开始注册您的进程,或者如果它不满足您的要求,则应查看下面的链接。Spring还能够通过公开的SimpUserRegistry
bean跟踪活动的Websocket会话及其用户。但是,您将需要根据应用程序的安全性为客户端输入通道配置自定义ChannelInterceptor适配器,以确定用户。


解决方案3(产品ID主题)

您还可以订阅特定的产品ID主题,因此甚至不需要知道要通知特定产品更改的用户。

客户订阅变更

//e.g if you want to be notified about changes for products with id 5 stompClient.subscribe('/product/changes/5', function (scoredata) {    // We received product changes});

服务器服务示例

@Servicepublic class WebSocketProductService{    @Autowired    private SimpMessagingTemplate simpMessagingTemplate;    // This would be the method which should inform your clients about specific product         // changes, instead of the String parameters a Product object should be used instead,     // you have to call this method yourself on product changes or schedule it or sth.    public void sendProductChange(String product, String productId) {        this.simpMessagingTemplate.convertAndSend("/product/changes/"+productId, product);    }}

服务器控制器

如果要管理产品ID订阅列表,则需要。如解决方案1中所述,您需要一个用@Controller注释的类,其中包含一个用@SubscribeMapping注释的方法。如果客户端尝试订阅指定的路径,则调用此方法。

@Controllerpublic class WebSocketContoller{    @SubscribeMapping("/product/changes/{productId}")    public void productIdSubscription(@DestinationVariable Long productId) {        //Manage your product id subscription list e.g.    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号