使用包node-opcua版本 2.14.0(位于:https ://github.com/node-opcua )我创建了一个简单的客户端,它将连接到服务器、创建会话并创建订阅以侦听值变化.
我正在试验的问题是,当连接丢失时,node-opcua 客户端似乎需要很长时间才能成功重新连接到服务器并恢复订阅。
当我强制断开连接(例如关闭 wifi)时,会发生以下情况:
1.订阅消息停止来自服务器 2.大约 60 秒后,“ timed_out_request ”事件触发了许多消息,例如:
requestHeader: RequestHeader {
authenticationToken: NodeId { identifierType: 1, value: 4213157730, namespace: 0 },
timestamp: 2020-09-09T14:49:46.851Z { high_low: [Array], picoseconds: 0 },
requestHandle: 24,
returnDiagnostics: 0,
auditEntryId: '',
timeoutHint: 50000,
additionalHeader: null
},
subscriptionAcknowledgements: [
SubscriptionAcknowledgement {
subscriptionId: 4265258243,
sequenceNumber: 13
}
]
}
注意: “关闭”事件似乎没有触发!
然后我重新连接网络,这将发生:
1.我计算机上的其他网络客户端将重新连接并继续工作(例如,youtube、MQTT 客户端、数据库客户端等) 2. node-opcua 客户端将继续接收具有相同消息类型的 ' timed_out_request ' 事件3.大约 5 分钟后,' start_reconnection ' 事件将触发 4. node-opcua 客户端将重新连接到服务器并恢复订阅
恢复连接后,有什么方法可以将 node-opcua 客户端设置为更快地重新连接?
提前致谢!
这是我的客户:
import { OPCUAClient,
MessageSecurityMode,
SecurityPolicy,
OPCUAClientOptions,
ClientSubscription,
DataValue,
ClientMonitoredItem,
ReadValueIdLike,
MonitoringParametersOptions,
AttributeIds,
TimestampsToReturn
} from 'node-opcua';
// Server connection
const connectionStrategy = {
initialDelay: 1000,
maxRetry: 1,
};
const options: OPCUAClientOptions = {
applicationName: 'AppName',
connectionStrategy: connectionStrategy,
securityMode: MessageSecurityMode.None,
securityPolicy: SecurityPolicy.None,
endpoint_must_exist: false,
};
const url = 'opc.tcp://opcuaserver.com:48010';
async function main() {
console.log('> Connecting...');
const client = OPCUAClient.create(options);
await client.connect(url);
console.log('> Connected !');
try {
// Create session
console.log('> Creating session...');
const session = await client.createSession();
console.log('> Session created !');
// Catch changes in the connection status
attachEventListeners(client);
// Create a subscription
initSubscription(client, session);
} catch (e) {
console.log(`couldn't create opcua session. error: ${e}`);
}
}
main().catch().then(()=> console.log('process finished'));
function attachEventListeners(client) {
client.on("start_reconnection", () => {
console.log(`> Starting reconnection...`);
})
.on("connection_reestablished", () => {
console.log(`> Connection reestablished !`);
})
.on("connection_failed", (e) => {
console.log(`> Connection failed !`, e);
})
.on("reconnection_attempt_has_failed", (e) => {
console.log(`> Re-connection attempt has failed !`, e);
})
.on("close", () => {
console.log(`> Connection closed !`);
})
.on("backoff", () => {
console.log(`> Connection backoff !`);
})
.on("after_reconnection", (err) => {
console.log( "> Reconnection process has been completed: ", err );
})
.on("timed_out_request", (request) => {
console.log( "> Request has timed out without receiving a response:", request);
});
}
function initSubscription(client, session) {
const subscription = ClientSubscription.create(session, {
requestedPublishingInterval: 10,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10,
});
subscription.on("keepalive", (x) => {
console.log("> Subscription > Keepalive", x);
})
.on("terminated", () => {
console.log("> Subscription > Terminated");
});
// Subscribe to "Air Conditioner Humidity" sensor
const itemToMonitor: ReadValueIdLike = {
nodeId: "ns=3;s=AirConditioner_6.Humidity",
attributeId: AttributeIds.Value
};
const parameters: MonitoringParametersOptions = {
samplingInterval: 10,
discardOldest: true,
queueSize: 10
};
const monitoredItem = ClientMonitoredItem.create(
subscription,
itemToMonitor,
parameters,
TimestampsToReturn.Both
);
monitoredItem.on("err", (err) => {
console.log(`subscribed resource error: ${err}`);
});
monitoredItem.on("changed", (value: DataValue) => {
console.log(`subscribed resource changed value: ${value.value.value}`);
});
}