12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import WebSocketManager from '@/utils/WebSocketManager.js';
- import store from '@/store'
- import { getToken } from '@/utils/auth'
- export default {
- data() {
- return {
- // userId: store.state.user.userId,
- // ws: store.state.user.wsManager
- }
- },
- onShow: function () {
- // uni.loadFontFace({
- // family: "uicon-iconfont",
- // source: 'url("https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf")',
- // global: true,
- // success: (success) => {
- // console.log("Font loaded successfully", success);
- // },
- // fail: (fail) => {
- // console.log("Font loading failed", fail);
- // },
- // });
- uni.$u.connectSoket = this.connectSoket;
- uni.$u.closeSoket = this.closeSoket;
- uni.$u.debounce(this.connectSoket(), 500)
- },
- onHide: function () {
- this.closeSoket();
- },
- methods: {
- connectSoket(user_id){
- const ws = store.state.user.wsManager;
- const userId = user_id || uni.getStorageSync('userId');
- const token = getToken();
- if (!ws && userId && token) {
- // 获取账户时,连接soket
- const ms = new WebSocketManager(userId);
- // 设置消息回调
- ms.onMessage(data => {
- // 处理消息逻辑
- if (data.type === 'msgUnreadCount') {
- store.dispatch('handleMessageCount',data.data);
- }
- });
- // 建立连接
- ms.connect();
- store.dispatch('handleSoket',ms);
- }
- },
- closeSoket(){
-
- const ws = store.state.user.wsManager;
- if(ws){
- ws.closeConnection();
- }
-
- }
- }
- }
|