WebSocket driver

Updated at:

IoT Edge provides drivers to connect devices that use the WebSocket communication protocol. IoT Edge offers a variety of WebSocket drivers for different gateway device architectures. You can deploy WebSocket drivers directly from the console to your gateway.

To connect a device using a WebSocket driver, you must follow the IoT Edge WebSocket access protocol. For more information, see IoT Edge Device Access Protocol (WebSocket Edition).

A WebSocket driver can directly accept device connections. The following diagram shows this process.WebSocket驱动直接设备
A WebSocket driver can also accept device connections through a gateway. The following diagram shows this process.WebSocket驱动网关接设备

This topic provides an example of how to use the official WebSocket driver.

Prerequisites

An edge instance is created and a gateway is connected to Link IoT Edge. For more information, see Set up environments.

Step 1: Assign the driver

  1. Log on to the Link IoT Edge console.
  2. In the left-side navigation pane, click Edge Instances. On the Edge Instances page, find the edge instance that you want to use and click View in the Actions column.
  3. On the Instance Details page, click the + icon next to Devices & Drivers.
  4. In the Assign Driver panel, select Official Drivers. Find the WebSocket driver that matches your gateway's CPU architecture and click Assign in the Actions column. Then, click Close.
    Note The WebSocket driver is supported only on IoT Edge v2.0.0 and later.
    分配WebSocket驱动到实例

Step 2: Configure the driver

  1. Click the assigned WebSocket driver. On the page that appears, click Driver Configuration to the right of the Device List
    Set the parameters as required and then click OK.驱动配置
    Table 1. Parameter description
    ParameterDescription
    IP addressThe listening IP address for the WebSocket driver. The default value is the IP address of the machine where the driver is located.
    Port numberThe listening port for the WebSocket driver. The value ranges from 1 to 65535. The default port is 17682.
    Enable encryptionSpecifies whether to use TLS encryption. By default, encryption is disabled. If you select Yes, you must upload a public key file and a private key file.
    • Public key file: Upload the public key certificate file. The file must be in .cer format. The file name cannot contain Chinese characters.
    • Private key file: Upload the private key file. The file must be in .pvk format. The file name cannot contain Chinese characters.
  2. Optional. On the right side of Devices, click Container Configurations. On the Container Configurations page, configure the container for the driver based on the parameters that are described in the following table. Click Save.
    Note You can configure the container only when the Instance Type parameter of the edge instance is set to Pro Edition.
    ParameterDescription
    Use Host NetworkSpecifies whether to isolate the container's network and use the host network environment directly.
    • Yes: The container's network is not isolated. The host network is used directly.
    • No: The container's network is isolated. You must configure Port Mapping.
    Port MappingThis parameter appears when Use Host Network is set to No. The network environment of the function is completely isolated from the host environment. Through port mapping, you can map the listening port of the function in the container to a port on the host. This allows client programs on different hosts to access the service provided by the function. You can map up to 10 network ports.

    For example, the fc-http-server function runs in a container on the host and provides services on port 80. In this case, client programs on other hosts cannot access the fc-http-server function by accessing port 80 of the host. Therefore, you need to map the container port of the fc-http-server function to a port on the host. For example, map port 80 in the container to port 8080 on the host. This allows client programs on other hosts to access the fc-http-server function in the container by accessing Host IP address:8080.

    Enable Privileged Mode

    The root user in a container is actually a regular user on the host. To perform operations that require root permissions inside the container, such as modifying the system time or using the mount command, you must grant the container privileged mode.

    Note In privileged mode, the container has root permissions on the host. All host devices are mapped to the container by default. You do not need to configure Device Mapping.
    Device MappingThis parameter appears when Enable Privileged Mode is set to No. The device management system is completely isolated from the host environment. When a function needs to access a host device, such as a serial port, you must map the device to the container where the function runs. You can add up to 10 device mappings.
    Volume MappingThe file system is completely isolated from the host environment. When a function needs to access a host file, you must map the file to the container where the function runs. You can add up to 10 volume mappings.
    Memory LimitSet the maximum memory for the container.
    • If the container's memory usage exceeds the limit, the container is restarted.
    • If the memory limit is too small, the application in the container may fail to run.

    The default memory is 1024 MB. Increase the memory limit based on the size of the application in the container.

Step 3: Associate the driver with a sub-device

  1. In the Device List area, click Assign Sub-device to assign a sub-device to the edge instance.
    You can assign an existing WebSocket device or create a new one.
  2. In the Assign Sub-device panel, click Add Sub-device.
    添加子设备按钮
  3. In the Add Device dialog box, click New Product to create a product for the WebSocket device.新增产品
  4. In the Create Product dialog box, set the parameters and then click Done.
    websocket驱动产品
    Table 2. Parameter description
    ParameterDescription
    Product NameSet a name for the product. The product name must be unique within your account. The name can contain letters, digits, underscores (_), hyphens (-), at signs (@), and parentheses (()). It must be 4 to 30 characters in length.
    CategorySelect a category to define a Thing Specification Language model for the product. In this example, select Custom Category.
    Gateway ProtocolYou must select Custom.
    Authentication MethodSelect an authentication method suitable for your device. For more information, see Device security authentication.
    Product DescriptionAdd a description for the product. This can be empty. (Maximum 100 characters.)
  5. In the Add Device dialog box, enter a DeviceName for the WebSocket device.
  6. Assign the newly created WebSocket device to the edge instance.
  7. After you assign the device, click Device Configuration in its Actions column to associate it with the WebSocket driver.
    设备配置

    Device IP: The IP address of the connected device or its gateway.

    Note If you configure this parameter, IoT Edge validates the IP address. Connections for this device will be accepted only from the specified IP address.

Step 4: Deploy the edge instance

On the Instance Details page, click Deploy in the upper-right corner to redeploy the instance.