vue2.0
<script>
import store from '@/store/'
export default {
data() {
return {
}
},
mounted() {
//初始化websocket
this.initWebSocket()
},
destroyed: function () { // 离开页面生命周期函数
this.websocketclose();
},
methods: {
initWebSocket: function () { // 建立连接
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
var userId = store.getters.userInfo.id;
var url = window._CONFIG['domianURL'].replace("https://","ws://").replace("http://","ws://")+"/websocket/"+userId;
this.websock = new WebSocket(url);
this.websock.onopen = this.websocketonopen;
this.websock.send = this.websocketsend;
this.websock.onerror = this.websocketonerror;
this.websock.onmessage = this.websocketonmessage;
wesocket对接vue2.0和vue3.0
最新推荐文章于 2024-08-18 17:53:49 发布
该博客详细介绍了在Vue2.0和Vue3.0中如何使用WebSocket进行连接初始化、消息发送、接收及错误处理。在Vue2.0中,`mounted`和`destroyed`生命周期钩子用于打开和关闭连接,而在Vue3.0中,采用函数式组件并利用`onMounted`进行初始化。当接收到不同类型的`cmd`消息时,客户端会执行不同的业务响应,如系统通知和用户消息处理。

最低0.47元/天 解锁文章
2239

被折叠的 条评论
为什么被折叠?



