1. Introduce SockJS dependencies
1. Vue introduces dependencies
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
2. Maven introduces dependencies
pom.xml
File add dependency
<dependency>
<groupId>org.webjars</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3</version>
</dependency>
- Introduce the js of the jar package in the html file
<script src="/webjars/sockjs-client/1.0.2/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/2.3.3/stomp.min.js"></script>
Note : Use maven to introduce the dependent project springboot
2. Create a connection, subscribe to the message
var socket = new SockJS('/wsdemo');
stompClient = Stomp.over(socket);
// 创建连接
stompClient.connect({}, function (frame) {
//订阅消息
stompClient.subscribe('/topic/message', function (data) {
initOnLine(data.body)
});
);
3. Get subscription news
//订阅消息
stompClient.subscribe('/topic/message', (msg) => {
// 输出返回的信息
console.log(msg);
// 输出返回的消息的文本内容
console.log(meg.body);
});