created tcp server, tcpserver:
var net = require("net")
var mysql = require("mysql");
var conn = mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"gprs",
port:3306
});
conn.connect();
net.createServer(function(socket){
socket.on("data",function(data){
console.log("got:",data.toString());
var text = JSON.parse(data.toString());
var arr = {};
arr.tem = text.tem;
arr.hum = text.hum;
arr.illsum = text.illsum;
console.log(arr);
console.log(arr.tem);
console.log(arr.hum);
console.log(arr.illsum);
//mysql
conn.query("INSERT INTO gprsdata SET ?", arr, function(error,result,fields){
if (error) throw error;
});
});
socket.on(
"end",function(data){
console.log("end");
});
socket.write("Ready to receive your message!")
}).listen(4001);
tcpclient.js: created
const net = require("net");
const client = net.connect({port:4001},()=> {
console.log("connected to server!");
client.write("{\"tem\":\"20\",\"hum\":\"30\",\"illsum\":\"473\"}");
});
client.on("data",(data) => {
console.log(data.toString());
client.end();
});
client.on("end",() => {
console.log("disconnected from server");
});