# AS-WebSocket **Repository Path**: 125586657/web-socket-encapsulation ## Basic Information - **Project Name**: AS-WebSocket - **Description**: 使用vuex封装了一下WebSocket - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2025-05-14 - **Last Updated**: 2025-05-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AS-WebSocket #### 介绍 使用vuex封装了一下WebSocket #### 使用说明 1. 初始化 WebSocket ```javascript this.$store.dispatch("WEBSOCKET_INIT", "wss://www.huobi.ge/-/s/pro/ws"); ``` 2. 发送消息 注:发送消息前确保连接正常 ```javascript let Interval = setInterval(() => { // 定时查询 连接是否正常 if (this.$store.state.websocket.is_open_socket) { // 发送消息 this.$store.dispatch('WEBSOCKET_SEND', JSON.stringify('data')) // 连接正常移除定时器 clearInterval(Interval) } console.log(this.$store.state.websocket.is_open_socket) }, 1000) ``` 3. 断线重连 ```javascript // 断线事件触发的回调 EventBus.$on('stopSocket',()=>{ let Interval = setInterval(() => { // 定时查询 连接是否正常 if(this.$store.state.websocket.is_open_socket){ // 发送消息 this.$store.dispatch("WEBSOCKET_SEND", JSON.stringify(data)); // 连接正常移除定时器 clearInterval(Interval) } // console.log(this.$store.state.websocket.is_open_socket) }, 10000) }) ``` 3.事件总集使用 ``` // 引入事件总集 import { EventBus } from '@/utils/event-bus' // 注册事件 EventBus.$on('goSend', () => { this.goSend() }) // 触发事件 EventBus.$emit('goSend') ``` > 初始化 WebSocket 和 断线重连 最好在APP.vue里面执行