RuleGo RuleGo
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • 快速入门

  • 规则链

  • 标准组件

    • 标准组件概述
    • 过滤器

      • js脚本过滤器
      • 字段过滤器
      • 消息路由
      • js脚本路由
        • 配置
        • Relation Type
        • 执行结果
        • 配置示例
          • 基本路由示例
          • 多路由示例
          • 基于数据类型的路由示例
          • JSON数据路由示例
          • 二进制设备数据路由示例
          • 文本日志路由示例
          • 混合数据处理路由示例
      • 过滤器组
      • 表达式过滤器
      • 并行网关
      • 条件分支
    • 动作

    • 转换器

    • 外部的

    • 流

  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

js脚本路由

jsSwitch组件:脚本路由。执行已配置的JS脚本,根据脚本返回值动态路由消息到一个或多个输出链。

JavaScript脚本支持ECMAScript 5.1(+) 语法规范和部分ES6规范,如:async/await/Promise/let。允许在脚本中调用Go自定义函数,请参考udf 。

# 配置

字段 类型 说明 默认值
jsScript string js脚本 无
  • jsScript:可以对msg、metadata、msgType、dataType进行处理和判断。该字段是以下函数体内容:

        function Switch(msg, metadata, msgType, dataType) { 
            ${jsScript} 
         }
    
    1
    2
    3

    参数说明:

    • msg:消息内容
      • 当dataType=JSON时,类型为jsonObject,可使用msg.temperature方式操作
      • 当dataType=BINARY时,类型为Uint8Array,可直接操作字节数组,如msg[0]访问第一个字节
      • 其他dataType时,类型为string
    • metadata:消息元数据,类型为jsonObject
    • msgType:消息类型,类型为string
    • dataType:消息数据类型(JSON、TEXT、BINARY等),需要使用String(dataType)转换为字符串使用
    • 函数返回值类型:数组,返回一个字符串数组,包含要路由到的一个或多个链名称

注意

  1. 脚本执行超时时间配置参考: config.ScriptMaxExecutionTime
  2. 返回的链名称必须在规则链connections中定义,否则消息会被丢弃

# Relation Type

使用脚本返回值动态决定路由关系,可以路由到一个或多个输出链。如果返回的链名称没有对应的连接,则会使用Default链进行路由。

# 执行结果

该组件不会改变msg、metadata和msgType内容,仅用于决定消息的路由方向。

# 配置示例

# 基本路由示例

  {
    "id": "s1",
    "type": "jsSwitch",
    "name": "脚本路由",
    "configuration": {
      "jsScript": "if (msg.temperature > 50) return ['highTemp']; else if (msg.temperature < 10) return ['lowTemp']; else return ['normalTemp'];"
    }
  }
1
2
3
4
5
6
7
8

# 多路由示例

  {
    "id": "s2",
    "type": "jsSwitch",
    "name": "多路由",
    "configuration": {
      "jsScript": "var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (msg.priority === 'high') routes.push('priority'); return routes;"
    }
  }
1
2
3
4
5
6
7
8

# 基于数据类型的路由示例

  {
    "id": "s3",
    "type": "jsSwitch",
    "name": "数据类型路由",
    "configuration": {
      "jsScript": "var dt = String(dataType); if (dt === 'BINARY') { if (msg.length > 1024) return ['largeBinary']; else return ['smallBinary']; } else if (dt === 'JSON') return ['jsonData']; else return ['textData'];"
    }
  }
1
2
3
4
5
6
7
8

# JSON数据路由示例

  {
    "id": "s4",
    "type": "jsSwitch",
    "name": "JSON数据路由",
    "configuration": {
      "jsScript": "if (String(dataType) === 'JSON') { var routes = []; if (msg.temperature > 50) routes.push('highTemp'); if (msg.humidity > 80) routes.push('highHumidity'); if (msg.level === 'critical') routes.push('critical'); return routes.length > 0 ? routes : ['normal']; } return ['skip'];"
    }
  }
1
2
3
4
5
6
7
8

# 二进制设备数据路由示例

  {
    "id": "s5",
    "type": "jsSwitch",
    "name": "设备数据路由",
    "configuration": {
      "jsScript": "if (String(dataType) === 'BINARY' && msg.length >= 4) { var deviceId = (msg[0] << 8) | msg[1]; var functionCode = (msg[2] << 8) | msg[3]; if (deviceId === 0x1001) { if (functionCode === 0x0001) return ['sensorData']; else if (functionCode === 0x0002) return ['statusData']; else if (functionCode === 0x0010) return ['commandData']; } return ['unknownDevice']; } return ['invalidData'];"
    }
  }
1
2
3
4
5
6
7
8

# 文本日志路由示例

  {
    "id": "s6",
    "type": "jsSwitch",
    "name": "日志路由",
    "configuration": {
      "jsScript": "if (String(dataType) === 'TEXT') { var routes = []; if (msg.includes('ERROR')) routes.push('errorLog'); if (msg.includes('WARN')) routes.push('warnLog'); if (msg.includes('DEBUG')) routes.push('debugLog'); return routes.length > 0 ? routes : ['infoLog']; } return ['nonTextData'];"
    }
  }
1
2
3
4
5
6
7
8

# 混合数据处理路由示例

  {
    "id": "s7",
    "type": "jsSwitch",
    "name": "混合数据路由",
    "configuration": {
      "jsScript": "var dt = String(dataType); var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (dt === 'JSON' && msg.priority === 'high') routes.push('priority'); else if (dt === 'BINARY' && msg.length > 0 && msg[0] === 0xFF) routes.push('protocolData'); else if (dt === 'TEXT' && msg.includes('URGENT')) routes.push('urgent'); return routes.length > 0 ? routes : ['default'];"
    }
  }
1
2
3
4
5
6
7
8
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/06/23, 10:45:49
消息路由
过滤器组

← 消息路由 过滤器组→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式