Javascript 秤重->;NodeJS通过TCP套接字连接到JSON,用于
正在尝试(半成功)从TCP套接字中提取权重。然后,我想将数据输出到JSON字符串,这样我就可以将其以某种角度进行包装。您可以在控制台中看到,我正在接收权重,我最终将去掉文本项的开头和结尾,这样输出将类似于: {“scaleA”:“43636LG”、“scaleB”等……} weightclient.jsJavascript 秤重->;NodeJS通过TCP套接字连接到JSON,用于,javascript,angularjs,json,node.js,sockets,Javascript,Angularjs,Json,Node.js,Sockets,正在尝试(半成功)从TCP套接字中提取权重。然后,我想将数据输出到JSON字符串,这样我就可以将其以某种角度进行包装。您可以在控制台中看到,我正在接收权重,我最终将去掉文本项的开头和结尾,这样输出将类似于: {“scaleA”:“43636LG”、“scaleB”等……} weightclient.js 'use strict' var net = require('net'); var HOST = '192.168.1.17'; var PORT = 1337; var client =
'use strict'
var net = require('net');
var HOST = '192.168.1.17';
var PORT = 1337;
var client = new net.Socket();
var weight = function(){
var weights = {};
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.on('data', function(data) {
// console.log('DATA: ' + data);
weights.scaleA = data.toString();
console.log(weights);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
});
return {
weights
};
}();
console.log(weight);
module.exports = weight;
var express = require('express');
var router = express.Router();
var net = require('net');
var getweight = require('../weightclient.js');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/weight', function(req, res) {
getweight.weight(function(err, data){
console.log(data);
});
});
module.exports = router;
index.js
'use strict'
var net = require('net');
var HOST = '192.168.1.17';
var PORT = 1337;
var client = new net.Socket();
var weight = function(){
var weights = {};
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.on('data', function(data) {
// console.log('DATA: ' + data);
weights.scaleA = data.toString();
console.log(weights);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
});
return {
weights
};
}();
console.log(weight);
module.exports = weight;
var express = require('express');
var router = express.Router();
var net = require('net');
var getweight = require('../weightclient.js');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/weight', function(req, res) {
getweight.weight(function(err, data){
console.log(data);
});
});
module.exports = router;
控制台(输出):
node bin/www
{ weights: {} }
CONNECTED TO: 192.168.1.17:1337
{ scaleA: '\u0002 43636LG \r\n' }
Connection closed
GET /weight 500 2973.083 ms - 1131
GET /stylesheets/style.css 304 25.835 ms - -
getweight.weight is not a function
TypeError: getweight.weight is not a function
at /home/pi/socketio-test/routes/index.js:13:15
at Layer.handle [as handle_request] (/home/pi/socketio-test/node_modules/express/lib/router/layer.js:95:5)
at next (/home/pi/socketio-test/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/pi/socketio-test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/pi/socketio-test/node_modules/express/lib/router/layer.js:95:5)
at /home/pi/socketio-test/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/pi/socketio-test/node_modules/express/lib/router/index.js:330:12)
at next (/home/pi/socketio-test/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/home/pi/socketio-test/node_modules/express/lib/router/index.js:176:3)
at router (/home/pi/socketio-test/node_modules/express/lib/router/index.js:46:12)
网络输出:
node bin/www
{ weights: {} }
CONNECTED TO: 192.168.1.17:1337
{ scaleA: '\u0002 43636LG \r\n' }
Connection closed
GET /weight 500 2973.083 ms - 1131
GET /stylesheets/style.css 304 25.835 ms - -
getweight.weight is not a function
TypeError: getweight.weight is not a function
at /home/pi/socketio-test/routes/index.js:13:15
at Layer.handle [as handle_request] (/home/pi/socketio-test/node_modules/express/lib/router/layer.js:95:5)
at next (/home/pi/socketio-test/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/pi/socketio-test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/pi/socketio-test/node_modules/express/lib/router/layer.js:95:5)
at /home/pi/socketio-test/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/pi/socketio-test/node_modules/express/lib/router/index.js:330:12)
at next (/home/pi/socketio-test/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/home/pi/socketio-test/node_modules/express/lib/router/index.js:176:3)
at router (/home/pi/socketio-test/node_modules/express/lib/router/index.js:46:12)
想法和建议。。仍在学习node和js,非常感谢任何指点或参考。您必须拥有(我喜欢英语):
var arr = []
和代码:
when response from server arrive{
arr.push([data.name,data.weight])}
然后你可以编写其他东西。
希望我没弄错你的问题
我的方法将为您节省大量调试时间
错误btw在必需的文件中,所以我根本帮不上忙,权重没有定义为函数,或者它是以私有方式定义的。除非我缺少某个键,否则您的
返回{weights}代码>是无效的javascript。