我目前正在尝试在两台不同的计算机之间运行服务器和客户端。但是,服务器运行良好,但客户端却不行。我曾尝试使用 sudo ufw 端口 4334 但无济于事。有人可以帮我吗。
/*global require,console,setTimeout */
var opcua = require("node-opcua");
var async = require("async");
var fs = require("fs");
var csv = require("fast-csv");
var sleep = require("system-sleep");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname() + ":4334/UA/MyLittleServer";
var the_session, the_subscription;
async.series([
// step 1 : connect to
function(callback) {
client.connect(endpointUrl,function (err) {
if(err) {
console.log(" cannot connect to endpoint :" , endpointUrl );
} else {
console.log("connected !");
console.log("Endpoint URL ", endpointUrl);
}
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession( function(err,session) {
if(!err) {
the_session = session;
}
callback(err);
});
},
// step 3 : browse
function(callback) {
the_session.browse("RootFolder", function(err,browse_result){
if(!err) {
browse_result[0].references.forEach(function(reference) {
console.log( reference.browseName.toString());
});
}
callback(err);
});
},
// step 4 : read a variable with readVariableValue
//function(callback) {
// the_session.readVariableValue("ns=2000;s=TEST", function(err,dataValue) {
// if (!err) {
// console.log(" free mem % = " , dataValue.toString());
// }
// callback(err);
// });
//},
// step 4' : read a variable with read
//function(callback) {
// var max_age = 0;
// var nodes_to_read = [
// { nodeId: "ns=2000;s=TEST", attributeId: opcua.AttributeIds.Value }
// ];
// the_session.read(nodes_to_read, max_age, function(err,nodes_to_read,dataValues) {
// if (!err) {
// console.log(" free mem % = " , dataValues[0]);
// }
// callback(err);
// });
//},
// function(callback){
// the_session.readVariableValue("ns=74;s=Dou", function(err,dataValue) {
// if(!err){
// console.log("Test Success", dataValue.toString());
// }
// callback(err);
// });
// },
//
// function(callback){
// the_session.readVariableValue("ns=74;s=Float", function(err,dataValue) {
// if(!err){
// console.log("Test Success", dataValue.toString());
// }
// callback(err);
// });
// },
//
// function(callback){
// the_session.readVariableValue("ns=74;s=String", function(err,dataValue) {
// if(!err){
// console.log("Test Success", dataValue.toString());
// }
// callback(err);
// });
// },
// function(callback){
// the_session.readVariableValue("ns=1;s=CSV", function(err, dataValue) {
// if(!err){
// console.log(dataValue.toString());
// sleep(5000);
// }
// callback(err);
// });
// },
// function(callback){
// the_session.readVariableValue("ns=1;s=CSV", function(err, dataValue) {
// if(!err){
// fs.createReadStream(dataValue.toString())
// console.log(dataValue.toString());
// sleep(5000);
// .pipe(csv())
// .on('data', function(data){
// console.log(csv);
// sleep(5000);
// })
// .op('end', function(data){
// console.log("Read Finish")
// });
// }
// callback(err);
// });
// },
// step 5: install a subscription and install a monitored item for 10 seconds
function(callback) {
the_subscription=new opcua.ClientSubscription(the_session,{
requestedPublishingInterval: 1000,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});
the_subscription.on("started",function(){
console.log("subscription started for 2 seconds - subscriptionId=",the_subscription.subscriptionId);
}).on("keepalive",function(){
console.log("keepalive");
}).on("terminated",function(){
callback();
});
setTimeout(function(){
the_subscription.terminate();
},10000000);
// install monitored item
var monitoredItem = the_subscription.monitor({
nodeId: opcua.resolveNodeId("ns=2000;s=TEST"),
attributeId: opcua.AttributeIds.Value
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
},
opcua.read_service.TimestampsToReturn.Both
);
console.log("-------------------------------------");
monitoredItem.on("changed",function(dataValue){
console.log(" New Data Receive = ",dataValue.value.value);
});
},
// close session
function(callback) {
the_session.close(function(err){
if(err) {
console.log("session closed failed ?");
}
callback();
});
}
],
function(err) {
if (err) {
console.log(" failure ",err);
} else {
console.log("done!");
}
client.disconnect(function(){});
}) ;