| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- <script>
- import config from '@/util/url.js';
- import HeartBeat from '@/util/HeartBeat.js'
- export default {
- globalData: {
- token: "",
- // 文本消息
- socketTask: null,
- // urlData: "wss://www.toreskj.com/api/api_gateway?method=control_center.real_time.im_message",
- urlData: config.wsUrl + "/api/api_gateway?method=control_center.real_time.im_message",
- is_open_socket: false, // 确保websocket是打开状态
- list: [],
- userId: null, // 用户ID
- // 即构
- video: null, //房间密码和token
- // 测试 - 心跳检测
- status: null, // websocket是否关闭
- lockReconnect: false, // 避免重复连接
- // timeout: 5000, // 多少秒执行监测
- timeout: 10000, // 多少秒执行监测
- timeoutObj: null, // 检测服务器端是否还活着
- reconnectTimeOutObj: null, // 重连之后多久再次重连
- // 测试
- app: [],
- // 消息发送加载
- msgSetTime: null,
- g_wakelock: null
- },
- onLoad() {},
- onLaunch: function() {
- console.log('启动了')
- HeartBeat.getInstance().destory();
- // App启动
- var that = this
- // #ifdef APP-PLUS
- this.wakeLock()
- // #endif
- // uni.getStorage({
- // key: 'session_key',
- // success: function(res) {
- // that.globalData.token = res.data
- // // console.log('登录成功了')
- // that.msgInit(); // 文本初始化
- // },
- // fail: function(err) {
- // console.log(err)
- // console.log('没有登录成功')
- // }
- // })
- },
- onShow: function() {
- var that = this
- this.$store.dispatch('updateTaskRecordRatio')
- // App展示在前台
- console.log('+++++++++++++++++++App Show-------------------------------------')
- // this.msgInit(); // 再进行请求
- uni.getStorage({
- key: 'session_key',
- success: function(res) {
- if(!res.data){
- return
- }
-
- that.globalData.token = res.data
- // console.log('登录成功了')
- console.log(res)
- that.msgInit(); // 文本初始化
- },
- fail: function(err) {
- console.log(err)
- console.log('没有登录成功')
- }
- })
- },
- onHide: function() {
- // App不再再展示在前台
- this.close(); // 关闭
- },
- methods: {
- // 实时通信 - 初始化
- msgInit() {
- console.log('进入了实时通信初始化=--------------------------', this.globalData.token)
- var that = this
- // 连接webscoket
- that.globalData.socketTask = uni.connectSocket({
- url: this.globalData.urlData + "&token=" + that.globalData.token,
- success(data) {
- // console.log("websocket连接中");
- }
- });
- // 发送消息 - 获取对话列表
- var obj = {
- 'action': 'list', // 动作标识,必填
- 'recv_user_id': '', // 接收人用户id, 非必填
- 'data': {}
- }
- that.globalData.socketTask.onOpen((res) => {
- console.log('websocket开启了')
- // 清除重连定时器
- // clearTimeout(that.globalData.reconnectTimeOutObj)
- // that.notReconnect()
- that.globalData.socketTask.send({
- data: JSON.stringify(obj),
- async success(res) {
- that.globalData.is_open_socket = true
- console.log("消息发送成功");
- uni.hideLoading(); // 关闭通信异常弹框
- },
- });
- that.start() // 开启心跳机制
- })
- // // 接收服务器返回的消息
- // // 注:只有连接正常打开中 ,才能正常收到消息
- that.globalData.socketTask.onMessage((res) => {
- // console.log("收到服务器内容:");
- var val = JSON.parse(res.data)
- console.warn('这是返回的内容---------', val)
- clearTimeout(that.globalData.msgSetTime); // 消息发送成功,清除发送失败定时器
- if (val.action == 'list') {
- clearTimeout(that.globalData.msgSetTime); // 消息发送成功,清除发送失败定时器
- console.log('这是发送人的ID', that.globalData.userId)
- var obj1 = {
- 'action': 'list', // 动作标识,必填
- 'recv_user_id': that.globalData.userId, // 接收人用户id, 非必填
- 'data': {
- 'msg_status': false, // 消息未读
- 'msg_info': '', // 发送消息
- }
- }
- that.globalData.socketTask.send({
- data: JSON.stringify(obj1),
- async success(res) {},
- });
- that.globalData.userId = null
- } else if (val.action == 'none') {
- console.log('进入了none', val)
- that.globalData.list = val.data
- // this.$store.state.list = val.data
- this.$store.commit('updateMessageList', val.data)
- } else if (val.action == 'recv_video') {
- console.warn('recv_video---------------- 1', val.data, this.$store.state
- .isVideoCallRefused)
- // if (this.$store.state.isVideoCallRefused) {
- // this.$store.commit('updateIsRefusedCall', false)
- // return
- // }
- // this.$store.state.video = val.data
- this.$store.commit('updateIsRefusedCall', false)
- if (this.$store.state.isVideoTalking) {
- // 占线中
- var obj = {
- 'action': 'occupy', // 动作标识,必填
- 'recv_user_id': val.data.user_id, // 接收人用户id, 非必填
- 'data': {}
- }
- this.globalData.socketTask.send({
- data: JSON.stringify(obj),
- async success(res) {
- console.warn('占线消息发送成功1111', obj)
- }
- });
- return
- }
- that.globalData.video = val.data
- uni.switchTab({
- url: '/pages/response/index',
- success(res) {
- console.warn(res, '--------------response index into room ')
- },
- fail(err) {
- console.error(err)
- },
- complete() {
- console.warn('--------------response index into room complete')
- }
- })
- this.$store.commit('updateVideoInfo', val.data)
- setTimeout(() => {
- console.log('跳转了')
- this.$store.getters.watchData;
- }, 1500)
- } else if (val.action == 'send_video_rsp') {
- console.warn('recv_video---------------- response', val.data)
- // if (this.$store.state.isVideoCallRefused) {
- // this.$store.commit('updateIsRefusedCall', false)
- // return
- // }
- this.$store.commit('updateIsRefusedCall', false)
- // if(this.)
- this.$store.commit('updateVideoSelfRoomInfo', val.data)
- console.warn('into romm ready ------------------', this.roomInfo)
- console.warn('into romm ------------------', this.roomInfo)
- this.$store.commit('updateTalkingStatus', true)
- uni.navigateTo({
- url: '/pages/response/video?videoObj=' + JSON.stringify(val.data),
- success(res) {
- console.warn(res, '-------------- into room ')
- },
- fail(err) {
- console.error(err)
- },
- complete() {
- console.warn('-------------- into room complete')
- }
- })
- } else if (val.action == 'reject_video_rsp') {
- console.warn('reject_video_rsp', '挂断 拒接')
- // 被拒绝后挂断视频通话
- this.$store.commit('updateIsRefusedCall', true)
- } else if (val.action == 'occupy_rsp') {
- console.warn('----------------------- is refused')
- // 占线中,挂断发起通话
- this.$store.commit('updateOccupyedStatus',true);
- this.$store.commit('updateIsRefusedCall', true)
- } else if (val.action == 'ok') {
- this.start(); // 心跳机制
- } else if (val.action == 'error') {
- console.log('action error')
- if (val.code == 403 || val.data && val.data.code == 403) {
- this.close();
- uni.setStorageSync('session_key', '');
- this.globalData.token = '';
- if (!this.$store.state.isInLoginPage) {
- uni.$u.totast && uni.$u.totast(val.data.data && val.data.data.error_info || '用户在其他地方登陆,请重新登陆')
- setTimeout(() => {
- uni.reLaunch({
- url: "/pages/login/login"
- })
- }, 2000)
- }
- }
- }
- // this.reset();
- // console.log('消息可正常接收');
- });
- // 连接断开
- // 如果希望websocket连接一直保持,在close或者error上绑定重新连接方法。
- this.globalData.socketTask.onClose((res) => {
- console.warn(res, '连接关闭');
- clearTimeout(this.globalData.timeoutObj);
- setTimeout(() => {
- that.reconnect();
- }, 5000)
- })
- // 连接失败
- that.globalData.socketTask.onError((res) => {
- console.log(res, '连接错误');
- // uni.showLoading({
- // title: "通信异常,连接中..."
- // })
- clearTimeout(this.globalData.timeoutObj);
- setTimeout(() => {
- that.reconnect();
- }, 5000)
- })
- },
- // 心跳机制
- start() {
- var that = this
- this.globalData.timeoutObj = setTimeout(() => {
- //这里发送一个心跳,后端收到后,返回一个心跳消息,
- //onmessage拿到返回的心跳就说明连接正常
- var obj = {
- 'action': 'keepalive', // 动作标识,必填
- 'type': 'App',
- 'recv_user_id': '', // 接收人用户id, 非必填
- 'data': {
- msg_status: false,
- msg_info: "",
- }
- }
- that.globalData.socketTask.send({
- data: JSON.stringify(obj),
- async success(res) {
- that.globalData.is_open_socket = true
- // console.log('消息发送成功1111')
- }
- });
- console.log('进入心跳机制了', that.globalData.socketTask)
- let ws_temp = null
- ws_temp = that.globalData.socketTask
- that.globalData.socketTask = ws_temp
- ws_temp = null
- }, this.globalData.timeout)
- },
- // 清除心跳机制
- reset() {
- // 清除定时器重新发送一个心跳信息
- // clearTimeout(this.globalData.timeoutObj);
- clearTimeout(this.globalData.timeoutObj);
- this.start();
- },
- // 重连
- reconnect() {
- // 防止多个方法调用,多处重连
- if (this.lockReconnect) {
- return;
- };
- if(!this.globalData.token){
- return
- }
-
- this.lockReconnect = true;
- console.log('准备重连');
- //没连接上会一直重连,设置延迟避免请求过多
- this.globalData.reconnectTimeOutObj = setTimeout(() => {
- // 重新连接
- this.msgInit()
- console.log('正在重新连接')
- this.lockReconnect = false;
- }, 4000);
- },
- // 清除重连
- notReconnect() {
- clearTimeout(this.globalData.reconnectTimeOutObj)
- },
- // 手动关闭
- close() {
- var that = this
- console.log('已经手动关闭了')
- that.globalData.socketTask.close()
- that.globalData.socketTask = null
- clearTimeout(that.globalData.timeoutObj);
- clearTimeout(that.globalData.reconnectTimeOutObj);
- uni.hideLoading(); // 关闭通信异常弹框
- },
- //允许程序后台运行,以持续获取GPS位置
- wakeLock() {
- //Android
- const main = plus.android.runtimeMainActivity();
- console.warn('--------- walk lock------------ dsd', main)
- const Context = plus.android.importClass("android.content.Context");
- const PowerManager = plus.android.importClass("android.os.PowerManager");
- const pm = main.getSystemService(Context.POWER_SERVICE);
- this.g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
- this.g_wakelock.acquire();
- },
- //结束程序后台运行
- releaseWakeLock() {
- if (this.g_wakelock != null && this.g_wakelock.isHeld()) {
- this.g_wakelock.release();
- this.g_wakelock = null;
- }
- },
- }
- }
- </script>
- <!-- // App.vue文件 中首行的位置引入,注意给style标签加入lang="scss"属性 -->
- <style lang="scss">
- @import './common/zego-nvue.css';
- /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
- @import "uview-ui/index.scss";
- </style>
|