学习二中画点可能存在丢点的情况,稍微改进了一下,node.js接受数据时把数存起来,发送时一起发,html也一样
const dgram = require('dgram'); let server = dgram.createSocket('udp4'); server.bind(6000); var pp = []; server.on('message',function(msg,rinfo){ const t = msg.slice(0,4); const b = msg.slice(4,8); const l = msg.slice(8,12); const p = new Array(4); p[0] = 32; p[0] = p[0].toString(); p[1] = t.readFloatLE(0).toString(); p[2] = b.readFloatLE(0).toString(); p[3] = l.readFloatLE(0).toString(); pp.push(p); //console.log(pp.toString()); }) var time = 1; var ws = require('nodejs-websocket'); var wss = ws.createServer(conn =>{ console.log("正在连接"); conn.on('text',function(data){ //console.log("收到:"+data); while(pp.length>0){ console.log(pp[0].toString()); conn.send(pp[0].toString()); pp.shift(); } time+=1; }) conn.on('close',function(){ conn.log('closed'); }) conn.on('error',function(){ conn.log('error'); }) }) wss.listen(3000,function(){ console.log('正在监听3000端口'); })
ws.onmessage = function(e){ var pp = (e.data).split(","); var head = parseInt(pp[0]); if(head==32){ var time = parseInt(pp[1]); tobeDraw.push([time,lon+0.3*time,lat+0.3*time]); lon -=0.01; console.log(time); } else{ console.log(pp); } };
var drawPoint = function(){while(tobeDraw.length>0){ var coord = ol.proj.transform([tobeDraw[0][1],tobeDraw[0][2]],'EPSG:4326', 'EPSG:3857'); var feature = new ol.Feature({ geometry: new ol.geom.Point(coord) }); vectorLayer.getSource().addFeature(feature); drawedpoints.push(tobeDraw[0][0]); } } var interID = window.setInterval(drawPoint,1000);
用定时器渲染,1hz时,cpu毫无压力,一屏幕点了