Vue+websocket+stompjs实现长连接(用于实时接收消息)

本文档展示了如何在Vue项目中使用Stompjs库来实现WebSocket长连接,通过`yarn add stompjs`安装依赖,然后在组件的`mounted`阶段建立连接。在`onConnected`回调中订阅指定的消息交换,当收到消息时更新通知计数。此外,还包含了断开连接的方法以及在组件销毁时解除监听的逻辑。
摘要由CSDN通过智能技术生成

参考

1、安装

yarn add stompjs

2、引入

import Stomp from "stompjs";

3、使用

<template>
	<!--通过点击消息图标来触发事件-->
	<img src="message.png" alt="" @click="openMessage" />
</template>

<script>
	import Stomp from "stompjs";

	export default {
		data() {
			return {
				client: undefined
			}
		},
		mounted() {
			this.connect() //页面挂载触发长连接
		},
		methods: {
			openMessage() {
				NoticeCount().then(res => {
					if (res.success) {
						this.num = res.data;
					}
				});
			},
			connect() {
				//VUE_APP_URL="wss://abcd.net/ws";   //后端提供
				const url = process.env.VUE_APP_URL;
				this.client = Stomp.client(url);
				const headers = {
					login: "admin",
					passcode: "admin",
					host: "/",
					"heart-beat": "0,0"
				};
				//client.connect(login, passcode, connectCallback, errorCallback, host);
				this.client.connect(headers, this.onConnected, this.onFailed);
			},
			onConnected() {
				const id = window.localStorage.id;
				const exchange = `/long/connection/${id}`;//后端提供
				//目的地(destination),回调函数(callback);还有一个可选的参数headers
				//client.subscribe(destination, successCallback, errorCallback);
				this.client.subscribe(exchange, this.responseCallback, this.onFailed);//订阅消息
			},
			onFailed() {
				setTimeout(() => {
					this.connect();
				}, 5000);
			},
			responseCallback() {
				NoticeCount().then(res => {
					if (res.success) {
						this.num = res.data;
					}
				});
			},
			disconnect() {
				if (this.client != null) {
					this.client.disconnect();
				}
			},
		},
		destroyed() {
			// 销毁监听
			this.disconnect();
		}
	}
</script>
这里提供一个简单的示例代码,实现了Spring Boot和Vue.js的单聊功能,使用WebSocket进行实时通信,并使用Redis存储历史消息后端代码(Spring Boot): 1. 依赖: ```xml <dependencies> <!-- Spring Boot Websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!-- Spring Boot Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies> ``` 2. 配置文件: ```yml spring: redis: host: localhost port: 6379 logging: level: org.springframework.web.socket: DEBUG ``` 3. 实体类: ```java public class Message { private String from; private String to; private String content; private Date time; // getters and setters } ``` 4. WebSocket配置: ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private WebSocketHandler webSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/chat").setAllowedOrigins("*"); } } ``` 5. WebSocket处理器: ```java @Component public class WebSocketHandler extends TextWebSocketHandler { @Autowired private RedisTemplate<String, Message> redisTemplate; private ObjectMapper objectMapper = new ObjectMapper(); private static final String KEY_PREFIX = "chat:"; @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 获取当前用户 String user = (String) session.getAttributes().get("user"); // 订阅Redis频道 redisTemplate.execute(new RedisCallback<Void>() { @Override public Void doInRedis(RedisConnection connection) throws DataAccessException { connection.subscribe(new MessageListener(), KEY_PREFIX + user); return null; } }); // 发送历史消息 List<Message> messages = redisTemplate.opsForList().range(KEY_PREFIX + user, 0, -1); if (messages != null && messages.size() > 0) { for (Message message : messages) { session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); } } } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 获取当前用户 String user = (String) session.getAttributes().get("user"); // 解析消息 Message msg = objectMapper.readValue(message.getPayload(), Message.class); msg.setFrom(user); msg.setTime(new Date()); // 存储到Redis redisTemplate.opsForList().rightPush(KEY_PREFIX + msg.getTo(), msg); // 发送给对方 WebSocketSession targetSession = sessions.get(msg.getTo()); if (targetSession != null && targetSession.isOpen()) { targetSession.sendMessage(new TextMessage(objectMapper.writeValueAsString(msg))); } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { // 获取当前用户 String user = (String) session.getAttributes().get("user"); // 取消订阅Redis频道 redisTemplate.execute(new RedisCallback<Void>() { @Override public Void doInRedis(RedisConnection connection) throws DataAccessException { connection.unsubscribe(KEY_PREFIX + user); return null; } }); } private Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>(); private class MessageListener implements MessageListenerAdapter { @Override public void onMessage(Message message, byte[] pattern) { WebSocketSession session = sessions.get(message.getTo()); if (session != null && session.isOpen()) { try { session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); } catch (Exception e) { e.printStackTrace(); } } } } } ``` 6. 控制器: ```java @RestController @RequestMapping("/api/chat") public class ChatController { @Autowired private RedisTemplate<String, Message> redisTemplate; @PostMapping("/send") public void send(@RequestBody Message message) { // 存储到Redis redisTemplate.opsForList().rightPush("chat:" + message.getFrom(), message); redisTemplate.opsForList().rightPush("chat:" + message.getTo(), message); // 发布消息 redisTemplate.convertAndSend("chat:" + message.getTo(), message); } @GetMapping("/history") public List<Message> history(String user1, String user2) { String key = "chat:" + user1 + ":" + user2; List<Message> messages = redisTemplate.opsForList().range(key, 0, -1); Collections.reverse(messages); return messages; } } ``` 前端代码(Vue.js): 1. 依赖: ```html <script src="/js/vue.min.js"></script> <script src="/js/sockjs.min.js"></script> <script src="/js/stomp.min.js"></script> <script src="/js/lodash.min.js"></script> ``` 2. HTML: ```html <div id="app"> <div> <label>当前用户:</label> <select v-model="currentUser" @change="connect"> <option v-for="user in users" :value="user">{{ user }}</option> </select> </div> <div v-if="connected"> <div> <label>对方用户:</label> <input v-model="otherUser"> </div> <div> <textarea v-model="message"></textarea> <button @click="send">发送</button> </div> <div> <ul> <li v-for="msg in messages">{{ msg.from }} -> {{ msg.to }}: {{ msg.content }}</li> </ul> </div> </div> </div> ``` 3. JavaScript: ```javascript var app = new Vue({ el: '#app', data: { users: ['user1', 'user2', 'user3'], currentUser: 'user1', otherUser: '', message: '', connected: false, messages: [] }, methods: { connect: function () { var self = this; if (self.stompClient != null) { self.stompClient.disconnect(); } var socket = new SockJS('/chat'); self.stompClient = Stomp.over(socket); self.stompClient.connect({}, function () { self.stompClient.subscribe('/user/queue/messages', function (msg) { var message = JSON.parse(msg.body); self.messages.push(message); }); self.connected = true; }, function (error) { console.log(error); }); }, send: function () { var self = this; var message = { from: self.currentUser, to: self.otherUser, content: self.message }; self.stompClient.send('/app/chat/send', {}, JSON.stringify(message)); self.message = ''; }, loadHistory: function () { var self = this; axios.get('/api/chat/history', { params: { user1: self.currentUser, user2: self.otherUser } }).then(function (response) { self.messages = response.data; }).catch(function (error) { console.log(error); }); } }, watch: { otherUser: function (newValue) { var self = this; self.loadHistory(); } } }); ``` 注意事项: 1. Redis的键名使用了前缀“chat:”,以便区分其他数据; 2. 存储历史消息和订阅消息时,使用了当前用户的名称作为频道名称; 3. 在订阅消息时,使用了内部类MessageListener处理接收到的消息,然后发送给对应的WebSocketSession; 4. 在WebSocketSession关闭时,需要取消订阅Redis频道,以免造成资源浪费; 5. 前端使用了STOMP协议进行通信,需要安装sockjs-client和stompjs库; 6. 前端通过WebSocket连接到后端时,需要指定当前用户; 7. 前端通过WebSocket接收消息时,需要将消息添加到消息列表中; 8. 前端通过REST API加载历史消息时,需要指定当前用户和对方用户。 这是一个基础的示例,具体实现可以根据自己的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值