【以下的问题经过翻译处理】 你好,
我有以下代码可以在注册到Greengrass的任何设备上使用IPC套接字。但是我无法找到在nodejs中执行它的好例子。有办法实现吗?我找不到像Python版本中设置SVCUID参数的地方。我的当前无法工作的nodejs版本如下所示:
Nodejs版本:
...
import { greengrass,iot,io,mqtt } from 'aws-iot-device-sdk-v2';
…
const hostname =
process.env.AWS_GG_NUCLEUS_DOMAIN_SOCKET_FILEPATH_FOR_COMPONENT;
const clientBootstrap = new io.ClientBootstrap();
const configBuilder =
iot.AwsIotMqttConnectionConfigBuilder.new_websocket_builder()
.with_clean_session(false)
.with_socket_options(
new io.SocketOptions(io.SocketType.STREAM, io.SocketDomain.LOCAL, 3000)
);
const config = configBuilder.build();
config.host_name = hostname;
config.port = 8033;
const client = new mqtt.MqttClient(clientBootstrap);
const connection = client.new_connection(config);
Python版本:
…
import os
from awscrt.io import (
ClientBootstrap,
DefaultHostResolver,。
EventLoopGroup,,
SocketDomain,,
SocketOptions,,
)
from awsiot.eventstreamrpc import Connection,LifecycleHandler, MessageAmendment
TIMEOUT = 10
class IPCUtils:
def connect(self):
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg,resolver)
socket_options = SocketOptions()
socket_options.domain = SocketDomain.Local
amender = MessageAmendment.create_static_authtoken_amender(os.getenv("SVCUID"))
hostname = os.getenv("AWS_GG_NUCLEUS_DOMAIN_SOCKET_FILEPATH_FOR_COMPONENT")
connection = Connection(
host_name=hostname,
port=8033,
bootstrap=bootstrap,
socket_options=socket_options,
connect_message_amender=amender,
)
self.lifecycle_handler = LifecycleHandler()
connect_future = connection.connect(self.lifecycle_handler)
connect_future.result(TIMEOUT)
return connection
谢谢,
Sukru