import Action from '@actions'; import {Modal, Socket} from 'frc-ui-pro'; import UserCache from '@caches/UserCache'; interface SocketItem { key: string; payload?: any; } export default (opt: any) => { const {host, path, onMessage, socketArr} = opt || {}; // 初始化 socket if (socketArr.length > 0) { Socket.on('login', () => { // 开始订阅 // 订阅推送 - 此处用 Action.emit 打接口 socketArr.forEach((item: SocketItem) => { const {key, payload = {}} = item; if (key) { Action.emit(key, {...payload}); } }); }); Socket.on('close', () => { Modal.info({ title: '提示', content: '服务连接已断开', okText: '重新连接', onOk() { window.location.reload(); } }); }); Socket.on('message', onMessage); Socket.start({host, path, userId: UserCache.user.userId, token: UserCache.user.gatewayToken}); } };