websocket

This commit is contained in:
xiaoshan
2023-09-06 17:55:48 +08:00
parent 13309008f0
commit 2c634ecd6f
5 changed files with 88 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
//#ifdef H5 //#ifdef H5
this.checkLogin() this.checkLogin()
//#endif //#endif
this.$store.dispatch('websocketInit')
}, },
initConfig() { initConfig() {
this.globalData.config = config this.globalData.config = config

View File

@@ -2,6 +2,8 @@
module.exports = { module.exports = {
baseUrl: 'http://localhost:8080', baseUrl: 'http://localhost:8080',
fileUploadUrl: 'http://192.168.1.135:8080/prod-api/uploadImage', fileUploadUrl: 'http://192.168.1.135:8080/prod-api/uploadImage',
WebSocketUrl: 'ws://121.199.24.205:9107/lxk/websocket',
WebSocketOpen: true,
// 应用信息 // 应用信息
appInfo: { appInfo: {
// 应用名称 // 应用名称

View File

@@ -1,13 +1,15 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import user from '@/store/modules/user' import user from '@/store/modules/user'
import websocket from '@/store/modules/websocket.js'
import getters from './getters' import getters from './getters'
Vue.use(Vuex) Vue.use(Vuex)
const store = new Vuex.Store({ const store = new Vuex.Store({
modules: { modules: {
user user,
websocket
}, },
getters getters
}) })

View File

@@ -0,0 +1,81 @@
import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { getToken, setToken, removeToken } from '@/utils/auth'
const baseUrl = config.WebSocketUrl
const websocket = {
state: {
socketTask: null,
websocketData: {}, // 存放从后端接收到的websocket数据
},
mutations: {
// 初始化
setWebsocketData (state, data) {
state.websocketData = data
}
},
actions: {
websocketInit ({ state, dispatch }, url) {
if(config.WebSocketOpen && getToken()) {
state.socketTast = uni.connectSocket({
url: `${baseUrl}?Authorization=Bearer ${getToken()}`, // url是websocket连接ip
success: () => {
console.log('websocket连接成功')
},
fail: e => {
console.log('连接失败' + e)
}
})
state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
state.socketTast.onClose(e => dispatch('websocketOnClose'))
state.socketTast.onError(e => dispatch('websocketOnError'))
}
},
websocketOnOpen ({ commit }) {
console.log('WebSocket连接正常打开中...')
},
// 收到数据
websocketOnMessage ({ commit }, res) {
console.log('收到服务器内容:' + res.data)
if (res.data !== '连接成功') {
commit('setWebsocketData', (res && JSON.parse(res.data) || null))
}
},
websocketOnClose ({ commit, dispatch }) {
console.log('WebSocket连接关闭')
},
websocketOnError ({ commit, dispatch }) {
console.log('WebSocket连接错误')
},
websocketClose ({ state }) {
if (!state.socketTast) return
state.socketTast.close({
success (res) {
console.log('关闭成功', res)
},
fail (err) {
console.log('关闭失败', err)
}
})
},
// 发送数据
websocketSend ({ state }, data) {
uni.sendSocketMessage({
data,
success: res => {
console.log('发送成功', res)
},
fail: e => {
console.log('发送失败', e)
}
})
}
}
}
export default websocket

View File

@@ -3,7 +3,7 @@ const version = '2.0.36'
// 开发环境才提示,生产环境不会提示 // 开发环境才提示,生产环境不会提示
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
console.log(`\n %c uView V${version} %c https://uviewui.com/ \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0; border-radius: 5px;'); // console.log(`\n %c uView V${version} %c https://uviewui.com/ \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0; border-radius: 5px;');
} }
export default { export default {