Node-OPCUA 项目教程
1. 项目的目录结构及介绍
Node-OPCUA 项目的目录结构如下:
node-opcua/
├── code_gen/
├── docker/
├── documentation/
├── modeling/
├── packages/
├── packages_extra/
├── packets/
├── test/
├── .codeclimate.yml
├── .eslintignore
├── .eslintrc.js
├── .gitattributes
├── .gitignore
├── .gitlab-ci.yml
├── .ignore
├── .jshintrc
├── .mocharc.js
├── .mocharc.yml
├── .npmignore
├── .nycrc.yaml
├── LICENSE
├── README.md
├── updateReleaseNotes.js
├── webpack.config.js
├── webpack.dll.config.js
└── yuidoc.json
目录介绍
code_gen/
: 代码生成相关文件。docker/
: Docker 容器相关文件。documentation/
: 项目文档。modeling/
: 模型相关文件。packages/
: 核心包文件。packages_extra/
: 额外包文件。packets/
: 数据包相关文件。test/
: 测试文件。.codeclimate.yml
: CodeClimate 配置文件。.eslintignore
: ESLint 忽略文件。.eslintrc.js
: ESLint 配置文件。.gitattributes
: Git 属性配置文件。.gitignore
: Git 忽略文件。.gitlab-ci.yml
: GitLab CI 配置文件。.ignore
: 忽略文件。.jshintrc
: JSHint 配置文件。.mocharc.js
: Mocha 配置文件。.mocharc.yml
: Mocha 配置文件。.npmignore
: NPM 忽略文件。.nycrc.yaml
: NYC 配置文件。LICENSE
: 项目许可证。README.md
: 项目说明文档。updateReleaseNotes.js
: 更新发布说明脚本。webpack.config.js
: Webpack 配置文件。webpack.dll.config.js
: Webpack DLL 配置文件。yuidoc.json
: YUIDoc 配置文件。
2. 项目的启动文件介绍
Node-OPCUA 项目的启动文件通常位于 packages/node-opcua-samples/bin/
目录下。以下是一个示例启动文件 simple_server.js
的介绍:
const { OPCUAServer } = require("node-opcua");
async function main() {
const server = new OPCUAServer({
port: 4840, // 默认端口
resourcePath: "/UA/MyLittleServer",
buildInfo: {
productName: "MySampleServer",
buildNumber: "1",
buildDate: new Date(2024, 1, 1)
}
});
await server.initialize();
await server.start();
console.log("Server is now listening ... ( press CTRL+C to stop)");
console.log("port ", server.endpoints[0].port);
console.log("endpointUrl", server.endpoints[0].endpointDescriptions()[0].endpointUrl);
process.once("SIGINT", async () => {
await server.shutdown();
console.log("Server has been shut down");
process.exit(0);
});
}
main();
启动文件介绍
OPCUAServer
: 创建一个 OPC UA 服务器实例。server.initialize()
: 初始化服务器。server.start()
: 启动服务器。process.once("SIGINT", ...)
: 捕获 CTRL+C 信号,优雅地关闭服务器。
3. 项目的配置文件介绍
Node-OPCUA 项目的配置文件主要包括以下几个:
webpack.config.js
Webpack 配置文件,用于打包项目。
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__