var hubUrl = config.hubUrl; var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build(); var connectionId; function connect() { if (connection.state === signalR.HubConnectionState.Disconnected) { connection.start().then(function () { console.log('signalR 连接成功'); }).catch(function (err) { console.log(err); setTimeout(connect, 5000); }); } } connection.onclose(function () { console.log('connect closed'); connect(); });connection.on('Connected', function (id) { connectionId = id; console.log('signalR 连接Id:' + connectionId); }); /** * 接收从服务器发出的信息 */ connection.on("ServerToClient", function (method, message, to, from) { /** * 格式化事件字符串 */ var event = method.substr(0, 1).toLowerCase() + method.substr(1); /** * 根据当前路由调用组件关联的消息回调方法 */ var model = JSON.parse(message); router.currentRoute.meta.onMessage(event, model, to, from); // 本地存储消息 saveMessage(message); /** * 调用定义的update事件处理 * 注:设备移除 * payload: {event,model,to,from} * event-pre: 'data', 'device', 'node' * event: 'EntityUpdated','EntityUpdated','EntityInserted','EntityDeleted' * model: {key: "State", value: "开", name: "状态", type: 50, unit: null, …} * to: 'page' * from: null */ /** * 事件过滤处理,绑定到对应异步操作 * 除设备操作事件外(Event > Mutations > State),其他事件只做Action触发,不直接刷新数据 * 具体流程:Event > Actions > Mutations > State */ switch(event) { /** * 产品添加 * count: 1 * displayOrder: 0 * id: "5e3fb468-b7e7-b9d5-e2f2-cca918d28844" * image: "/images/socket.png" * name: "插座" * number: "fbee:0009:01" */ case 'productEntityInserted': //alert('getProducts') //store.dispatch('getProducts', payload); break; // 设备移除 case 'deviceEntityDeleted': // 设备新增 case 'deviceEntityInserted': // 当当前为产品设备列表页面时,刷新产品设备列表 (由于每次进入页面都触发数据刷新,非当前页面则不做数据更新处理) var number = getUrlKey('number'); if(router.currentRoute.path === '/pages/iot/product'){ if(number){ // 向产品设备列表中新增 store.dispatch('getProduct', {number:number}); } } // 产品列表 else if(router.currentRoute.path === '/pages/iot/products'){ // 更新产品列表设备数 store.dispatch('getProducts'); } // 节点列表(智慧教室) else if(router.currentRoute.path === '/pages/iot/nodes'){ // 更新产品列表设备数 store.dispatch('getNodes'); } break; default: /** * dataEntityUpdated */ //store.dispatch('update', payload); } });