|
@@ -38,6 +38,57 @@ const saveLifeData = function(key, value) {
|
|
|
uni.setStorageSync('lifeData', tmp);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// websocket 连接
|
|
|
+const SocketInit = function($store,state,wsUrl){
|
|
|
+ let token = getApp().globalData.token
|
|
|
+ // 开始连接
|
|
|
+ state.vuex_socket = uni.connectSocket({
|
|
|
+ header: {
|
|
|
+ 'X-AUTH-TOKEN': token
|
|
|
+ },
|
|
|
+ url: wsUrl, // ws 请求地址
|
|
|
+ complete: () => {
|
|
|
+ console.log('连接complete回调!')
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 打开连接
|
|
|
+ state.vuex_socket.onOpen(function(e) {
|
|
|
+ console.log('连接成功!')
|
|
|
+ uni.$emit('wsMessage', 'connectOpen')
|
|
|
+ })
|
|
|
+ // 连接关闭
|
|
|
+ state.vuex_socket.onClose(function(e) {
|
|
|
+ console.log('ws关闭!')
|
|
|
+ })
|
|
|
+ // 连接错误
|
|
|
+ state.vuex_socket.onError(function(e) {
|
|
|
+ console.log('ws错误!', JSON.stringify(e))
|
|
|
+ })
|
|
|
+ // 接受消息
|
|
|
+ state.vuex_socket.onMessage(function(e) {
|
|
|
+ console.log(e,'onmessage')
|
|
|
+ if (e.data == 'pong' || e.data == 'connected') {
|
|
|
+ uni.$emit('wsMessage', e.data)
|
|
|
+ }else{
|
|
|
+ uni.$emit('wsMessage', JSON.parse(e.data))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // 发送心跳包
|
|
|
+ state.vuex_heartInterId = setInterval(function() {
|
|
|
+ // console.log(state.vuex_socket.readyState)
|
|
|
+ if (state.vuex_socket.readyState != 1) {
|
|
|
+ // 停止心跳
|
|
|
+ clearInterval(state.vuex_heartInterId)
|
|
|
+ SocketInit($store,state,wsUrl)
|
|
|
+ uni.$emit('wsMessage','reconnect')
|
|
|
+ } else {
|
|
|
+ $store.commit("$sendWebsocket", "ping")
|
|
|
+ }
|
|
|
+ }, 20000)
|
|
|
+}
|
|
|
+
|
|
|
+// store 对象
|
|
|
const store = new Vuex.Store({
|
|
|
// 下面这些值仅为示例,使用过程中请删除
|
|
|
state: {
|
|
@@ -99,57 +150,10 @@ const store = new Vuex.Store({
|
|
|
// 创建通知消息websocket
|
|
|
$webSocket(state, params) {
|
|
|
let _this = this
|
|
|
- let token = getApp().globalData.token
|
|
|
let url = getApp().globalData.baseUrl
|
|
|
let wsBaseUrl = url.indexOf("https:") >= 0 ? url.replace("https:", "wss:") : url.replace("http:", "ws:")
|
|
|
let wsUrl = wsBaseUrl + 'websocket/weixin/' + state.vuex_orderInfo.bizId
|
|
|
- // 开始连接
|
|
|
- state.vuex_socket = uni.connectSocket({
|
|
|
- header: {
|
|
|
- 'X-AUTH-TOKEN': token
|
|
|
- },
|
|
|
- url: wsUrl, // ws 请求地址
|
|
|
- complete: () => {
|
|
|
- console.log('连接complete回调!')
|
|
|
- }
|
|
|
- });
|
|
|
- // 打开连接
|
|
|
- state.vuex_socket.onOpen(function(e) {
|
|
|
- console.log('连接成功!')
|
|
|
- })
|
|
|
- // 连接关闭
|
|
|
- state.vuex_socket.onClose(function(e) {
|
|
|
- console.log('ws关闭!')
|
|
|
- })
|
|
|
- // 连接错误
|
|
|
- state.vuex_socket.onError(function(e) {
|
|
|
- console.log('ws错误!', JSON.stringify(e))
|
|
|
- })
|
|
|
- // 接受消息
|
|
|
- state.vuex_socket.onMessage(function(e) {
|
|
|
- if (e.data == 'pong' || e.data == 'connected') {
|
|
|
- uni.$emit('wsMessage', e.data)
|
|
|
- }else{
|
|
|
- uni.$emit('wsMessage', JSON.parse(e.data))
|
|
|
- }
|
|
|
- })
|
|
|
- // 发送心跳包
|
|
|
- state.vuex_heartInterId = setInterval(function() {
|
|
|
- // console.log(state.vuex_socket.readyState)
|
|
|
- if (state.vuex_socket.readyState != 1) {
|
|
|
- state.vuex_socket = uni.connectSocket({
|
|
|
- header: {
|
|
|
- 'X-AUTH-TOKEN': token
|
|
|
- },
|
|
|
- url: wsUrl, // ws 请求地址
|
|
|
- complete: () => {
|
|
|
- console.log('连接complete回调!')
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- _this.commit("$sendWebsocket", "ping")
|
|
|
- }
|
|
|
- }, 10000)
|
|
|
+ SocketInit(_this,state,wsUrl)
|
|
|
},
|
|
|
// 关闭websocket
|
|
|
$closeWebsocket(state) {
|
|
@@ -168,7 +172,7 @@ const store = new Vuex.Store({
|
|
|
state.vuex_socket.send({
|
|
|
data: msg,
|
|
|
complete: function(e) {
|
|
|
- console.log('ws发送完成', e)
|
|
|
+ console.log('ws发送完成', msg)
|
|
|
}
|
|
|
})
|
|
|
},
|