我的计划是在NodeJS中编写一个Query“class”,它连接到服务器并发送一些数据。
我的Query.js
var net = require('net');
function Query(ip,port,user,pass,sid){
this.ip = ip;
this.port = port;
this.user = user;
this.pass = pass;
this.sid = sid;
this.dataString = "";
this.socket = net.connect(port,ip,function(){
this.socket.write("login "+user+" "+pass+"\r\n");
this.socket.write("use "+sid+"\r\n");
});
this.socket.on("data", function (data) {
this.dataString += data.toString();
});
}
Query.prototype.test = function(){
this.socket.write("logview\r\n");
}
module.exports = Query;
这是我创建对象的部分(index.js):
var srvleinQ = new Query(config.Serverlein.adress,config.Serverlein.port,config.Serverlein.user,config.Serverlein.pass,config.Serverlein.sid);
srvleinQ.test();
htmlData = srvleinQ.dataString;
IP,端口和登录数据正确无误!