154 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

var fs = require('fs');
var http = require('http');
var net = require('net');
var HTTP_PORT = "8000";
var TCP_PORT = "9000"
var TIMEOUT = 30*1000;//tcp客户端超过30秒没发数据判为超时并断开连接
var tcpClient=null;//tcp客户端
2019-03-19 20:38:19 +08:00
// 创建http server并传入回调函数:
var httpServer = http.createServer(function (request, response) {
2019-03-19 20:38:19 +08:00
// 回调函数接收request和response对象,
// 获得HTTP请求的method和url:
console.log(request.method + ': ' + request.url);
switch (request.url) {
case "/":
//访问首页
// 读取html文件并发送
response.end(fs.readFileSync('./index.html'));
break;
case "/open":
// 开灯命令
openLed()
response.end('succeed');
break;
case "/close":
// 开灯命令
closeLed()
response.end('succeed');
break;
case "/data":
// 获取数据
var data = getData() || "无数据";
2019-03-19 20:38:19 +08:00
var addr = "无连接";
2019-03-19 20:38:19 +08:00
if(tcpClient && tcpClient.addr){
addr = tcpClient.addr
}
// 将结果转换成字符串再发出去
var result = JSON.stringify({addr:addr,data:data});
2019-03-19 20:38:19 +08:00
response.end(result);
break;
default:
response.writeHead(400);
response.end();
break;
}
2019-03-16 17:49:52 +08:00
});
2019-03-19 20:38:19 +08:00
httpServer.listen(HTTP_PORT);
httpServer.on('error', onError);
httpServer.on('listening', onListening);
//创建TCP服务器
var tcpServer = net.createServer((socket)=>{
2019-03-19 20:38:19 +08:00
//connect
var addr = socket.remoteAddress + ':' + socket.remotePort
console.log(addr," connect.",socket)
2019-03-19 20:38:19 +08:00
socket.addr = addr
tcpClient = socket
// recieve data
socket.on("data",data=>{
var str = addr+" --> " + data.toString('ascii') + '\n'
2019-03-19 20:38:19 +08:00
console.log(str)
socket.lastValue = data.toString('ascii')
})
// close
socket.on('close',()=>{
console.log(addr,"close")
tcpClient = null;
})
socket.on('error',(err)=>{
console.log("error",err)
tcpClient = null;
})
socket.setTimeout(TIMEOUT);
// 超过一定时间 没接收到数据,就主动断开连接。
socket.on('timeout', () => {
console.log(socket.id,socket.addr,'socket timeout');
socket.end();
tcpClient = null;
});
})
tcpServer.on("error",(err)=>{
console.log(err)
tcpClient = null;
})
tcpServer.listen({port: TCP_PORT,host: '0.0.0.0'}, () => {
console.log('demo0.1 tcp server running on', tcpServer.address())
})
// 开灯
function openLed() {
// 向TCP客户端发送1
if(tcpClient){
tcpClient.write('1', 'ascii')
}
else{
console.log("openLed error:not tcpClient.")
}
}
// 关灯
function closeLed() {
// 向TCP客户端发送0
if(tcpClient){
tcpClient.write('0', 'ascii')
}
else{
console.log("closeLed error:not tcpClient.")
}
}
// 获取数据
function getData() {
// 获取设备最新数据
if(tcpClient){
return tcpClient.lastValue
}
else{
console.log("getData error:not tcpClient.")
}
}
2019-03-16 17:49:52 +08:00
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
console.error(error)
2019-03-16 17:49:52 +08:00
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
2019-03-19 20:38:19 +08:00
var addr = httpServer.address();
2019-03-16 17:49:52 +08:00
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
2019-03-19 20:38:19 +08:00
console.log('http server Listening on ' + bind);
2019-03-16 17:49:52 +08:00
}