letlogins=[getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),];letinterval;app.on("connection",(connection)=>{// On a new real-time connection, add it to the anonymous channelapp.channel("anonymous").join(connection);// create 5 second interval to emit "dataAvailable" event with data payloadinterval=setInterval(()=>{console.log("Sending new data");// remove one value, add a new onelogins.shift();logins.push(getRandomInt(50,70));// send the data through the 'dataAvailable' eventapp.io.emit("dataAvailable",{messageCount:getRandomInt(1000,10000)+getRandomInt(0,100),activeChatRooms:getRandomInt(5,100),recentLogins:logins,openTickets:getRandomInt(0,100),closedTickets:getRandomInt(0,100),unassignedTickets:getRandomInt(0,100),});},5000);});app.on("disconnect",(connection)=>{clearInterval(interval);});
mounted(){// add an event listener to dataAvailable eventthis.establishConnection();},destroyed(){// remove the dataAvailable event listenerthis.destroyConnection();},methods:{destroyConnection(){feathersClient.io.off('dataAvailable');},establishConnection(){feathersClient.io.on('dataAvailable',(data)=>{console.log('Receiving data from server: ',JSON.stringify(data));// update variables to the data received from the serverthis.messageCount=data.messageCount;this.recentLogins=data.recentLogins;this.activeChatRooms=data.activeChatRooms;this.openTickets=data.openTickets;this.closedTickets=data.closedTickets;this.unassignedTickets=data.unassignedTickets;this.serverMessage=data;});},},
? What kind of service is it? A custom service
? What is the name of the service? metrics
? Which path should the service be registered on? /metrics
? Does the service require authentication? No
create src/services/metrics/metrics.service.js
force src/services/index.js
create src/services/metrics/metrics.class.js
create src/services/metrics/metrics.hooks.js
create test/services/metrics.test.js
将 MetricsService 定义为每 5 秒创建一次数据的自定义服务。
const{getRandomInt}=require("../../utils/dataGenerator");/* eslint-disable no-unused-vars */exports.Metrics=classMetrics{asynccreate(data){returndata;}setup(){letlogins=[getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),getRandomInt(50,70),];setInterval(()=>{console.log("Sending new data");logins.shift();logins.push(getRandomInt(50,70));this.create({messageCount:getRandomInt(1000,10000)+getRandomInt(0,100),activeChatRooms:getRandomInt(5,100),recentLogins:logins,openTickets:getRandomInt(0,100),closedTickets:getRandomInt(0,100),unassignedTickets:getRandomInt(0,100),});},5000);}};
然后我们可以更新数据连接以使用该服务:
establishConnection(){feathersClient.service('metrics').on('created',data=>{console.log('Receiving data from server: ',JSON.stringify(data));// update variables to the data received from the serverthis.messageCount=data.messageCount;this.recentLogins=data.recentLogins;this.activeChatRooms=data.activeChatRooms;this.openTickets=data.openTickets;this.closedTickets=data.closedTickets;this.unassignedTickets=data.unassignedTickets;this.serverMessage=data;});},}