import { Stomp } from '@stomp/stompjs';
import _ from 'lodash';
import store from './store/index'
let timer
let stompClient;
connect();
function connect() {
let socket = new WebSocket('地址');
stompClient = Stomp.over(socket);
stompClient.connect(
{},
function () {
stompClient.subscribe(`前缀`, function (message) {
clearTimeout(timer)
//接收
let msg = JSON.parse(message.body);
console.log(msg);
// 前后沟通格式 指令的字段
// const reception = {
// operate:'',// switchYear,detail,syncScreen
// homePage:"",// welcome,screen,timeline
// year:'',// 2004,2005...
// url:'',// 查看的图片地址或close
// mode:'',// VIP COMMON
// address:'',// One Two Three Four
// text:'' 文本
// }
function () {
setTimeout(() => {
connect();
}, 1000);
}
);
}
// 推送信息
function sendMessageDo(obj) {
if (!stompClient.connected) return;
stompClient.send(`前缀`, {}, JSON.stringify(obj));
}
//推送
const sendMessage = _.debounce(sendMessageDo, 1000, { 'leading': true });
export default sendMessage // 导出 你可以在main.js中导入玩玩
//在此应用中,websockt接收和推送两种消息
//1.路由切换 type="hashChange"
//2.点击操作 type="action"
关于webSockt双向通信 开箱即用 做双向控制
最新推荐文章于 2024-06-06 14:49:19 发布