123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import {
- API_URL,
- API_ROOT,
- UNIACID
- } from '@/common/request/request';
- import store from "@/common/store";
- //是否已经连接上ws
- let isOpenSocket = false
- //心跳间隔,单位毫秒
- let heartBeatDelay = 10000
- let heartBeatInterval = null
- //心跳时发送的消息文本
- let heartBeatText = "PING"
- //最大重连次数
- let reconnectTimes = 10
- let reconnectInterval = null
- //重连间隔,单位毫秒
- let reconnectDelay = 5000
-
- var url = API_ROOT;
-
- var domain = url.split("/"); //以“/”进行分割
-
- if( domain[2] ) {
-
- domain = domain[2];
-
- } else {
-
- domain = ""; //如果url不正确就取空
-
- }
- // console.log("wss://"+domain+":8288");
- //let wsUrl = "wss://faraway.wike.cc/wss" //ws请求 或者 wss
- let wsUrl = [{url:'wss://away.wike.cc/wss'},{url:'wss://faraway.wike.cc/wss'}];
- let socketTask = null
-
- //这个参数是防止重连失败之后onClose方法会重复执行reconnect方法,导致重连定时器出问题
- //连接并打开之后可重连,且只执行重连方法一次
- let canReconnect = false
-
- //封装的对象,最后以模块化向外暴露,
- //init方法 初始化socketTask对象
- //completeClose方法 完全将socketTask关闭(不重连)
- //其他关于socketTask的方法与uniapp的socketTask api一致
- let ws = {
- socketTask: null,
- init,
- completeClose,
- send,
- socketStatus
- }
-
- function init(s) {
- let u = wsUrl[uni.$u.random(0, 1)].url;
- // if(store.getters.appInfo && store.getters.appInfo.connect_lines){
- // if(store.getters.appInfo.connect_lines == 2){
- // wsUrl = "wss://faraway.wike.cc/wss"
- // }
- // }
- socketTask = uni.connectSocket({
- url: u,
- complete: (res)=> {console.log("WebSocket连接成功",res,u)}
- })
- socketTask.onOpen((res) => {
- console.log("WebSocket连接已打开",res)
- clearInterval(heartBeatInterval)
- clearInterval(reconnectInterval)
- isOpenSocket = true
- canReconnect = true
-
- //开启心跳机制 向websocket发送数据,json格式,参数:sceneId
- // heartBeat()
- })
- socketTask.onMessage((res) => {
- // 每次返回的数据不一样,需要加入判断
- // console.log('收到服务器内容',JSON.parse(res.data))
- // let result = JSON.parse(res.data)
- // //这边可以根据推送的数据, 做相应的 do somethings, 根据自己需求, 下面只是例子..
- // // token存在,说明,小程序用户点击了授权(比如推送的数据有token,那就做什么事xxxx
- // send('{"state":"bind","fromid":"' + store.getters.userInfo.id + '"}')
-
- // if(result.state == 'new_content'){
- // uni.vibrateLong({
- // success: function () {
- // // console.log('success');
- // }
- // });
- // uni.showTabBarRedDot({
- // index:2
- // })
- // }
- // if(result.token){
- // uni.setStorageSync('token', result.token)
- // uni.setStorageSync('userInfo', JSON.stringify(result.userInfo))
- // }
- // client_id存在,说明连websocket接成功
- // if(result.msg=="连接成功"){
- // uni.setStorageSync('client_id', result.data.client_id);
- // }
- // JSON.parse(res.data).msg=="已归还导览机", 已归还导览机,清空用户缓存
- // if(result.msg=="已归还导览机"){
-
- // uni.clearStorageSync();
- // uni.navigateBack({
- // delta: 100
- // })
- // }
-
- })
- socketTask.onClose(() => {
- // console.log(isOpenSocket);
- if(isOpenSocket){
- console.log("ws与服务器断开")
- }else{
- console.log("连接失败")
- }
- isOpenSocket = false
- if(canReconnect){
- reconnect()
- canReconnect = false
- }
-
- })
- ws.socketTask = socketTask
- }
- function socketStatus() {
- return isOpenSocket;
- }
-
- function heartBeat() {
- heartBeatInterval = setInterval(() => {
- // console.log(heartBeatText)
- uni.getSystemInfo({
- success: function (res) {
- //console.log('设备id----',res.deviceId);//设备id
- uni.setStorageSync('deviceId', res.deviceId)
- let obj = {
- sceneId:res.deviceId
- }
- send(JSON.stringify(obj));
- }
- });
-
- }, heartBeatDelay)
- }
-
- // 发送消息
- function send(value) {
- ws.socketTask.send({
- data: value,
- async success(res) {
- // console.log("消息发送成功",res,value)
- }
- });
- }
- function reconnect() {
- //停止发送心跳
- clearInterval(heartBeatInterval)
- //如果不是人为关闭的话,进行重连
- if (!isOpenSocket) {
- let count = 0;
- // reconnectInterval = setInterval(() => {
- console.log("正在尝试重连")
- init();
- count++
- //重连一定次数后就不再重连
- if(count >= reconnectTimes){
- // clearInterval(reconnectInterval)
- console.log("网络异常或服务器错误")
- }
- // }, reconnectDelay)
- }
- }
- function completeClose(){
- //先将心跳与重连的定时器清除
- console.log('停止连接');
- clearInterval(heartBeatInterval)
- clearInterval(reconnectInterval)
- canReconnect = false
- ws.socketTask.close()
- }
-
- module.exports = ws
|