通信
前端
-
<title>ECharts</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <div id="main" style="height:400px;"></div> <script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script> <script src="js/jquery-1.12.3.min.js"></script> <script> var myChart; window.onload=function(){ myChart = echarts.init(document.getElementById('main')); wssk(); } function setOptionn(data,data1){ //根据需求进行相应的设置 var option = { title: { text: '数据方式' }, backgroundColor: '#FFFFFF', tooltip : { trigger: 'axis' }, legend: { data:['加入数据'] }, calculable : true, xAxis : [{ type : 'category',// 坐标轴类型,横轴默认为类目型'category',纵轴默认为数值型'value' name : '时间', data : (function (){ var now = new Date(); var res = []; var len = 10; while (len--) { res.unshift(now.toLocaleTimeString().replace(/^\D*/,'')); now = new Date(now - 2000); } return res; })() } ], yAxis : [ ], series : [ { name: '直接访问', type: 'line', barWidth: '60%', data: data } ] }; myChart.setOption(option); } function wssk(){ var ws = new WebSocket('ws://localhost:8080/ws'); ws.onmessage=function(e){ console.log(e.data) var dat=e.data; var n1=dat.length; var n2=dat.indexOf("="); var idd=dat.substr(n2+1,n1-n2); console.log(idd); setOptionn(idd); }; ws.onerror=function(err){ console.log(err); }; ws.onopen=function(){ }; ws.onclose=function(){ console.log('_close'); }; } // 初次加载图表 </script> </body> </html>
后端
// Websocket
var webSocketsServerPort = 8080;
var webSocketServer = require(‘websocket’).server;
var http = require(‘http’);
var server = http.createServer(function(request, response) {
// Not important for us. We’re writing WebSocket server, not HTTP server
});
var clients = [];
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
wsServer.on(‘request’, function(request) {
console.log((new Date()) + ’ Connection from origin ’ + request.origin + ‘.’);
var connection = request.accept(null, request.origin);
console.log((new Date()) + ’ Connection accepted.’);
var index = clients.push(connection) - 1;
// user sent some message
connection.on('message', function(message) {
onReceive(message);
});
// user disconnected
connection.on('close', function(connection) {
console.log((new Date()) + " Peer "
+ connection.remoteAddress + " disconnected.");
// remove user from the list of connected clients
clients.splice(index, 1);
});
});
function onReceive(msg)
{
console.log(“ws msg:” + msg);
serialPort.write(msg);
}
function onSerial(msg)
{
console.log(“uart msg:” + msg);
for (var i=0; i < clients.length; i++)
clients[i].sendUTF(msg);
}
// Serial port
var SerialPort = require(“serialport”)
var portName = ‘COM6’;
var buffer = “”;
var serialPort = new SerialPort(portName, {
baudRate: 9600,
// defaults for Arduino serial communication
dataBits: 8,
parity: ‘none’,
stopBits: 1,
flowControl: false
});
serialPort.on(“open”, function () {
console.log(‘open serial communication’);
// Listens to incoming data
serialPort.on(‘data’, function(data) {
buffer += new String(data);
var lines = buffer.split("\n");
while ( lines.length > 1 )
onSerial( lines.shift() );
buffer = lines.join("\n");
});
});
其实还有点没懂