久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Spring:向 websocket 客戶端發送消息

Spring: send message to websocket clients(Spring:向 websocket 客戶端發送消息)
本文介紹了Spring:向 websocket 客戶端發送消息的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在構建一個使用 Spring Boot、RabbitMQ 和 WebSocket 作為 POC 的網絡聊天,但我被困在最后一點:WebSockets
我希望我的 ws 客戶端連接到特定端點,例如 /room/{id} 并且當新消息到達時,我希望服務器將響應發送給客戶端,但我搜索了類似的東西并沒有找到.

I'm building a webchat with Spring Boot, RabbitMQ and WebSocket as POC, but I'm stucked a the last point: WebSockets
I want my ws clients to connect to a specific endpoint, like /room/{id} and when a new message arrives, I want the server to send the response to clients, but I searched for something similar and didn't found.

目前,當消息到達時,我使用RabbitMQ對其進行處理,例如

Currently, when the message arrives, I process it with RabbitMQ, like

container.setMessageListener(new MessageListenerAdapter(){
            @Override
            public void onMessage(org.springframework.amqp.core.Message message, Channel channel) throws Exception {
                log.info(message);
                log.info("Got: "+ new String(message.getBody()));
            }
        });

我想要的是,而不是記錄它,我想將它發送給客戶端,例如:websocketManager.sendMessage(new String(message.getBody()))

what I would like is, instead log it , I want to send it to the client, for example: websocketManager.sendMessage(new String(message.getBody()))

推薦答案

好的,我想我明白了,給需要的人,這里是答案:

Ok, I think I got it, for everyone who needs it, here is the answer:

首先,需要在 pom.xml 中添加 WS 依賴

first, you need to add WS dependencies to the pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
</dependency>

創建一個 WS 端點

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // the endpoint for websocket connections
        registry.addEndpoint("/stomp").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/");

        // use the /app prefix for others
        config.setApplicationDestinationPrefixes("/app");
    }

}

注意:我使用的是 STOMP,所以客戶端應該像這樣連接

Note: I'm using STOMP, so the clients should connect like this

<script type="text/javascript">
    $(document).ready(function() {
        var messageList = $("#messages");
        // defined a connection to a new socket endpoint
        var socket = new SockJS('/stomp');
        var stompClient = Stomp.over(socket);
        stompClient.connect({ }, function(frame) {
            // subscribe to the /topic/message endpoint
            stompClient.subscribe("/room.2", function(data) {
                var message = data.body;
                messageList.append("<li>" + message + "</li>");
            });

        });
    });
</script>

然后,您可以簡單地使用

Then, you can simply wire the ws messenger on your components with

@Autowired
private SimpMessagingTemplate webSocket;

并使用

webSocket.convertAndSend(channel, new String(message.getBody()));

這篇關于Spring:向 websocket 客戶端發送消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 7777在线 | 国产午夜精品一区二区三区嫩草 | 国产精品国产三级国产aⅴ无密码 | 欧美精品一区在线发布 | 伊人手机在线视频 | 97人人澡人人爽91综合色 | 久久精品久久精品久久精品 | 一级毛片成人免费看a | 伊人狠狠| 激情a| 中文字幕不卡在线88 | 狠狠艹 | 91在线一区 | 久久久久久久久国产成人免费 | 婷婷激情五月网 | 亚洲电影第1页 | 欧美一级片在线 | 久久精品亚洲精品国产欧美 | 不卡av电影在线播放 | 精品美女视频在线观看免费软件 | 久久精品亚洲精品国产欧美 | 亚洲成av片人久久久 | 中文字幕av亚洲精品一部二部 | 日本不卡视频 | 成人在线视频观看 | 午夜精品在线观看 | 91网站在线看 | 欧美一区二区免费在线 | 欧美一区二区大片 | 亚洲日本一区二区三区四区 | 国产日产欧产精品精品推荐蛮挑 | 精品亚洲视频在线 | 欧美成人激情 | 国产精品二区三区在线观看 | 天堂一区在线观看 | 免费在线视频a | 久久亚洲精品久久国产一区二区 | 日本三级电影在线观看视频 | 日日日色| 成人在线精品 | 久久久精品视频一区二区三区 |