websocket
<script>
import Stomp from 'stompjs';
mounted() {
this.login = this.$store.state.account.id //从vuex state中获取登录id
this.initConnect()
},
methods:{
//建立websocket连接
initConnect() {
this.socket = new WebSocket('ws://192.168.200.219:20173/ricky-websocket?userId=' + this.login + '&system=mooc');
this.stompClient = Stomp.over(this.socket);
this.stompClient.connect({}, (frame) => {
this.stompClient.subscribe('/ricky/topic/mooc', (greeting) => {
console.log(JSON.parse(greeting.body).content)
//获取后台推送前台的消息条数
userService.unReadNum({
userId: this.login
}).then(data => {
if (data) {
this.messageNum = data
}
})
//发送消息
// this.stompClient.send("/app/msg/mooc", {}, Number(JSON.parse(JSON.parse(greeting.body).content).id));
});
});
},
}
</script>